Start Activity Intent on Clicking Text Inside Webview

18,275

Solution 1

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.

Solution 2

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!

Share:
18,275
uLYsseus
Author by

uLYsseus

Like interacting and asking questions.....

Updated on June 04, 2022

Comments

  • uLYsseus
    uLYsseus almost 2 years

    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.

  • uLYsseus
    uLYsseus over 10 years
    Concrete answer! Thanks a hell lot bro ! Can you explain me why I went wrong /
  • Jagdish
    Jagdish about 10 years
    @surbiks, its works fine for text view.Is it possible to do the same thing in webview.
  • Golil
    Golil about 10 years
    @jagdish, yes.you can do it for webview and all link.
  • Jagdish
    Jagdish about 10 years
    Thanks @surbiks, I tried but it open new tab in browser.but it work fine if i override the method shouldOverrideUrlLoading() and compare the every url.
  • Bartek Kosa
    Bartek Kosa about 10 years
    I'm trying to implement this solution in my project but when I click on link I get "Web page not available" message. I added intent-filter to the activity which I want to open in manifest and I added link to activity in my webview. Do I have to do something else or am I doing something wrong?