Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I change the look and feel of the clickablespan

I have to embed a clickable text in a paragraph text, clickablespan can do it. But instead of using the default focus/press/unfocus style, can I change those style ? Like when focus, the background color is blue, text color is white, when unfocus, the background color is white, text color is blue.

like image 297
user441316 Avatar asked Nov 26 '10 06:11

user441316


People also ask

How do you set the part of the text view is clickable in Android?

This example demonstrates how do I set the part of the Android textView as clickable. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is LinkMovementMethod?

android.text.method.LinkMovementMethod. A movement method that traverses links in the text buffer and scrolls if necessary. Supports clicking on links with DPad Center or Enter.


3 Answers

You can override the updateDrawState method of ClickableSpan:

SpannableString spannableString = new SpannableString("text");
spannableString.setSpan(
    new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(getContext(), "Click!", Toast.LENGTH_LONG).show();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(getResources().getColor(R.color.link));
        }
    }, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

textView.setText(spannableString);

textView.setMovementMethod(LinkMovementMethod.getInstance());
like image 158
bryant1410 Avatar answered Sep 27 '22 19:09

bryant1410


Here is the example

bottomMsg = (TextView) findViewById(R.id.bottom_msg);
int start = bottomMsg.getText().toString().indexOf(terms);
MovementMethod movementMethod = LinkMovementMethod.getInstance();
bottomMsg.setMovementMethod(movementMethod);

Spannable text = (Spannable) bottomMsg.getText();
//TermAndConditions is a clickableSpan.
text.setSpan(new TermAndConditions(), start, start + terms.length(), Spannable.SPAN_POINT_MARK);
text.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.customYellowColor)), start, start + terms.length(), 0);

The point is, color-span is after the clickable-span. otherwise, the color not changed.

like image 23
yincan Avatar answered Sep 27 '22 21:09

yincan


Link color can be changed in xml:

<TextView
    android:textColor="..."
    android:textColorLink="..." />
like image 23
Pavel Avatar answered Sep 27 '22 19:09

Pavel