Question Detail

How to create a WebView in android ?

5 years ago Views 1458 Visit Post Reply

create a webview in which a web page work properly . I creating a webview in which a web page is open but when i click on any link on the web page it want to select browser and i want to resume browsing that page in the same app.

xml:

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

 

javafile:

public class Techximum extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_techximum);
        WebView mywebview = (WebView)findViewById(R.id.web_id);
        mywebview.loadUrl("https://www.google.com");
    }
}


Thread Reply

- 5 years ago

When we click on any link on website default behaviour of WebView is open link on Default browser. To handle these type of Navigation,

so we need to change its behaviour accordingly 

simply provide a WebViewClient for your WebView, using setWebViewClient()

Just implement the web client and set it before loadUrl.

The simplest way is:

mywebview.setWebViewClient(new WebViewClient());

For more advanced processing for the web content, consider the ChromeClient.

 

The Developer

- 3 years ago

Create a function in your CommonClass to use WebView Dialog as on any Activity or Fragment from anywhere.

 

You have first include some permissions mention below:

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

Funcation here

public static void customizedWebViewFuncation(Activity activity, String title, String URL) {
    if (isNetworkAvailable(activity)) {
        Dialog dialog = new Dialog(activity, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        dialog.setContentView(R.layout.custome_webview_layout);
        dialog.show();
        TextView webview_Title = dialog.findViewById(R.id.webview_Title_ID);
        webview_Title.setText(title);
        ImageView webview_cancel = dialog.findViewById(R.id.webview_cancel_ID);
        WebView webView = dialog.findViewById(R.id.custome_webview_ID);
        webview_cancel.setOnClickListener(v -> dialog.dismiss());
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl(URL);
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });
    } else {
        Toast.makeText(activity, "No Internet Connection.", Toast.LENGTH_SHORT).show();
    }
}

 

Where you want to show your page load in WebView android call wise

customizedWebViewFuncation(activity, title, "https://bit.ly/2sL0wtN");