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());
Related
I have a TextView that shows all inserted text except for the links, but the place where they should be is still clickable and takes me to the desired destination. What would be making the link itself invisible?
From string resource
<string name="credits">Video from Youtube</string>
From layout
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:fontFamily="#font/abril_fatface"
android:lineSpacingExtra="12sp"
android:paddingLeft="16dp"
android:paddingTop="75dp"
android:paddingRight="16dp"
android:text="#string/credits"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp" />
From activity
textCredits.movementMethod = LinkMovementMethod.getInstance()
The cause might be the equality of text color and background. Try to set another color to the links using:
android:textColorLink="#color/someColor"
Use a color like this this
android:textColorLink="#fff"
And
android:autoLink="true"
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)
How to make single line TextView appears if the screen small?, Because it's makes 3 dots of ellipsize without defining it in XML and the rest of text disappeared.
Example:
Please add:
android:singleLine="false"
OR
android:inputType="textMultiLine"
to your TextView
EDIT:
Your TextView can look something like this:
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_text"
android:maxLines = "1"
android:scrollbars = "vertical"/>
and add these two lines to your onCreate
TextView yourTextView = (TextView) findViewById(R.id.test);
yourTextView.setMovementMethod(new ScrollingMovementMethod());
Not too clean but does the job.
SECOND EDIT:
<TextView
android:text="#string/yourText"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever" <!-- Please change according to requirement -->
android:scrollHorizontally="true"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"/>
Hope this helps!
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 want to linkify a email address and put a static subject to the email.
In the layout I have this:
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:autoLink="email"
android:text="#string/email" />
How can I add the subject to the email? I should do this in the Activity with the addLinks() method, am I right? I tried some combinations but the email was being appended to the subject...
I checked those sources but without luck:
Email from a link and Android Intents - bring it home
Android Text Links Using Linkify
Linkify your Text!
I found a solution:
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:clickable="true"/>
TextView mailTV = (TextView) findViewById(R.id.textView5);
mailTV.setText(Html.fromHtml("<a href=\"mailto:"+getString(R.string.email)+"?subject="+getString(R.string.email_subject)+"\" >"+getString(R.string.email)+"</a>"));
mailTV.setMovementMethod(LinkMovementMethod.getInstance());