Open webview in app when clicking on Textview

I used android:autolink="web"

in textview

to display text as links when it's a link. It is currently working fine and when you click on this link, it opens on the default browser for phones. I currently have a Webview in my activity. I need to open a url in my webview when I clicked on it. is there a way to achieve this?

+3


source to share


1 answer


You need to create a class that extends WebViewClient and overrides the toOverrideUrlLoading method, for example:

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

      

then you have to set the WebView client to the object of the created class:

    webView.setWebViewClient(new MyBrowser());

      

To view the full text of the page, you need to add the following properties:

    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

      

To open your URL in a WebView, you need to add properties to the TextView, for example:

    android:onClick="onUrlClick"
    android:linksClickable="false"
    android:clickable="true"

      

and then override the onClick method in the TextView like:

 public void onUrlClick(final View view) {
    TextView textView = (TextView)view;
    String sUrl = String.valueOf(textView.getText());
    webView.loadUrl(sUrl);
}

      



You may need to add an internet prefect

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

      

This answer was partially derived from here

Full activity code:

public class MyActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new MyBrowser());
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
}


public void onUrlClick(final View view) {
    TextView textView = (TextView)view;
    String sUrl = String.valueOf(textView.getText());
    webView.loadUrl(sUrl);

}

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

      

}

Full layout code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">

<TextView
    android:text="http://www.google.com"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:onClick="onUrlClick"
    android:linksClickable="false"
    android:clickable="true"
    />
<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView">

    </WebView>

      

+1


source







All Articles