A
A
ALXTNTCN2021-01-18 12:12:39
Android
ALXTNTCN, 2021-01-18 12:12:39

How to prevent a WebView from opening a browser when a link is clicked?

Hello I am new to programming and just started to develop an android app so I need help with setting up the app on webview.
How to prevent opening links in a third-party browser?
Below is my MainActivity code:

package com.example.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends Activity {

    private WebView mWebView;

    @Override
    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new MyWebViewClient());



        // REMOTE RESOURCE
        // mWebView.loadUrl("");

        // LOCAL RESOURCE
        mWebView.loadUrl("file:///android_asset/index.html");
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Decaf, 2021-01-18
@Decaf

May help:
How to prevent WebView from opening a browser when clicking on a link?

M
metal_millitia, 2021-01-19
@metal_millitia

Need to redefine Webview client

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question