Benny
Benny

Reputation: 879

Cannot open Android Webbrowser using Action_View

I am trying to open the standard android webbrowser by clicking on a textview. I defined the android:autoLink="web" in the textview and then use an onTouchListener to start a browserintent:

// On Touch Listener

chatText.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                      // view.performClick();
                       view.onTouchEvent(event);

                       if (event.getAction() == MotionEvent.ACTION_DOWN){
                           view.performClick();
                           openBrowser(chatText.getText().toString());
                       }
                return false;
            }
        });

 // Start Browser function

 public void openBrowser(String url) {
    Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (webIntent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(webIntent);
    }
}

However, everytime I click a link on my textview I get the error:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Although I do have the flags added to my newly started activity, anyone knows what I do wrong here?

I am calling the activity from a class ChatArrayAdapter which extends an ArrayAdapter, however, I pass the appropriate context to the newly started activity

Upvotes: 0

Views: 125

Answers (1)

An SO User
An SO User

Reputation: 24998

Going over your code, your chat text has some URL in it. To make things easier, Android has Linkify class and the android:autoLink XML attribute. These help to automatically highlight links and perform the standard action when you click on them. All of this is handled out of the box.

Your chatText is probably a TextView. You can use one of the two ways I mentioned like so:

In XML:
In your chatText's XML add the following attribute:

android:autoLink="web"  

In code:

Linkify.addLinks(chatText,Linkify.WEB_URLS);

Upvotes: 1

Related Questions