0

I want to put a clickable URL in my activity so when the user click that url the device browser triggers and the url opens normally. Any help about that please....

Mero_vic280
  • 207
  • 1
  • 3
  • 10

2 Answers2

0

Simplest way is to use Linkify

TextView textView = (TextView) findViewById(R.id.yourTextView);
textView.setText(someContent);          
Linkify.addLinks(textView, Linkify.ALL);

detailed example

Android Linkify api doc

error in findViewById I don't know why

Id of view is defined in yr XML

<TextView android:id="@+id/yourTextView"
android:layout_width="wrap_content"       
android:layout_height="wrap_content"/>

cannot resolve method 'findViewById(int)' it is a fragment from a navigation drawer not an activity maybe the error in that point

In fragment use:

getView().findViewById(id) ;

or in case u need activity reference:

getActivity(). 
ceph3us
  • 7,326
  • 3
  • 36
  • 43
-1

Create a textView with id "clickableUrl", with the text of your choice (in exemple "Click here to open in browser")

Then in the onCreate method of your Activity, put this code :

TextView clickableText = (TextView) findViewById(R.id.clickableUrl)
clickableText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               String url = "http://www.google.com";
               Intent i = new Intent(Intent.ACTION_VIEW);
               i.setData(Uri.parse(url));
               startActivity(i);
        }
    });

It should start the google.com webpage on the device browser when you click on the TextViex.

Please ask me if you need any help.

victorleduc
  • 232
  • 2
  • 9