How to make links in textview clickable - android

How to make links clickable in text view. This is the problem
(1). view.setMovementMethod(LinkMovementMethod.getInstance());
this works only when textview contains link(anchor tag/href) like this:
Go to Google
it does'nt work when textview contains link like this:
http link
(2). While android:autoLink="web"
this works only when textview contains link like this:
http link
it does'nt work when textview contains link like this:
Go to Google
What to do when textview contains both types of link.
Please help.

try this
<TextView
android:id="#+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://www.google.com"
android:gravity="center_vertical"
android:textColor="#color/white"
android:textColorLink="#color/white"
android:linksClickable="true"
android:autoLink="web"
android:textStyle="normal"
android:textSize="15sp" />

Try this
textView.setOnClickListener(new View.OnClickListener());
in xml:
android:clickable="true"
or you can use String
htmltext=Html.fromHtml(htmltext)
then set this htmltext to textview
textview.setText(htmltext)

Try This
String mobile = "12345678";
Set this text with link and click listener
SpannableString mobile_underline = new SpannableString(mobile);
mobile_underline.setSpan(new UnderlineSpan(), 0, mobile.length(), 0);
textviewID.setOnClickListener(new PhoneNumberClickListener());//Implement custom listener to write respective code of click listener

Related

Textview links with and without <a> tag

I am trying to make these links text clickable in TexView.
String s = "google https://google.com";
textview.setText(Html.fromHtml(s));
I could not make both clickable at the same time.
This makes <a> tag clickable
textview.setMovementMethod(LinkMovementMethod.getInstance());
This makes web link clickable
textview.setAutoLinkMask(Linkify.ALL);
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/txtCredits"
android:autoLink="web" android:id="#+id/infoTxtCredits"
android:layout_centerInParent="true"
android:linksClickable="true"></TextView>
<string name="txtCredits">Google</string>
you have to add android:autoLink="web" in our application

Creating a hyperlink within a textview inside a fragment

I have looked through many of the threads on this site to find the solution to my problem but none seem to work. At the moment I am able to create a hyperlink while displaying the website eg - Please go here - www.google.com (The www.google.com is hyperlinked). What I am trying to do is have the link as - Please go here (Here is the link). Below is the code i have tried but once I remove the link itself, the 'here' still highlights like a link but has no function.
Strings.xml:
<string name="goog">Please go here</string>
fragment_home.xml:
<TextView
android:id="#+id/npd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:autoLink="web"
android:fontFamily="sans-serif-light"
android:linksClickable="true"
android:text="#string/npd"
android:textColorLink="#FF0000"
android:textSize="12dp" />
If i change 'here' for www.google.com within Strings.xml, the link takes me directly there. Has anyone been able to figure this problem out yet? Thanks in advance.
Try this and let me know is it what you are looking for
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

onclicklistener textView in android

I have one TextView with two different colors. Here if I click mini it should redirect to one activity, if I click metro redirect to another activity. How can I achieve this?
TextView t = (TextView)v.findViewById(R.id.text);
String text = "<font color=#000000><b>"+"mini"+"</b></font><font color=#000000> added </font><font color=#1569C7>"+"Metro"+"</font><font color=#000000> as a favourite.</font>";
t.setText(Html.fromHtml(text));
What you can do is create an custom class that extends ClickableSpan and manage your clickable text. I had answered the same here.
On your click listener of textview you have to find its text and you can use getText property just compare the text and opens activity as per the text.
I think you can use two textview in linear layout with two different colours and text. Then you can implement onClicklistener() for two text view. You will achieve what you want and it looks same like what you expecting.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/settings"
android:layout_marginTop = "10dip"
android:padding="3dip"
android:text="#string/txt1"
android:textColor="#000000"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/settings"
android:layout_marginTop = "10dip"
android:padding="3dip"
android:text="#string/txt2"
android:textColor="#1569C7"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
Hope it helps
In an onclick listener I'd try this:
if (text.equalsIgnoreCase("mini")) {
// call your next activity here
} else if(text.equalsIgnoreCase("metro")) {
// call your next activity here
}
Don't forget to set android:clickable="true" in your XML file of the TextView.
This answers what you want, though keep in mind that it's very hard to get the usability of such a component right. It's very likely that someone somewhere will have trouble clicking the links - especially if the text is on multiple lines and the linked words are close to each other vertically.
I would advice against such a solution.

Autolink inside a TextView in android

