Reputation:
I'm trying to create clickable textviews in Android without success, in layout android I've the correct output but not linkable.
Here's what I have right now:
<TextView
android:id="@+id/textTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:clickable="true"
android:textSize="16sp"
android:textStyle="bold" />
textTitle.setText(Html.fromHtml(response.toString()));
textTitle.setMovementMethod(LinkMovementMethod.getInstance());
In
Log.i("myApp1", response.toString());
I've
<a href=http://...>MyLink</a>
Upvotes: 0
Views: 502
Reputation: 29285
You just should register a listener to this TextView
findViewbyId(R.id.textTitle).setOnClickListener(new OnClickListener{
@Override
protected void onClick(View view){
String link = (TextView)view.getText().toString();
/* redirect to URL here for example: */
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivity(intent);
}
});
Or:
Add android:autoLink="all"
property to your TextView
and set its text a HTML A
element. for example:
<TextView
android:text="@string/mylink"
android:autoLink="all"/>
And in strings.xml
:
<string name="mylink"><a href='http://www.google.com'>http://www.google.com</a></string>
Upvotes: 1
Reputation: 532
XML view is absolutely correct, make sure you have pragmatically handled the onClickListener properly.
Upvotes: 0