Building Simple browser application in android studio :-
To build browser application in android studio first we need to create new project in android studio, to know how to create project in android studio you can check from here
Lets add Internet permission in manifest file :
![]() |
adding internet permission in manifest |
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sam.webviewdemo">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
1. Add dependency for swipedownrefresh:
dependencies {
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"}
copy and paste above dependency in build.gradle file
adding dependency in build.gradle 2. Create Layout: activity_main.xml:
layout for browser application with WebView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"android:layout_height="wrap_content" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>View after creating layout :
view after creating activity_main.xml Now Working on MainActivity.java :import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
//here id should be same as in the layout file ...
swipeRefreshLayout = findViewById(R.id.refresh_layout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webAction();
//press alt+enter to create a method by by placing on the method
}
});
}
private void webAction() {
//this method is called each time refreshed ....
/// lets write our webview functionality here...
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
//lets add a url, it means which website should be opened here
// i go with google.com
//lets copy google url..
//paste the link inside
webView.loadUrl("https://www.google.com/");
//now activate the refresh layout when swipe down...
swipeRefreshLayout.setRefreshing(true);
// now its time to add a web view client
// webview client handles the data gathering form the internet...
// for example when our app hits the server with the url
// webview client gather all the images,texts etc and load into our webview...
//webview client can be added by
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
// this method is called when a wrong or website address is given in the url
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//this method is called when the full web page is loaded into our webview
// so here we have to stop our refresh layout
swipeRefreshLayout.setRefreshing(false);
}
});
}
//last and final step is to set backpress
//when user presses back button app should not be closed it should load previous webpage
//for example if use opens 3 webpages when use presses back on the third web page it should to 2nd webpage similarly to 1st page
//so now add on backpress method here
@Override
public void onBackPressed() {
//comment this line..
// super.onBackPressed();
//this method is called when use press back button on the device
//now check weather the webview can go back or not (example if the webview contains mutiple webpages or not )
//sry...for the mistake :)
//checking...
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
//lets run our app :)
// when i swipe down our website is loaded i.e google.com lets se
//so guys we have successfully made our browser app
// back press is also working fine :)
// thanks for watching..
// please comment for any doubts ...:)
}
}
Comments
Post a Comment