Skip to content Skip to sidebar Skip to footer

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 nee

Solution 1:

Something like this should work.

TextViewlocation= (TextView) findViewById(R.id.location);
  location.setMovementMethod(LinkMovementMethod.getInstance());
  Spannablespans= (Spannable) location.getText();
  ClickableSpanclickSpan=newClickableSpan() {

     @OverridepublicvoidonClick(View widget)
     {
        //put whatever you like here, below is an example
        AlertDialog.Builderbuilder=newBuilder(MainActivity.this);
        builder.setTitle("Location clicked");
        AlertDialogdialog= builder.create();            
        dialog.show();
     }
  };
  spans.setSpan(clickSpan, 0, spans.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Solution 2:

there is a example in ApiDemos which can solve your problem. Check out com.example.android.apis.text.Link class, it may help. Following is part of the code:

TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(
        Html.fromHtml(
            "<b>text3:</b>  Text with a " +
            "<ahref=\"http://www.google.com\">link</a> " +
            "created in the Java source code using HTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());

Solution 3:

Post a Comment for "How To Create Textview That Will Act As A Link"