How to use WebView in Android
public class WebView
extends AbsoluteLayout
implements
ViewTreeObserver.OnGlobalFocusChangeListener,
ViewGroup.OnHierarchyChangeListener
WebView is a view that display web pages inside the application. It is used to turn the application into a web application.
Class Hierarchy :
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.AbsoluteLayout
↳ android.webkit.WebView
MainActivity.java
package com.example.hp.webview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Binding MainActivity.java with // activity_main.xml file setContentView(R.layout.activity_main); // Find the WebView by its unique ID WebView w = (WebView) findViewById(R.id.web); // loading http://www.google.com url in the the WebView. // this will enable the javascipt. w.getSettings().setJavaScriptEnabled(true); // WebViewClient allows you to handle // onPageFinished and override Url loading. w.setWebViewClient(new WebViewClient()); } } |
chevron_right
filter_none
activity_main.xml
In the xml file only use of WebView is made inside RelativeLayout.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.hp.webview.MainActivity"> <WebView <!-- covers 368dp width as required. --> android:layout_width="368dp" <!-- unique ID of WebView --> android:id="@+id/web" <!-- covers 495dp height as required. --> android:layout_height="495dp" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" /> </RelativeLayout> |
chevron_right
filter_none
In AndroidManifest.xml, one needs to include below permission, in order to access internet:
"uses-permission android:name="android.permission.INTERNET"
Recommended Posts:
- JavaFX | WebView Class
- Android | How to add Radio Buttons in an Android Application?
- Android | AdMob Interstitial Ads for Android Studio
- Android | AdMob Banner Ads for Android Studio
- Android | Android Application File Structure
- Android | How to Request permissions in Android Application?
- Android | Starting with first app/android project
- Android | Running your first Android app
- Android | RecyclerView
- How to use CheckBox in Android
- Android | 8.0 Oreo
- Android | App to Add Two Numbers
- Android | Creating a SeekBar
- Volley Library in Android
- Android | What is Toast and How to use it with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




