Android Web View Layout Example:-
Web view layout is used to display online content in your activity. Web
view class is extension of view class in Android. It’s not like a web browser
so it does not provide navigation control and URL. Web kit rendering engine
displays web pages and allow us to navigate forward and backward. Web view is
very common in use if you want to show some web page info in your application.
Or another use is if you want to provide information which needs to be updated
like user guides or agreements.
If you want to access internet in your application then you
must add INTERNET permission to your manifest file such as
<uses-permission android: name= ”android.permission.INTERNET” />
Web view methods
Following are the methods of web view layout in Android.
- addJavascriptInterface( Object object, String name), used to inject JavaScript code in web view layout.
- canGoBack(), used to
go to the previous history item.
- canGoBackOrForward(int steps), used to go back or forward as given steps.
- canGoForward(), used to
go to the next history item.
- canZoonIn(), used to
zoom in.
- canZoomOut(), used to
zoom out.
- clearHistory(), Used to
clear the history of web view.
- clearView(), Used to
reset the web view and release resources.
- destroy(), Used to destroy
internal state of the web view.
- findFocus(), Find the
view in the hierarchy rooted at this view that currently has focus.
- freeMemory(), used to
free memory, now this method is deprecated.
- getSettings(), gets the
settings of web view.
- getTitle(), gets title of
current page in the form of string.
- getURL(), gets the URL of
current page in the form of string.
- goBack(), go back to the
history of web view.
- capturePicture(), used to take bitmap snapshot of web view.
Web view example in Android
activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Welcome to codewithAndroid"
android:textColor="@android:color/black"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="40dp"
android:text="Click Here"
android:id="@+id/button" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button"
android:layout_marginTop="86dp"
android:id="@+id/webView"/>
</RelativeLayout>
MainActivity.java
package com.example.codewithandroid;
import androidx.appcompat.app.AppCompatActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button b1;
private WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
webview=(WebView)findViewById(R.id.webView);
webview.setWebViewClient(new MyBrowser());
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.CodeWithandroid.com/";
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview.loadUrl(url);
}
});
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
1 Comments
My husband heads straight for craps and typically video poker, my son-in-law heads straight for blackjack. 1xbet My daughter and I attempt to play together, however she actually likes the video better and I actually like the reels. Playing video along with her this one time, I received a type of|a sort of} free spin bonuses, and it went very well. I kept getting more free spins the center of|in the midst of|in the course of} the round, and I had some good winners too.
ReplyDelete