Skip to main content

Android Browser - WebView - Complete Tutorial | Open a Web Page in Your Android App

 Let's learn details of WebView.

While there are great YouTube videos to get you through the entire process, but it is always handy to get some code to copy-paste. This is the purpose why I maintain this blog. This is for my own assistance. Now, if you find it useful, it will be of great help for me.

Now let us add a new empty activity.
Name it as BrowserActivity.

Write the following codes in activity_browser.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".BrowseActivity"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="30dp">

<ProgressBar
android:id="@+id/myBrowserProgressBar"
android:layout_weight="0.1"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="@+id/myBrowserImageView"
android:src="@mipmap/ic_launcher"
android:layout_weight="0.9"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

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


</LinearLayout>

</LinearLayout>

/* END OF activity_browser.xml */

Now, work with BrowserActivity.java
This is the code:-

import androidx.appcompat.app.AppCompatActivity;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class BrowseActivity extends AppCompatActivity {

ProgressBar superProgressBar;
ImageView superImageView;
WebView superWebView;


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

superProgressBar = findViewById(R.id.myBrowserProgressBar);
superImageView = findViewById(R.id.myBrowserImageView);
superWebView = findViewById(R.id.myBrowserWebView);

superProgressBar.setMax(100);
superWebView.loadUrl("https://google.com");
superWebView.getSettings().setJavaScriptEnabled(true);

superWebView.setWebViewClient(new WebViewClient());
superWebView.setWebChromeClient(new WebChromeClient(){

@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
superProgressBar.setProgress(newProgress);
}

@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
getSupportActionBar().setTitle(title);
}

@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
superImageView.setImageBitmap(icon);
}
});
}
}

End of Content for BrowserActivity.java

Grant Internet Permission in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
Your application should run successfully.

Also make sure to use the following settings in styles.xml file:-
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

This detailed WebView Tutorial must be followed for through understanding.

Comments

Popular posts from this blog

How to enable pinch zoom on a web view loading a url with pdf(Android)?

How to enable pinch zoom on a web view loading a url with pdf(Android)? Ref: https://stackoverflow.com/questions/16630382/how-to-enable-pinch-zoom-on-a-web-view-loading-a-url-with-pdfandroid   WebView mWebView = (WebView) findViewById(R.id.webView);         mWebView.getSettings().setJavaScriptEnabled( true ); WebSettings webSettings = mWebView.getSettings(); webSettings.setBuiltInZoomControls( true ); webSettings.setSupportZoom( true );

[SOLVED] Is Android Studio supported on Mac OS 10.15 (Catalina)? Android Studio Not Working on MAC

 Technically Yes! But not that all versions work the same way. While I upgraded to Android Studio to Version 4.1.2. But it did not work in OS 10.15. I deleted my previous android installations following this guide , but still nothing seemed to work. The hell bent installer seemed to get stuck in the middle. I contacted few experts who also appear clueless. A lot of time in my developers' life, I prompt to self - "Read Sid, Read!'. This was no different this time. Instead of searching for help online / offline, I started reading the Android Studio Release Note for version 4.1.2 . It clearly says ,  System Requirements  For MAC:  Mac® OS X® 10.10 (Yosemite) or higher, up to 10.14 (macOS Mojave) So, it may not work with MAC OS Catalina anyway. And it did not work as in my case. Strangely, my previous Android Version 4.0 worked. It is bit tricky to find old releases. But here is where you can find old Android Studio Archives . I downloaded Android Version 4.0, and it wor...

How to Align Views at The Bottom of The Screen

This is h ow you can Align Views at The Bottom of The Screen You know, you have to go to layout_main.xml file (If this is the layout you want to tweak with). Here we add a LinearLayout Component. Then place a TextView Component within that liner layout. Most important note: Add android:gravity="bottom" in the LinearLayout Component Add  android :gravity ="center_horizontal" in the TextView Component You are done! Here goes your SAMPLE code. < LinearLayout android :layout_width ="match_parent" android :layout_height ="match_parent" android :gravity ="bottom" android :orientation ="horizontal" > < TextView android :id ="@+id/bottomText" android :layout_width ="match_parent" android :padding ="2dp" android :textSize ="18sp" android :layout_height ="wrap_content" android :background ="@color/cardview_...