2

Is there a way to highlight a word in a TextView, so you can click it and an other Activity starts? I want to make a kind of dictionary App and some Word in the definitions aren't self-explaining, so i thought when i highlight them and the user could click them, an other activity should start with an other definition.

Thanks.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • 1
    Please take a look at [this](http://stackoverflow.com/questions/1697084/handle-textview-link-click-in-my-android-app) – Sergey Kuryanov Mar 14 '12 at 01:32
  • ...which points to here: http://blog.elsdoerfer.name/2009/10/29/clickable-urls-in-android-textviews/ – AJcodez Mar 14 '12 at 01:40
  • check this existing SO question: [Clickable words in a TextView](http://stackoverflow.com/questions/5453102/clickable-words-in-a-textview) – Paresh Mayani Mar 14 '12 at 05:12

2 Answers2

0

Give you a demo:

public class TextViewDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TextView txtInfo = new TextView(this);
            SpannableString ss = new SpannableString("helloworldandroid:.");
            ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new StrikethroughSpan(), 7, 10,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new UnderlineSpan(), 10, 16,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 15,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
            ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            txtInfo.setText(ss);
            txtInfo.setMovementMethod(LinkMovementMethod.getInstance());
            setContentView(txtInfo);
    }

}

Evan
  • 16
  • 2
0

Try With This..

    public class TextViewDemo extends Activity { 
        /** Called when the activity is first created. */ 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.main);
         TextView textdata = (TextView) findViewById(R.id.textdata);
          // To Display the Text as Link
            textdata .setText(Html
                    .fromHtml("<a href='Write Ur Text Here'>Write Ur Text Here</a>"));
            textdata .setMovementMethod(LinkMovementMethod.getInstance());

            // Performing action on TouchListener of ForgotPassword
            textdata .setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    Intent Forgotpwdintent = new Intent();
                    Forgotpwdintent.setClass(TextViewDemo .this,
                            TargetActivity.class);
                    startActivity(Forgotpwdintent);
                    return true;

                }
            });
}
}
user1213202
  • 1,305
  • 11
  • 23