Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create TextView that will act as a link

I have a Textview with location:

eg. "Mountain View, CA"

What I want to achieve is to create this text to act like a Link - color,underline, focusability etc.

This link doesn't need to direct anywhere - surrounding view has attached onClick listener that fires google maps intent.

like image 237
pixel Avatar asked Dec 01 '22 04:12

pixel


1 Answers

Something like this should work.

  TextView location = (TextView) findViewById(R.id.location);
  location.setMovementMethod(LinkMovementMethod.getInstance());
  Spannable spans = (Spannable) location.getText();
  ClickableSpan clickSpan = new ClickableSpan() {

     @Override
     public void onClick(View widget)
     {
        //put whatever you like here, below is an example
        AlertDialog.Builder builder = new Builder(MainActivity.this);
        builder.setTitle("Location clicked");
        AlertDialog dialog = builder.create();            
        dialog.show();
     }
  };
  spans.setSpan(clickSpan, 0, spans.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
like image 131
ageektrapped Avatar answered Dec 21 '22 05:12

ageektrapped