Renjith Krishnan
Renjith Krishnan

Reputation: 2636

Open in app webview when click on Textview

I used the android:autolink="web" in textview for showing the text as link when it is a link. Currently it is working fine and when clicked on that link it is open on phones default browser. Currently i have a Webview in my activity., I need to open url in My webview when i clicked on it . is there any way to achieve that?

Upvotes: 3

Views: 2515

Answers (1)

Androj
Androj

Reputation: 34

You need to create class which extends WebViewClient and override shouldOverrideUrlLoading method like:

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

then you should set WebView client on object of the created class:

    webView.setWebViewClient(new MyBrowser());

To see the full content of the page you need to add this properties:

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

To open your Url in the WebView you need to add in TextView properties like:

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

and then override onClick method in 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 Internet premission

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

This answer was partially took from here

The full code of activity:

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;
    }
}

}

The full code of layout:

<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>

Upvotes: 1

Related Questions