<TextView
android:id="#+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Forget password?"
android:autoLink="all"
android:linksClickable="true"
android:textColor="#color/lgreen"
android:textStyle="italic"
/>
Android is highlighting the links in the TextView, but they do not respond to clicks. Can someone tell me how can i do that as a clickable and move to another link.
i tried more times by seeing examples. but i can't. Can some one explain me clearly how to change text view as a clickable link.
All tested and working 100%
below is a complete example
Solution: android:autoLink="web"
Sample Xml:
<TextView
android:id="#+id/txtLostpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="email"
android:gravity="center"
android:padding="20px"
android:text="#string/lostpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/txtLostpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="web"
android:gravity="center"
android:padding="20px"
android:text="#string/defaultpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
String in string.xml
<string name="lostpassword">If you lost your password please contact support#cleverfinger.com.au</string>
<string name="defaultpassword">User Guide http://www.cleverfinger.com.au/user-guide/</string>
You can add click listener to TextView and start websearch:
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
Intent browser= new Intent(Intent.ACTION_VIEW, Uri.parse(PASTE_YOUR_URL_HERE));
startActivity(browser);
}
});
and xml looks like:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/link_string"
android:textSize="14dp"
android:autoLink="web" />
In the string file put this code
<string name="link">'<a href="http://www.www.com" >ForGet Password</a>'</string>
and in the XML file
android:text="#string/link"
this is the most easy and understandable code
TextView link = (TextView) findViewById(R.id.hyper);
String linkText = "This is my website <a href='http://programondaspot.blogspot.com'>Programondaspot</a> .";
link.setText(Html.fromHtml(linkText));
link.setMovementMethod(LinkMovementMethod.getInstance());
Check out this post!
HAPPY CODING!
In your onCreate() method put this code:
TextView clickableTextLink = (TextView)findViewById(R.id.your_textview_id); clickableTextLink.setMovementMethod(LinkMovementMethod.getInstance());
I tried a number of solutions. But this worked perfectly for me.
this is the EDITED answer
Hi you can try by replacing this in you layout.xml file
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="visit the site at www.google.com from here"
android:autoLink="all"
android:clickable="true"
android:linksClickable="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
I have tried it and it will surely work. and treats "www.google.com" as web link
Just add onClick tag to your TextView which equals the defined function. Just as below
<TextView
android:id="#+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Forget password?"
android:autoLink="all"
android:linksClickable="true"
android:textColor="#color/lgreen"
android:textStyle="italic"
android:onClick="callFunction"/>
Here is simple implementation tested up to Android N
String termsText = "By registering, you are agree to our";
String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
String allText = termsText + termsLink + privacyLink;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
} else {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
}
Related
Here I used clickable URL on Textview and it works. But how can I set clickable and highlighted Text with Open URL with a browser. It's possible from Android XML OR kotlin without using java code like setText(Html.fromHtml("")).
String value = "<html>Visit Web mysite View</html>";
TextView text = (TextView) findViewById(R.id.text);
text.setText(Html.fromHtml(value));
text.setMovementMethod(LinkMovementMethod.getInstance());
<TextView
android:textSize="18sp"
android:autoLink="web"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="http://www.example.com"
tools:ignore="HardcodedText" />
It's No required declear layout id and no code from java side. it's works from xml
Use Autolink
For website android:autoLink="web"
For call android:autoLink="phone"
For email android:autoLink="email"
For map android:autoLink="web"
For all android:autoLink="all"
This property works for you
TextView: android:autoLink="web"
here is an example Layout
layout.xml
<TextView
android:id="#+id/txtLostpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="email"
android:gravity="center"
android:padding="20px"
android:text="#string/lostpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/txtDefaultpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="web"
android:gravity="center"
android:padding="20px"
android:text="#string/defaultpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
string.xml
<string name="lostpassword">If you lost your password please contact support#cleverfinger.com.au</string>
<string name="defaultpassword">User Guide http://www.cleverfinger.com.au/user-guide/</string>
Just add this attribute to your TextView android:autoLink="web"
e.g
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:autoLink="web"
android:clickable="true"
android:text="https://www.google.co.in"/>
Add line to the TextView:
`android:autoLink="web"`
Like this
<TextView
................
................
android:autoLink="email"
.................
/>
as #Aakash Verma suggested use android:autoLink="web".
but it will change your textColor. To over-come this also add android:textColorLink="your_text_color" to you TextView.
Your TextView should look like.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="https://www.google.co.in/"
android:textColor="#color/colorPrimary"
android:textColorLink="#color/colorPrimary" />
If you still want to do in in java
myUrl= (TextView) rootView.findViewById(R.id.myUrl);
myUrl.setText("https://www.google.co.in/");
Linkify.addLinks(myUrl, Linkify.WEB_URLS);
myUrl.setLinkTextColor(Color.parseColor("#000000"));
EDIT:
Happy Coding
Use android:autoLink="web" in your TextView's xml. It should automatically convert urls click-able (if found in text)
Also, don't forget android:linksClickable="true". I don't think that you need to set html tag like you did, just the <a href> tag...
In case you are using text from strings.xml resource, it seems that the html tags gets stripped out.
So you have to use <![CDATA[**your string with click links**]]> in the strings.xml to convert it to html using Html.fromHtml(string)
I am testing this little code snippet on a tablet. So i am not sure if it is not working because of that. Nothing happens when i click the number. No error message or log shown in LogCat. Please look at my implementation below.:
MainActivity.java
public void callPolice(View view){
TextView tv = (TextView) findViewById(R.id.policeno);
String val = tv.getText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+val));
startActivity(intent);
Log.v("Calling", "Calling..... "+val);
}
activity_main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="999"
android:textColor="#000000"
android:textSize="13pt"
android:textStyle="bold"
android:id="#+id/policeno"
android:layout_alignTop="#+id/textView2"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:onClick="callPolice"/>
AndroidManifest.xml
<uses-permission android:name = "andriod.permission.CALL_PHONE" />
Just put android:clickable="true" in textview.
<TextView
android:id="#+id/policeno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="87dp"
android:onClick="callPolice"
android:text="999"
android:textColor="#000000"
android:textSize="13pt"
android:clickable="true"
android:textStyle="bold" />
if you want to call you should use
Intent.setAction(Intent.ACTION_CALL);
If you are using fragments the xml onclick may not work. So try implementing onclick listner. Put the log before starting activity.
My String is somthing conbination of url string and url within href like:
" Go http://www.google.co.in for more detail click here"
No i need to show clickable both:
1- http:www.google.co.in
2- here
and clicking on both the link it should show the respective url.
Use android:autoLink="web" in your XML file.
Or
Just use an HTML format link in your resource (Strings.xml):
<string name="my_link"><a ref="http://somesite.com/">Click me!</a></string>
You can then use setMovementMethod(LinkMovementMethod.getInstance()) on your TextView to make the link clickable.
You can try:
// text2 has links specified by putting <a> tags in the string
// resource. By default these links will appear but not
// respond to user input. To make them active, you need to
// call setMovementMethod() on the TextView object.
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
Another Way:
https://stackoverflow.com/a/17544212/1318946
Edited:
Its not perfectly right solution but you can solve your problem using this one.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Go"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/textView1"
android:text="Google.com"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0000ff" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="5dp"
android:text="For more Details"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/textView3"
android:text="Click here"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0000ff" />
Output is looks like:
Now you have to define onClickListener for textView2 and textView4 and you can do want you want. :)
Happy Coding.
I have a few links in my application. One for a website, one for a phone number, and one for an email. The email and phone links are both working and clickable, however the website hyperlink is still not clickable for some reason. Any thoughts? Code below.
<string name="website" >XXXXXX Website</string>
<string name="email" >sales#XXXXXXX.com</string>
<string name="phone" >P: XXX.XXX.XXXX</string>
<string name="fax" >F: XXX.XXX.XXXX</string>
Above are my strings, and below is the xml file that displays them:
<TextView android:id="#+id/website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/imageButtonTwitter"
android:gravity="center_horizontal"
android:padding="10dp"
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
android:text="#string/website" />
<TextView android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/website"
android:gravity="center_horizontal"
android:padding="10dp"
android:autoLink="email"
android:linksClickable="true"
android:text="#string/email" />
<TextView android:id="#+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/email"
android:gravity="center_horizontal"
android:padding="10dp"
android:autoLink="phone"
android:linksClickable="true"
android:text="#string/phone" />
<TextView android:id="#+id/fax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/phone"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="#string/fax" />
Like I said.. the others are clickable and working. I've tested it on two emulators, as well as my Galaxy S4. Any thoughts why the website is not clickable?
You need to call this on your textview:
TextView tv = (TextView) findViewById(R.id. website);
tv.setMovementMethod(LinkMovementMethod.getInstance());
You need to remove from your TextView:
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
Add this line -
htmlContent.setMovementMethod(LinkMovementMethod.getInstance());
I'm not very experienced android programmer, but my small remarks which can be helpful (at least I hope):
Don't add those additional parameters to TextView. Just android:text and android:clickable="true" is enough for having html clickable link.
And next when setting text please use:
TextView myTextView = (TextView) findViewById(R.id.myId);
myTextView.setText(Html.fromHtml(htmlText));
More here: android.text.Html
I'm guessing you are missing that Html.fromHtml
I hope it helps.
I have a TextView in which I must load a message. The TextView has maximum 2 lines (android:lines="2"). The message may contain an '\n' character.
How should I load the message in that TextView so that words would be wrapped and if in those 2 lines message does not fit, at the end of last visible word I must add three dots (...)? How can I detect the length of text that fits in that TextView?
My code for TextView is
<TextView
a:id="#+id/tv_message"
a:gravity="top"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_alignParentTop="true"
a:layout_toRightOf="#id/iv_icon"
a:layout_marginLeft="2dp"
a:layout_marginTop="4dp"
a:paddingRight="7dp"
a:paddingBottom="5dp"
a:textSize="12sp"
a:typeface="sans"
a:ellipsize="marquee"
a:lines="2"
a:maxLines="2"
a:textColor="#android:color/black"
/>
But in application, text appears on two lines, even if there is a line containing the signature. The message is like: "message text" + "\nSignature" but the message can be on 1,2,3 lines, depending of message length.
In XML, use the property android:ellipsize="marquee"for the TextView.
Your TextView can be defined like:
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:lines="2"
android:ellipsize="marquee"/>
Edit: I've tried the following code, adapted from yours:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlt_news"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/cell_background"
android:padding="5dip" >
<TextView
android:id="#+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="5dip"
android:text="Name"
android:textStyle="bold" />
<ImageView
android:id="#+id/rss_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_title"
android:layout_marginLeft="2dip"
android:adjustViewBounds="true"
android:maxHeight="100dip"
android:maxWidth="100dip"
android:src="#drawable/rss_cell_icon"
android:visibility="visible" />
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="#id/rss_icon"
android:ellipsize="marquee"
android:gravity="top"
android:lines="2"
android:maxLines="2"
android:paddingBottom="5dp"
android:paddingRight="7dp"
android:text="Description of the news, just the short paragraph, \nSignature, to see how it works if there are more than two lines"
android:textColor="#android:color/black"
android:textSize="12sp"
android:typeface="sans" />
</RelativeLayout>
And here is the output:
Use android:ellipsize="end" instead of "marquee" that did it for me.
Gabi,
I was also having the same problem. To enable ellipsize on a textview with 2 lines I use the following parameters:
android:maxLines="2" // You already got this one
android:ellipsize="end" // also this one
android:singleLine="false" // This is the command that solves your problem.
Unfortunately I got a new scenario for my app. I have to use 3 lines with ellipsize and this configuration only works for 2 lines... :( Good luck
If you set text from code, than you must set options once again!
textView.setText(content);
textView.setEllipsize(TruncateAt.END);
textView.setLines(5);
textView.setMaxLines(5);
It works for me when I set it in code not in the XML layout
holder.txtDescription.setText(text);
holder.txtDescription.setMaxLines(3);
holder.txtDescription.setEllipsize(TruncateAt.END);
String s = "line1 \nline2 \nline3";
String [] lines = s.split("\n");
String twolines;
if (lines.length > 2) {
twolines = lines[0] + "\n" + lines[1] + "...";
} else {
twolines = s;
}
This should give you the desired result, just use
((TextView)findViewById(R.id.text)).setText(twolines);
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform" />
android:autoSizeTextType="uniform" did the trick for me. Text size reduces to fit available space but complete text is displayed.
Refer this link
https://developer.android.com/develop/ui/views/text-and-emoji/autosizing-textview