Home » Android » TextView – OnClick – Android

TextView – OnClick – Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

Is it possible to add onclick to some portion of a textview? For instance my code goes like this,

String content = "Hello this a test.. For more details contact @Peter";
someTextView.setText(content);

I would like to add an onClick event for “@Peter”. Is that possible?

TIA.

How to&Answers:

Simple: -)

SpannableString link = makeLinkSpan("@Peter", new View.OnClickListener() {          
        @Override
        public void onClick(View v) {
            // Peforme Click
        }
    });
String content = "Hello this a test.. For more details contact";
someTextView.setText(content);
someTextView.append(link);

And makeLinkSpan() method is

private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) {
    SpannableString link = new SpannableString(text);
    link.setSpan(new ClickableString(listener), 0, text.length(), 
        SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
    return link;
}

ClickableString Class

private static class ClickableString extends ClickableSpan {  
    private View.OnClickListener mListener;          
    public ClickableString(View.OnClickListener listener) {              
        mListener = listener;  
    }          
    @Override  
    public void onClick(View v) {  
        mListener.onClick(v);  
    }        
}

Answer:

Thanks to @user370305 for the response.. However he missed some code snippet in his answer.. For the benefit of others, I’m pasting the entire code that worked for me,

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView someTextView = (TextView)findViewById(R.id.someTextView);

        String content = "Hello this a test.. For more details contact";
        someTextView.setText(content);

        SpannableString link = makeLinkSpan("@Peter", new View.OnClickListener() {          
                @Override
                public void onClick(View v) {
                    //Do something here..
                }
            });
            someTextView.append(link); 
        }
        makeLinksFocusable(someTextView);
    }

    private void makeLinksFocusable(TextView tv) {
        MovementMethod m = tv.getMovementMethod();  
        if ((m == null) || !(m instanceof LinkMovementMethod)) {  
            if (tv.getLinksClickable()) {  
                tv.setMovementMethod(LinkMovementMethod.getInstance());  
            }  
        }  
    }

    private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener)                 {
        SpannableString link = new SpannableString(text);
        link.setSpan(new ClickableString(listener), 0, text.length(), 
            SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        return link;
    }

    private static class ClickableString extends ClickableSpan {  
        private View.OnClickListener mListener;          
        public ClickableString(View.OnClickListener listener) {              
            mListener = listener;  
        }          
        @Override  
        public void onClick(View v) {  
            mListener.onClick(v);  
        }        
    }

Answer:

In you Xml add this,android:clickable="true"

in you activity use like this,

textView.setOnClickListener(new View.OnClickListener(){
    public void onClick(){

    }
}

Answer:

This is possible wither using Java code:

someTextView.setOnClickListener(new View.OnClickListener(){
    public void onClick(){
        //Do Stuff
    }
});

But first make sure that you set it clickable.

someTextView.setClickable(true);

Answer:

Try this:

SpannableString str = new SpannableString("@Peter");
str.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                // TODO Auto-generated method stub

            }
        }, 0, str.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

Then append str to your TextView.

You also need to call setMovementMethod(LinkMovementMethod.getInstance()); on your TextView