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.
Related
I have textview with clickable email and link. My text has link and email
my xml is:
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColorLink="#color/colorPrimeTopBar"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:text="#string/sms_deposit_information"
android:textColor="#color/sms_deposit_text"
android:textSize="16sp"
android:autoLink="email"
app:layout_constraintLeft_toLeftOf="#id/smsNumberField"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/dividerBottom" />
But I have a problem. When I put android:autoLink="email" it shows email and makes it clickable and not link. If I remove it shows link clickable and not email.
What should I do?
Can you try adding attributes as:
android:autoLink="email|web"
android:linksClickable="true"
I think you need to set the android:linksClickable="true" attribute.
If set to false, keeps the movement method from being set to the link movement method even if autoLink causes links to be found.
Set the sms_deposit_information in strings.xml in the form of
<string name="sms_deposit_information"><![CDATA[<p>abc#xyz.com</p>]]></string>
Then do the below programmatically
TextView textView = findViewById(R.id.description);
textView.setText(Html.fromHtml(getString(R.string.sms_deposit_information)));
textView.setMovementMethod(LinkMovementMethod.getInstance());
put a String in xml
<resources>
<string name="test">Support: click here</string>
</resources>
TextView
<TextView
android:layout_width="fill_parent"
android:id="#+id/text"
android:layout_height="wrap_content"
android:autoLink="web"
android:gravity="center"
android:linksClickable="true"
android:text="#string/test" />
EDIT
add this code too:
TextView t2 = (TextView) findViewById(R.id.text);
t2.setMovementMethod(LinkMovementMethod.getInstance());
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)
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 take a string from my ressource strings.xml and put the string in a TextView.
In strings.xml :
<string name="description">Hello please look into www.webtour.dk</string>
In main.xml :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/description"
android:textSize="12dp"
android:textStyle="bold" />
I got this result good result,www.webtour.dk appear as a link. But when i click on the link i got nothing!!! no navigator is launched??
Simply use Linkify to look for web sites automatically:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="#string/description"
android:textSize="12dp"
android:textStyle="bold" />
Then take the html out of your text:
<string name="description">Hello please look into www.webtour.dk</string>
I am using 3 italic textviews with different colors
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:id="#+id/submittedBy" android:paddingTop="10dip">
<ImageView android:id="#+id/subByImg"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="left" android:layout_gravity="bottom" android:src="#drawable/submitted_by_arrow"/>
<TextView android:id="#+id/submitLabel"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="left" android:text="Submitted by" android:textStyle="italic"
android:textSize="12sp" android:textColor="#color/gray" android:paddingLeft="5dip"/>
<TextView android:id="#+id/submitName" android:textStyle="italic"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="12sp" android:textColor="#color/maroon_dark" android:paddingLeft="10dip"/>
<TextView android:id="#+id/submitByDate" android:textStyle="italic"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="left"
android:textSize="12sp" android:textColor="#color/gray" android:paddingLeft="10dip"/>
</LinearLayout>
I wonder every last character is not displaying properly specially name displayed in the middle is "Dan Buckland" and it it is missing last character looks like "Dan Bucklano"
Also tell me pls how can have textview italic and bold both..
alt text http://www.freeimagehosting.net/uploads/953d573113.jpg
I had the exact same problem. I got around it by simply adding a space to the end of any string that needs to be in italics.
It may not be the most long-term-correct solution but it worked for me.
Seems like the bounding box is not correctly calculated when using italic.
Have you tried to use paddingLeft=6 and paddingRight=6 for the elements?
(less chance of overlap).
For multiple styles in a TextView see Is it possible to have multiple styles inside a TextView?
You can use with the text in string.xml file.