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....
Asked
Active
Viewed 44 times
0
-
Do you want to open it in a browser or in a web view? – Anis LOUNIS aka AnixPasBesoin Jul 31 '15 at 23:34
-
1possible duplicate of [How do I make links in a TextView clickable?](http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable) – Ye Myat Thu Jul 31 '15 at 23:35
-
No i dont want to use the webview if it is possible – Mero_vic280 Jul 31 '15 at 23:37
2 Answers
0
Simplest way is to use Linkify
TextView textView = (TextView) findViewById(R.id.yourTextView);
textView.setText(someContent);
Linkify.addLinks(textView, Linkify.ALL);
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
-
-
What error? U need define your text view Id in XML or create new via code – ceph3us Aug 01 '15 at 01:08
-
I did that and no error at all with the id but there is an error in "findViewById" itself there is a red line under it – Mero_vic280 Aug 01 '15 at 01:11
-
-
cannot resolve method 'findViewById(int)' it is a fragment from a navigation drawer not an activity maybe the error in that point – Mero_vic280 Aug 01 '15 at 01:16
-
-
thank you very much I finally did it only add (getActivity) before findViewById and everything solved – Mero_vic280 Aug 01 '15 at 01:29
-
-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