Make parts of textview clickable (not url)

android, android-layout

Solution

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

Problem

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

Original source

Related problems