WebView is a view that display web pages inside your application.
You can also specify HTML string and can show it inside your application using WebView.
WebView makes turns your application into a web application.
Map the webview into the Activity java File.
and enable the setJavaScriptEnabled(true)
then load the url into the webview.
webview.loadUrl("");
Methods of webview Class.
findAllAsync(String find) - This method finds all instances of string and highlights them.
getProgress() - This method gets the progress of the current page.
getTitle() - This method returns the title of the current page.
getUrl() - This method returns the URL of the current page.
If you click on any link inside the webpage of the WebView,
That page will not be loaded inside your WebView.
In order to do that you need to extend your class from WebViewClient and override its method.
Example :
MainActivity.Java
main_activity.xml
Don't forget to add internet permission
AndroidManifest.xml
Screenshot:
Download example
You can also specify HTML string and can show it inside your application using WebView.
WebView makes turns your application into a web application.
To create webview first, you need to create webview in activity XML.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
Map the webview into the Activity java File.
and enable the setJavaScriptEnabled(true)
then load the url into the webview.
webview.loadUrl("");
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new MyBrowser());
webView.loadUrl("https://velmuruganandroidcoding.blogspot.in");
Methods of webview Class.
canGoBack() - This method specifies the WebView has a back history item.
canGoForward() - This method specifies the WebView has a forward history item.
clearHistory() - This method will clear the WebView forward and backward history.
destroy() - This method destroys the internal state of WebView.findAllAsync(String find) - This method finds all instances of string and highlights them.
getProgress() - This method gets the progress of the current page.
getTitle() - This method returns the title of the current page.
getUrl() - This method returns the URL of the current page.
If you click on any link inside the webpage of the WebView,
That page will not be loaded inside your WebView.
In order to do that you need to extend your class from WebViewClient and override its method.
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Example :
MainActivity.Java
package rajaapps.lifeandtechnology;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new MyBrowser());
webView.loadUrl("https://velmuruganandroidcoding.blogspot.in");
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="rajaapps.lifeandtechnology.MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
Don't forget to add internet permission
<uses-permission android:name="android.permission.INTERNET"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rajaapps.lifeandtechnology">
<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>
Screenshot:
webview in android studio |
Download example
No comments:
Post a Comment