Search code examples
androidtext-formatting

android dev how to underline and change color of word in string like a link color


I have a app that gets a string from a database and it sets to a label. Now i want that label to underline one word such as "This word should be underlined." and i want to be able to click on that underline word and get its value. So do i set it up before i send it to the database or after. Thanks for any help. I tried code below and each line is highlighted because of the for loop. please help

SpannableStringBuilder builder = new SpannableStringBuilder();
            for(int i=0;i<ListClass.getLatestActivity().size();i++){
                String myString  = ListClass.getLatestActivity().get(i);
                builder.append(myString);
                String substringThatShouldBeClickable = myString.substring(0,myString.indexOf(' ')).trim();
                MySpan span = new MySpan(substringThatShouldBeClickable);
                span.setOnMySpanClickListener(mySpanOnClickListener);

                int start = 0;
                int end = builder.length();

                builder.setSpan(span, start, end, 0);
                builder.append("\n" + "\n") ;
            }
            RAInfo.setText(builder);    
            RAInfo.setMovementMethod(LinkMovementMethod.getInstance());

Solution

  • Ok, so there's a few things you'll need to do. They way to accomplish this is by using a span inside of the TextView.

    First you'll need a class that extends ClickableSpan:

    public class MySpan extends ClickableSpan {
        public interface OnMySpanClickListener {
                public void onMySpanClick(String tag);
        }
    
        private final String myData;
        private OnMySpanClickListener mOnMySpanClickListener;
    
        public MySpan(String tag) {
                super();
                if (tag == null) {
                        throw new NullPointerException();
                }
                myData = tag;
        }
    
        @Override
        public void onClick(View widget) {
                if (mOnMySpanClickListener != null) {
                    mOnMySpanClickListener.onMySpanClick(myData);
                }
        }
    
        public OnMySpanClickListener getOnMySpanClickListener() {
                return mOnMySpanClickListener;
        }
    
        public void setOnMySpanClickListener(OnMySpanClickListener onMySpanClickListener) {
                mOnMySpanClickListener = onMySpanClickListener;
        }
    }
    

    In your Activity, you'll set the text of the TextView like this:

    String myString = getFromDatabase();    
    
    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append(myString);
    
    //You'll need to call the constructor for MySpan with only the value of the part
    //of the string that you want to work with ("Bob" in the example), however you 
    //determine that.
    String substringThatShouldBeClickable = getMySubstring(myString);  //"Bob"
    MySpan span = new MySpan(substringThatShouldBeClickable);
    span.setOnMySpanClickListener(mySpanOnClickListener);
    
    //start and end control the range of characters in the string that are clickable,
    //so modify this part so it only underlines the characters you want clickable
    int start = 0;
    int end = bulider.length();
    
    builder.setSpan(span, start, end, 0);
    
    label.setText(builder);
    label.setMovementMethod(LinkMovementMethod.getInstance());
    

    Finally, you'll need a handler for the click events on the span:

    MySpan.OnMySpanClickListener mySpanOnClickListener = new MySpan.OnMySpanClickListener() {
        public void onMySpanClick(String tag) {
            //Here is where you'll do your work with the value in the String "tag"
        }
    };
    

    Hope this helps.