Search code examples
androidtextviewandroid-text-color

How to set different color to some text of a textiew and make that text clickable?


In my app i want to use a text view that should have half of its text black and second half should be green, also i want the green part to be clickable. I found some posts that show how to change color or how to make clickable but i am unable to find a combination of both. i have implemented this idea by myself, but the problem is that the string that should be clickable is not clickable. How to fix this? Any help will be appreciated. Here is my code

String firststring="Hello i am a textiew";
lString secondstring="ClickMe";
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString firstStringSpannable = new SpannableString(firststring);
firstStringSpannable .setSpan(new ForegroundColorSpan(Color.BLACK), 0, firststring.length(), 0);
builder.append(firstStringSpannable );

String space = " ";
SpannableString spaceSpannable = new SpannableString(space);
builder.append(spaceSpannable);

SpannableString secondSpannable = new SpannableString(secondstring);
secondSpannable .setSpan(new ForegroundColorSpan(Color.GREEN), 0, 
builder.append(secondSpannable );

textview.setText(builder, TextView.BufferType.SPANNABLE);
String comepleteString = firststring + " " + secondstring;
SpannableString spannableString = new SpannableString(comepleteString);
int startIndex = comepleteString.indexOf(secondstring);
int endIndex = startIndex + secondstring.length();

Spannable Spannable = (Spannable) textview.getText();
ClickableSpan myClickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
//performing some function
    }
};
Spannable.setSpan(myClickableSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Solution

  • Using setHighlightColor() method works most of the time:

    textView.setHighlightColor(ContextCompat.getColor(context, R.color.green));
    

    NOTE: Updated code to 2 different strings into 1 TextView and Second string will be coloured and clickable.

    Set your TextView's default color as BLACK Clickable part of will be GREEN

    There is a simple example:

    String stringFirst = "..."
    String stringSecond = "..."
    
    SpannableString spannable = new SpannableString(stringFirst + stringSecond);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
            }
    
            @Override
            public void onClick(View widget) {
                //Do your click action
            }
        };
        spannable.setSpan(clickableSpan, stringFirst.length()-1, stringFirst.length() + stringSecond.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    textView.setText(spannable);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setHighlightColor(ContextCompat.getColor(context, R.color.green));
    

    If it's not work add the line below

    ds.setColor(ContextCompat.getColor(context, R.color.green));
    

    to your updateDrawState method. It looks like this:

    String stringFirst = "..."
    String stringSecond = "..."
    
    SpannableString spannable = new SpannableString(stringFirst + stringSecond);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ContextCompat.getColor(context, R.color.green));
            }
    
            @Override
            public void onClick(View widget) {
                //Do your click action
            }
        };
        spannable.setSpan(clickableSpan, stringFirst.length()-1, stringFirst.length() + stringSecond.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    textView.setText(spannable);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setHighlightColor(ContextCompat.getColor(context, R.color.green));