Clickable Word Inside Textview In Android
Solution 1:
This should do the trick. Just change your edittext's text in the OnClickListener
. It may be able to be reduced but this should work.
privatevoidfoo() {
SpannableStringlink= makeLinkSpan("click here", newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
// respond to click
}
});
// We need a TextView instance. TextViewtv=newTextView(context);
// Set the TextView's text
tv.setText("To perform action, ");
// Append the link we created above using a function defined below.
tv.append(link);
// Append a period (this will not be a link).
tv.append(".");
// This line makes the link clickable!
makeLinksFocusable(tv);
}
/*
* Methods used above.
*/private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) {
SpannableStringlink=newSpannableString(text);
link.setSpan(newClickableString(listener), 0, text.length(),
SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
return link;
}
privatevoidmakeLinksFocusable(TextView tv) {
MovementMethodm= tv.getMovementMethod();
if ((m == null) || !(m instanceof LinkMovementMethod)) {
if (tv.getLinksClickable()) {
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
/*
* ClickableString class
*/privatestaticclassClickableStringextendsClickableSpan {
private View.OnClickListener mListener;
publicClickableString(View.OnClickListener listener) {
mListener = listener;
}
@OverridepublicvoidonClick(View v) {
mListener.onClick(v);
}
}
Solution 2:
Better approach is
SpannableStringss=newSpannableString("Android is a Software stack");
ClickableSpanclickableSpan=newClickableSpan() {
@OverridepublicvoidonClick(View textView) {
startActivity(newIntent(MyActivity.this, NextActivity.class));
}
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//where 22 and 27 are the starting and ending index of the String. Now word stack is clickable // onClicking stack it will open NextActiivtyTextViewtextView= (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Solution 3:
You can use below code;
SpannableStringmyString=newSpannableString(Html.fromHtml("Please "+"<font color=\"#F15d36\"><u>"+"login"+"</u></font>" +" or "+ "<font color=\"#F15d36\"><u>"+"sign up"+ "</u></font>"+" to begin your YupIT experience"));
ClickableSpanclickableSpan=newClickableSpan() {
@OverridepublicvoidonClick(View textView) {
Toast.makeText(getContext(),"dfsgvdfs",Toast.LENGTH_SHORT).show();
}
};
ClickableSpanclickableSpan1=newClickableSpan() {
@OverridepublicvoidonClick(View textView) {
Toast.makeText(getContext(),"dfsgvdfs",Toast.LENGTH_SHORT).show();
}
};
myString.setSpan(clickableSpan,6,12,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myString.setSpan(clickableSpan1,15,23,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myString.setSpan(newForegroundColorSpan(Color.parseColor("#F15d36")),6, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myString.setSpan(newForegroundColorSpan(Color.parseColor("#F15d36")),15,23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvFound.setMovementMethod(LinkMovementMethod.getInstance());
tvFound.setText(myString);
Solution 4:
The best workaround I know is to create your own Button class. You could make the Button have a transparent background so that only the text is seen by the user. Then when the Button is pressed down change the TextColor and TextStyle of the button to be a darker color and underlined. This will work exactly as a link does. You can then use startActivity to go to the appropriated activity. You should not use hyperlinks to connect to other activities within your application.
Solution 5:
My personal opinion would be to make a second textview containing the text that you want to be your link. Then you could do your action in the onClick of this second textView . Also as zzzzzzzzzzz stated above, you could choose to change the font properties of that text to whatever you want once it has been clicked.
Post a Comment for "Clickable Word Inside Textview In Android"