Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make parts of textview clickable (not url)

i've been struggling for some time now. On an activity in need the text: By clicking Register, I agree to the Terms of service and Privacy policy. The parts "Terms of service" and "Privacy policy" needs to be clickable. The solutions that i've found, made url's. But i don't need an url to browse to the terms, in need to start the terms activity, or the privacy activity.

Then i've found this: https://stackoverflow.com/a/9076448/1387161 But the problem here is, they aren't aligned beautifull. What i mean: if there is a phone with a small resolution, i need to set textview1 next to textview2 and textview3 under textview1, with next to it, textview4. But when i use the app on a tablet or a phone with a bigger screen, maybe all of the textviews can stand next to eachother, but the layout stays the same as on aphone's with a small screen =

Textview1 - Textview2 Textview3 - Textview4

One possible solution can be the flowlayout, but i'm getting errors and can't seem to find a good tutorial (for beginners) How to use flowlayout (or any custom layout)

Any ideas are appreciated!

Thx,

Bjorn

like image 799
Bjorn Vdkerckhove Avatar asked Mar 18 '13 11:03

Bjorn Vdkerckhove


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.

Can a TextView be clickable?

Just like Buttons and ImageViews we can add onClickListeners to TextViews by simply adding the attribute android:onClick="myMethod" to your TextView XML tag. The other way, TextView tv = (TextView) this.


3 Answers

public class MainActivity extends Activity {
TextView _tv;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    _tv = (TextView) findViewById( R.id.textView1 );
    String sentence = "this is [part 1 clickable] and [part 2 clickable] and [part 3 clickable]";


   _tv.setMovementMethod(LinkMovementMethod.getInstance());
   _tv.setText(addClickablePart(sentence), BufferType.SPANNABLE);
}
private SpannableStringBuilder addClickablePart(String str) {
    SpannableStringBuilder ssb = new SpannableStringBuilder(str);

    int idx1 = str.indexOf("[");
    int idx2 = 0;
    while (idx1 != -1) {
        idx2 = str.indexOf("]", idx1) + 1;

        final String clickString = str.substring(idx1, idx2);
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, clickString,
                        Toast.LENGTH_SHORT).show();
            }
        }, idx1, idx2, 0);
        idx1 = str.indexOf("[", idx2);
    }

    return ssb;
 }
}

Edit

public class MainActivity extends Activity {
TextView _tv;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    _tv = (TextView) findViewById( R.id.textView1 );

    SpannableString ss = new SpannableString("Android is a Software stack");

    ss.setSpan(new MyClickableSpan(), 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//22 to 27 stack is clickable
    ss.setSpan(new MyClickableSpan(), 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//0 to 7 Android is clickable

   _tv.setText(ss);
   _tv.setMovementMethod(LinkMovementMethod.getInstance());
}
class MyClickableSpan extends ClickableSpan{ //clickable span
    public void onClick(View textView) {
    //do something
       Toast.makeText(MainActivity.this, "Clicked",
            Toast.LENGTH_SHORT).show();
   }
    @Override
    public void updateDrawState(TextPaint ds) {
       ds.setColor(Color.GREEN);//set text color 
       ds.setUnderlineText(false); // set to false to remove underline
    }
}
}

More info on ClickableSpan http://developer.android.com/reference/android/text/style/ClickableSpan.html

You can also style the spannable string by making it bold , italics or setting font size.

    StyleSpan boldSpan = new StyleSpan( Typeface.ITALIC );
    ss.setSpan( boldSpan, 22, 27, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
    StyleSpan boldSpan1 = new StyleSpan(Typeface.BOLD);
    ss.setSpan(new RelativeSizeSpan(3f), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//set fontsize
    ss.setSpan( boldSpan1, 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
like image 132
Raghunandan Avatar answered Nov 11 '22 20:11

Raghunandan


If you need to use Raghunandan Answer with a HTML Message do the following In your Display function

public void displayText(String message) {

            chapterTextView.setText(Html.fromHtml(message),TextView.BufferType.SPANNABLE);
            chapterTextView.setMovementMethod(LinkMovementMethod.getInstance());
            Spannable clickableMessage = (Spannable) chapterTextView.getText();
            chapterTextView.setText(addClickablePart(clickableMessage), BufferType.SPANNABLE);
}

The Modified function of addClickablePart

private SpannableStringBuilder  addClickablePart(Spannable charSequence) {
        SpannableStringBuilder  ssb = new SpannableStringBuilder(charSequence);

        int idx1 = charSequence.toString().indexOf("(");
        int idx2 = 0;
        while (idx1 != -1) {
            idx2 = charSequence.toString().indexOf(")", idx1) + 1;

            final String clickString = charSequence.toString().substring(idx1, idx2);
            ssb.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View widget) {
                    Toast.makeText(getActivity(), clickString,
                            Toast.LENGTH_SHORT).show();
                }
            }, idx1, idx2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            idx1 = charSequence.toString().indexOf("(", idx2);
        }

        return ssb;
    }

Hope this help someone.

like image 30
Bahaa Hany Avatar answered Nov 11 '22 18:11

Bahaa Hany


ClickableSpan cs = new ClickableSpan() {  
     @Override 
     public void onClick(View view) {  
        //action stuff here
     }  
};

String text_terms_string = getResources().getString(R.string.register_terms_text);
SpannableString ss = new SpannableString(text_terms_string);

int termStart = text_terms_string.indexOf("Terms");
int termStop = termStart + "term".length();

//we set here clickable our text  
ss.setSpan(cs, termStart, termStop, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//we set here our color i.e. #cccccc in this example I take color from xml  
ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.aquaBlue)),termStart,termStop,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//we set here text to be bolded    
ss.setSpan(new StyleSpan(Typeface.BOLD),termStart,termStop,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//I set here spannable text to my previous declared TextView  
text_terms_link.setText(ss);

//We set here clickable text, this is important !!  
text_terms_link.setMovementMethod(LinkMovementMethod.getInstance());
like image 43
SAndroidD Avatar answered Nov 11 '22 20:11

SAndroidD