Reputation: 3600
I have a text view that is displaying some HTML and it displays correctly. If the HTML contains a link with just the URL text I can tap on the link to open it, but if the URL is attributed to other text, like this:
<a href="https://cdn.kyfb.com/KYFB/assets/File/Federation/Across%20Kentucky/2017/January/AK_Promo_Jan_23_2017_mixdown.mp3">Listen here</a></strong></p>
"Listen here" looks like a clickable link in my text view but it can't be selected.
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
import android.text.method.LinkMovementMethod;
public class BenefitDetailActivity extends BaseActivity {
static Context context;
TextView detailTextView;
String descriptionText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_benefit_details);
context = getApplicationContext();
Intent detailIntent = getIntent();
descriptionText = detailIntent.getStringExtra("description");
detailTextView = (TextView)findViewById(R.id.benefitText);
Typeface kfb = Typeface.createFromAsset(getAssets(), "FranklinGothicStd-ExtraCond.otf");
detailTextView.setTypeface(kfb);
detailTextView.setTextSize(20);
System.out.println("DESCRIPTION: " + descriptionText);
detailTextView.setText(Html.fromHtml(descriptionText));
}
}
Layout:
<TextView
android:id="@+id/benefitText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="@color/transparent"
android:textColor="@color/white_text"
android:autoLink="all" />
Based on the suggestions of others, I've added
detailTextView.setMovementMethod (LinkMovementMethod.getInstance());
and
android:linksClickable="true"
With that, the problem is that when I add setMovementMethod(), the linked text (Listen here) is no longer displayed as a link and still can't be selected.
Upvotes: 1
Views: 908
Reputation: 3600
I fixed this by adding detailTextView.setMovementMethod(LinkMovementMethod.getInstance());
and android:linksClickable="true"
. I also had to remove android:autoLink="all"
Upvotes: 1
Reputation: 218
add following attribute to textview in layout file
<TextView
android:id="@+id/benefitText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="@color/transparent"
android:textColor="@color/white_text"
android:autoLink="all"
android:linksClickable="true"
/>
and the add following line to on setMovementMethod.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
detailTextView.setText(Html.fromHtml(descriptionText,Html.FROM_HTML_MODE_LEGACY));
} else {
detailTextView.setText(Html.fromHtml(descriptionText));
}
detailTextView.setMovementMethod(LinkMovementMethod.getInstance());
Upvotes: 2