How to Set the Part of the Text View Is Clickable

How to set the part of the text view is clickable

android.text.style.ClickableSpan can solve your problem.

SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent(MyActivity.this, NextActivity.class));
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);

In XML:

<TextView 
...
android:textColorLink="@drawable/your_selector"
/>

Part of TextView clickable with a link

check below point before testing

  • Check your link starts with http:// or https://
  • Place your String in CDATA tag (like <string name="some_text"><![CDATA[PLACE_YOUR_HTML_STRING_HERE]]></string>)
  • Check you have added internet Permission in your manifest.xml file <uses-permission android:name="android.permission.INTERNET"/>
  • then dynamically html text can be set using below code into textView
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(getString(R.string.html_string), Html.FROM_HTML_MODE_COMPACT));
} else {
textView.setText(Html.fromHtml(getString(R.string.html_string)));
}
Linkify.addLinks(textView, Linkify.ALL);
textView.setMovementMethod(LinkMovementMethod.getInstance());

Make parts of textview clickable (not url)

public class MainActivity extends Activity {
TextView _tv;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_tv = (TextView) findViewById( R.id.textView1 );
String sentence = "this is [part 1 clickable] and [part 2 clickable] and [part 3 clickable]";


_tv.setMovementMethod(LinkMovementMethod.getInstance());
_tv.setText(addClickablePart(sentence), BufferType.SPANNABLE);
}
private SpannableStringBuilder addClickablePart(String str) {
SpannableStringBuilder ssb = new SpannableStringBuilder(str);

int idx1 = str.indexOf("[");
int idx2 = 0;
while (idx1 != -1) {
idx2 = str.indexOf("]", idx1) + 1;

final String clickString = str.substring(idx1, idx2);
ssb.setSpan(new ClickableSpan() {

@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, clickString,
Toast.LENGTH_SHORT).show();
}
}, idx1, idx2, 0);
idx1 = str.indexOf("[", idx2);
}

return ssb;
}
}

Edit

public class MainActivity extends Activity {
TextView _tv;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_tv = (TextView) findViewById( R.id.textView1 );

SpannableString ss = new SpannableString("Android is a Software stack");

ss.setSpan(new MyClickableSpan(), 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//22 to 27 stack is clickable
ss.setSpan(new MyClickableSpan(), 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//0 to 7 Android is clickable

_tv.setText(ss);
_tv.setMovementMethod(LinkMovementMethod.getInstance());
}
class MyClickableSpan extends ClickableSpan{ //clickable span
public void onClick(View textView) {
//do something
Toast.makeText(MainActivity.this, "Clicked",
Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.GREEN);//set text color
ds.setUnderlineText(false); // set to false to remove underline
}
}
}

More info on ClickableSpan http://developer.android.com/reference/android/text/style/ClickableSpan.html

You can also style the spannable string by making it bold , italics or setting font size.

    StyleSpan boldSpan = new StyleSpan( Typeface.ITALIC );
ss.setSpan( boldSpan, 22, 27, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
StyleSpan boldSpan1 = new StyleSpan(Typeface.BOLD);
ss.setSpan(new RelativeSizeSpan(3f), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//set fontsize
ss.setSpan( boldSpan1, 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );

Android: Setting onClickListener to a Part of text in a TextView - Issue

there is a way... after seeing your question i was just googling .. and i found this, i hope it will work...

1.
you can use android.text.style.ClickableSpan link

SpannableString ss = new SpannableString("Hello World");
ClickableSpan span1 = new ClickableSpan() {
@Override
public void onClick(View textView) {
// do some thing
}
};

ClickableSpan span2 = new ClickableSpan() {
@Override
public void onClick(View textView) {
// do another thing
}
};

ss.setSpan(span1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(span2, 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());

another way.. link

 TextView myTextView = new TextView(this);
String myString = "Some text [clickable]";
int i1 = myString.indexOf("[");
int i2 = myString.indexOf("]");
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
myTextView.setText(myString, BufferType.SPANNABLE);
Spannable mySpannable = (Spannable)myTextView.getText();
ClickableSpan myClickableSpan = new ClickableSpan()
{
@Override
public void onClick(View widget) { /* do something */ }
};
mySpannable.setSpan(myClickableSpan, i1, i2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

answer just copied from those link...

How handle 2 link click in textview use string resources?

I have used as below mentioned in my apps.

string.xml

<string name="about_fragment_privacy_policy" translatable="false">User Agreement and Privacy Policy</string>

Layout.xml

<TextView
android:id="@+id/privacyPolicy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_fragment_privacy_policy" />

Kotlin code

string = getString(R.string.about_fragment_privacy_policy)
spannableString = SpannableString(string)
val clickableUserAgreement = object : ClickableSpan() {
override fun onClick(widget: View) {
startActivity(
Intent(Intent.ACTION_VIEW).setData(
Uri.parse(
"https://example.com"
)
)
)
}
}
val clickablePrivacyPolicy = object : ClickableSpan() {
override fun onClick(widget: View) {
startActivity(
Intent(Intent.ACTION_VIEW).setData(
Uri.parse(
"https://example.com"
)
)
)
}
}
spannableString.setSpan(
clickableUserAgreement,
0,
14,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(
ForegroundColorSpan(resources.getColor(R.color.colorPrimary)),
0,
14,
0
)
spannableString.setSpan(
clickablePrivacyPolicy,
19,
string.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(
ForegroundColorSpan(resources.getColor(R.color.colorPrimary)),
19,
string.length,
0
)
privacyPolicy.text = spannableString
privacyPolicy.movementMethod = LinkMovementMethod.getInstance()

How to make links in a TextView clickable

Buried in the API demos, I found the solution to my problem:

File Link.java:

    // 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());

I removed most of the attributes on my TextView to match what was in the demo.

<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtCredits"/>

That solved it. It is pretty difficult to uncover and fix.

Important: Don't forget to remove autoLink="web" if you are calling setMovementMethod().

How can I make several clickable parts of text in TextView

you can use android.text.style.ClickableSpan

    SpannableString ss = new SpannableString("Hello World");
ClickableSpan span1 = new ClickableSpan() {
@Override
public void onClick(View textView) {
// do some thing
}
};

ClickableSpan span2 = new ClickableSpan() {
@Override
public void onClick(View textView) {
// do another thing
}
};

ss.setSpan(span1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(span2, 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());


Related Topics



Leave a reply



Submit