How to give autolink for some part of textview ? For example : My text inside TextView is "Please click here to open this webpage". I want to show link for only the text "here". And I should open that webpage onclick of the text "here" but not on the anywhere of TextView.
Put a String in string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="txtCredits">Support: click here</string>
</resources>
And you can use it in textView like this:
<TextView
android:layout_width="fill_parent"
android:id="#+id/text"
android:layout_height="wrap_content"
android:gravity="center"
android:linksClickable="true"
android:text="#string/txtCredits" />
EDIT
For some reason above code does not work properly. So, add below code also,
TextView t2 = (TextView) findViewById(R.id.text);
t2.setMovementMethod(LinkMovementMethod.getInstance());
Important: Don't forget to remove autoLink="web" if you are calling setMovementMethod().
Textviews are capable of displaying HTML, which solves your problem. Wrap what you want clickable in a hyperlink:
String html = "My link is here";
myTextView.setText(Html.fromHtml(html));
use simple Url in strings.xml :
<string name="autolink_val">Please Click Here : http://www.google.com</string>
And in Java code write this:
<TextView android:id="#+id/linkVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="#string/autolink_val1"/>`
Use HTML syntax in strings.xml:
<string name="test">Click <a href="http://vtuhtan.info">here</a></string>
Set TextView properties to have links clickable and auto link.
TextView tv = findViewById(R.id.textView);
tv.setText(Html.fromHtml(getResources().getString(R.string.test)));
You can test it with following code:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://www.yahoo.com"
android:autoLink="web"
/>

Android active link of url in TextView

I have getting dynamic text from a web service and showing the same in a TextView. Sometimes the TextView has url like hello. I have set the text using the following code.
textView.setText(Html.fromHtml(sampletext));
And also set android:autoLink="web" in the corresponding xml of that contains the TextView. Now the link is showing properly with blue color and underline, but I found the its just a dead link. Nothing is happening if we try to click it. What I have to do to make the link active?
After revisiting all solutions, a summary with some explanations:
android:autoLink="web"
will find an URL and create a link even if android:linksClickable is not set, links are by default clickable. You don't have to keep the URL alone, even in the middle of a text it will be detected and clickable.
<TextView
android:text="My web site: www.stackoverflow.com"
android:id="#+id/TextView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:autoLink="web">
</TextView>
To set a link via the code, same principle, no need for pattern or android:autoLink in layout, the link is found automatically using Linkify:
final TextView myClickableUrl = (TextView) findViewById(R.id.myClickableUrlTextView);
myClickableUrl.setText("Click my web site: www.stackoverflow.com");
Linkify.addLinks(myClickableUrl, Linkify.WEB_URLS);
This works for me:
<TextView
android:text="www.hello.com"
android:id="#+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:autoLink="web">
</TextView>
To Save any one time the true solution is
<TextView
android:text="www.hello.com"
android:id="#+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:autoLink="web"
android:linksClickable="true">
</TextView>
and i use it and it work perfect
There are 2 cases:
the text looks like "click on http://www.hello.com"
then you just have to set the autoLink attribute in the xml so that the link is automatically detected in the text:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="click on http://www.hello.com"/>
the text looks like click on hello
then you have to do it by code and tell the text is html, and specify a Link movement method for the click:
String text = "click on hello";
TextView textView = view.findViewById(R.id.textView);
textView.setText(Html.fromHtml(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());
Check out this approach:
String text = "Visit stackoverflow.com";
TextView label = new TextView(this);
label.setText(text);
Pattern pattern = Pattern.compile("stackoverflow.com");
Linkify.addLinks(label, pattern, "http://");
i have give some idea which i have found it
TextView tv = ( TextView ) findViewById( R.id.link );
WebView wv = ( WebView ) findViewById( R.id.webView );
URLSpan[] urlSpans = tv.getUrls();
for ( URLSpan urlSpan : urlSpans )
{
wv.loadUrl( urlSpan.getURL() );
}
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Hello, Android</string>
<string name="link">'Google'</string>
</resources>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/link" />
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
</LinearLayout>
In your XML, you need to add android:linksClickable="true" in the TextView.
If you are displaying in textview the string from strings.xml, strings containing the web link should not have word "a href=". If these words are deleted from the strings.xml file then the link will work.
Add these lines of code to your textView in xml File it will work perfectly fine..
android:autoLink="web"
android:textColorLink="#android:color/holo_orange_dark"
android:linksClickable="true"
or if want a your own link in textview add these lines of code in your java file
final SpannableString s = new SpannableString("https://play.google.com/store/apps/details?id=cn.wps.moffice_eng");
Linkify.addLinks(s, Linkify.ALL);
set this 's' String in your TextView by function
Textview.setText(s);
and don't forget to add this line
((TextView)findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
id will be your textview id
Enjoy ...
Answer is right, BUT not complete, because I had in my xml some properties from other answers like autoLink and linksClickable and programatic way did not work. Also when I passes string with html from string resource also it did not work, so beware, you have to clean your xml and pass string directly exactly as in that answer.
String text = "click on hello";
TextView textView = view.findViewById(R.id.textView);
textView.setText(Html.fromHtml(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());
I did not try it without without LinkMovementMethod but now I am ok as it finally work. Other answers did not work for me or was for visible url text, not clickable text as link.

Categories

Resources