简体   繁体   中英

Android Textview with clickable email and link

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?

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: <a href="http://www.stackoverflow.com">click here</a></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());

Can you try adding attributes as:

android:autoLink="email|web"
android:linksClickable="true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM