How to make WebView App in Android studio
Today i share with you how to make an app for your website.
Use this line code in your xml file Example - activity_main.xml Use this line code <RelativeLayout> Here your past the code < RelativeLayout />
<WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"/>
Add the INTERNET permission to your manifest file. outside the application tag in your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Assign Variable :
WebView webView;
Just rearrange your code a bit as follows onCreate() Method :-
webView = findViewById(R.id.webview); webView.getSettings().setLoadsImagesAutomatically(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setLoadWithOverviewMode(true); webView.getSettings().setUseWideViewPort(true); webView.setWebViewClient(new WebViewClient()); webView.getSettings().setDomStorageEnabled(true); webSettings.setAllowContentAccess(true); webView.loadUrl("YOUR WEBSITE URL");
Handling the Android Back Button
WebView has a method canGoBack which tells you if there is anything on the page stack that can be popped. All you need to do is detect a back button press and determine if you should step back through the WebView's history or allow the platform to determine the correct behaviour. Inside your MainActivity class, add the following method (in bold):
@Override public void onBackPressed() { if(webView.canGoBack()) { webView.goBack(); } else { super.onBackPressed(); } }
Thanks for visiting our website. i thik it will help you.