Let's learn details of WebView.
<?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
Post a Comment