Created
May 23, 2018 20:10
-
-
Save devmike01/2cfcfbb0a02dca36b94c6ece07a053ac to your computer and use it in GitHub Desktop.
This code snippet makes part of a string clickable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<declare-styleable name="PartClickTextView"> | |
<attr name="clickable_texts" format="string"/> | |
</declare-styleable> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xxx.xxxx.xxxx.PartClickTextView | |
android:id="@+id/cant_login" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:clickable="true" | |
android:focusable="true" | |
android:padding="20dp" | |
android:text="Can't login?" | |
android:textAppearance="?android:textAppearanceSmall" | |
android:textColor="@color/grey" | |
android:textSize="23sp" | |
app:clickable_texts="Click here" /> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PartClickTextView extends TextView{ | |
public static final String TAG = PartClickTextView.class.getSimpleName(); | |
private OnPartTextClickListener mListener; | |
public interface OnPartTextClickListener{ | |
void onPartTextClick(View view); | |
} | |
public void setOnPartTextClickListener(OnPartTextClickListener mCallback){ | |
this.mListener = mCallback; | |
} | |
public PartClickTextView(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
turnTextToLink(context, attrs); | |
} | |
//Allow the user to click on part of the text | |
private void turnTextToLink(final Context context, AttributeSet attr){ | |
TypedArray a = context.getTheme().obtainStyledAttributes(attr, R.styleable.PartClickTextView, 0, 0); | |
String text = a.getString(R.styleable.PartClickTextView_clickable_texts); | |
if (TextUtils.isEmpty(text))return; | |
String mainText = getText().toString().concat(" "+ text); | |
setClickable(true); | |
SpannableString ss = new SpannableString(mainText); | |
ClickableSpan clickableSpan = new ClickableSpan() { | |
@Override | |
public void onClick(View textView) { | |
if(mListener != null) { | |
mListener.onPartTextClick(textView); | |
} | |
Log.d(TAG, "CLICKED"); | |
} | |
@Override | |
public void updateDrawState(TextPaint ds) { | |
super.updateDrawState(ds); | |
ds.setUnderlineText(false); | |
} | |
}; | |
ss.setSpan(clickableSpan, mainText.lastIndexOf(text.substring(0, 1)), | |
mainText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | |
setText(ss); | |
setMovementMethod(LinkMovementMethod.getInstance()); | |
setHighlightColor(Color.TRANSPARENT); | |
a.recycle(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment