2

How to intents (sent to other activity) part of string from textview? like this image : intent part of string

I already settext on textview like this:

lbl_checkback_tommorow = (TextView) findViewById(R.id.lbl_checkback_tommorow);
final String update_profile = "update your profile";
final String hot_news = "Hot news?";
lbl_checkback_tommorow.setText("Check back tommorow,\n and you should be ready to start booking your workout. While you wait, would you want to "+update_profile+", or read some of the interesting posts we have in our "+hot_news);

How to make update_profile and hot_news clickable (go to another activity)?

Update: Clickable span like this:

SpannableString update_profile = new SpannableString("update your profile");
SpannableString hot_news = new SpannableString("Hot news?");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
    startActivity(new Intent(MyActivity.this, NextActivity.class));
}
@Override
public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setUnderlineText(false);
    }
};
update_profile.setSpan(clickableSpan, 118, 215, 234, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
hot_news.setSpan(clickableSpan, 118, 215, 234, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableStringBuilder builder = new StringBuilder("Check back tommorow,\n and you should be ready to start booking your workout. While you wait, would you want to "+update_profile+", or read some of the interesting posts we have in our "+hot_news);
lbl_checkback_tommorow = (TextView) findViewById(R.id.lbl_checkback_tommorow);
lbl_checkback_tommorow.setText(builder);

1 Answers1

3

How to make update_profile and hot_news clickable (go to another activity)?

Using a SpannableStringBuilder you can convert your text in a Spannable object and set a ClickableSpan on the part of your text you want to make clickable. When the ClickableSpan's onClick is invoked you can start the Activity.

EDIT

this will do it

final String part1 = "Check back tommorow, and you should be ready to start booking your workout. While you wait, would you want to ";
final String part2 = " , or read some of the interesting posts we have in our ";
SpannableStringBuilder builder = new SpannableStringBuilder(part1);
SpannableString update_profile = new SpannableString("update your profile");
SpannableString hot_news = new SpannableString("Hot news?");
update_profile.setSpan(new ClickableSpan() {
      @Override
      public void onClick(View widget) {
            Toast.makeText(MainActivity.this, "first", Toast.LENGTH_LONG).show();
      }
 }, 0, update_profile.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(update_profile);
builder.append(part2);
hot_news.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(MainActivity.this, "second", Toast.LENGTH_LONG).show();
        }
}, 0, hot_news.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(hot_news);
lbl_checkback_tommorow.setText(builder);
lbl_checkback_tommorow.setMovementMethod(LinkMovementMethod.getInstance());
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • more or less. You need to instantiate a `SpannableStringBuilder` providing the text (SpannableStringBuilder builder = new StringBuilder("Check back tommorow,\n and you should be..");`, then you will have to look the start index and end index of update_profile and hot_news, and call setSpan on the builder object with those index. After that you will have to set the builder as text for your textView – Blackbelt Dec 14 '15 at 10:10
  • what is setspan, is it for coloring the string? just like span in html. I update the code with SpannableStringBuilder and settext as builder. is that right? – cari jawaban Dec 14 '15 at 10:18
  • `setSpan` applies the Spannable object to the builder. You can't use `+` to concatenate the strings in this case. Try with `builder.append(hot_news)` – Blackbelt Dec 14 '15 at 10:21