Skip to content Skip to sidebar Skip to footer

Long Press On Selected Text In Textview In Android

In my android app, I have a text view and some text in it. I want that user can see the meaning of the words in a tool tip box when he long press on the word . My problem is that I

Solution 1:

use following code:

tv.setOnLongClickListener(newOnLongClickListener() {

        @OverridepublicbooleanonLongClick(View v) {
            // TODO Auto-generated method stubreturnfalse;
        }
});

and import:

import android.view.View.OnLongClickListener;

if you want Click just for some part Of View you need ClickableSpan. copy code from this link.

SpannableStringss=newSpannableString("Android is a Software stack");
    //ss.setSpan(new StyleSpan(Typeface.ITALIC), 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);ClickableSpanclickableSpan=newClickableSpan() {
        @OverridepublicvoidonClick(View textView) {
            startActivity(newIntent(MyActivity.this, NextActivity.class));
        }
    };
    ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextViewtextView= (TextView) findViewById(R.id.hello);
    textView.setText(ss);
    textView.setMovementMethod(LinkMovementMethod.getInstance());

Solution 2:

use setOnLongClickListener instead of OnClickListner

Post a Comment for "Long Press On Selected Text In Textview In Android"