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

3.5k views Asked by At

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);
3

There are 3 answers

3
Oğuzhan Döngül On BEST ANSWER

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));
0
Pravin Suthar On

first of all you can put two textview one by one horizontally. than after activity class count character of given string than after split two same numbering (example : 26 char having than divide by 2 so basically ans will be 13 char in first text view and second 13 char in second textview)

how to work this:

string msg;
int lenght = msg.length();
int half = lenght/2;

String msg1 = msg.subString(0,half);
String msg2 = msg.subString(half,lenght);

This two string set on your two textView and which you want click event than plz manage it on your requirement textView also you con change color of textview by programmatically or xml file also

0
Geek On

Hope this could help you :)

protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            // Do something with span.getURL() to handle the link click...
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}

protected void setTextViewHTML(TextView text, String st1, String st2)
{
    CharSequence sequence = Html.fromHtml("<font color='green'>"+st1+"</font><font color='black'>"+st2+"</font");
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);   
    for(URLSpan span : urls) {
        makeLinkClickable(strBuilder, span);
    }
    text.setText(strBuilder);
    text.setMovementMethod(LinkMovementMethod.getInstance());       
}