Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Activity Intent on Clicking Text Inside Webview

I have a webview in my xml which goes like below:

 <WebView
        android:id="@+id/webView"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

I am loading the webview like this:

String webView_text = "Lorem ipsum..............**<a><u>Link to fire intent</u></a>**";

 WebView webView= (WebView) findViewById(R.id.webView);
webView.loadData(String.format(htmlText, webView_text), "text/html", "utf-8");
        webView.setWebViewClient(new WebViewClient()
        {
            // Override URL
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                Intent intent = new Intent(getApplicationContext(),OtherActivity.class);
                startActivity(intent);
                return true;
            }
        });

Please notice that I am creating the link in my string (webView_text) by using the html tag and overriding the function to fire an intent. It is not doing in this case. What is the problem here? I am not sure if Android Webview supports the tag (I believe it should). What is my mistake here.Thanks in advance.

like image 811
uLYsseus Avatar asked Feb 14 '23 07:02

uLYsseus


2 Answers

you can this by define scheme in activity intent filter in manifest. for sample create activity (A) and activity (B) and define in manifest like this :

<activity android:name="A" >
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="activity_a" />
    </intent-filter>
</activity>
<activity android:name="B" >
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="activity_b" />
    </intent-filter>
</activity>

if in your html have linke like this:

<a href="activity_b://b">Activity B</a>

when you click it , start activity B. Activity A is similar to it.

you can get source code from Source Code

NOTE : if using webview for this method you must override the method shouldOverrideUrlLoading() and compare the every url.

like image 125
Golil Avatar answered Feb 24 '23 04:02

Golil


We can use webview same as textview also.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webView=(WebView)findViewById(R.id.link2);

        /*TextView t3 = (TextView) findViewById(R.id.link);

        t3.setText(
                Html.fromHtml(
                    "<b>text3:</b>  Text with a " +
                    "<a href=\"activity_a://a\">Activity A</a> : " +
                    "<a href=\"activity_b://b\">Activity B</a> "));
        t3.setMovementMethod(LinkMovementMethod.getInstance());*/

        String data= "<html><head></head>"+
                "<b>text3:</b>  Text with a " +
                "<a href=\"activity_a://a\">Activity A</a> : " +
                "<a href=\"activity_b://b\">Activity B</a></html> ";

        webView.loadData(data, "text/html", "utf-8");

        webView.setWebViewClient(new WebViewClient()
        {
            // Override URL
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                if(url.equalsIgnoreCase("activity_a://a")){
                    Intent intent=new Intent(getApplicationContext(),A.class);
                    startActivity(intent);
                }
                else if(url.equalsIgnoreCase("activity_b://b")){
                    Intent intent=new Intent(getApplicationContext(),B.class);
                    startActivity(intent);
                }
                Log.e("URL","URL "+url);
                return true;
            }
        });

    }

I hope this may help others.Thanks!

like image 22
Jagdish Avatar answered Feb 24 '23 03:02

Jagdish