How to use WebView in android

WebView allows you to display web content as part of your activity layout. A WebView is useful when you need to increase control over the UI and advanced configuration options that will allow you to embed web pages in a specially-designed environment for your app.

The App looks Like

Github: https://github.com/rincemathew/SimpleWebView

Open AndroidManifest.xml and add ‘ACCESS_NETWORK_STATE’ permission.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

activity_main.xml

<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">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView=findViewById(R.id.webView);

        String url="http://androidstuff.net/";
        webView.loadUrl(url);

        //preventing other browser to open links
        webView.setWebViewClient(new ourViewClint());
    }
}

ourViewClint.java

class ourViewClint extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

Keep exploring more tutorial from this site. In case of any queries make a comment. Happy coding!!!!

Add a Comment

Your email address will not be published. Required fields are marked *