Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Linkify work for TextView in Android?

I have this code working for my method that calls an EditText, I tried to use the same code for a TextView but it does not work. The text does not turn into a hyperlink like it does in EditText, does anybody know why?

public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv = (TextView) findViewById(R.id.link_view);
    // make sure that setText call comes BEFORE Linkify.addLinks call
    tv.setText(tv.getText().toString());
    Linkify.addLinks(tv, Linkify.WEB_URLS);
}}

Here is the layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow>

        <TextView
            android:id="@+id/link_lbl"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingRight="10dip"
            android:text="Link" />

        <TextView
            android:id="@+id/link_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="google.com" />
    </TableRow>
</TableLayout>

This will work fine in EditText, i just need help doing the same thing in TextView

like image 802
Lucas Avatar asked Jul 03 '26 10:07

Lucas


1 Answers

Have clickable span and set the text with the clikable span. You can have custom color for the clickabke span. When you click on the text in textview it displays a toast.

String title="hello";
SpannableString ss1=  new SpannableString(title);
    ss1.setSpan(new MyClickableSpan(title), 0, ss1.length(), 0); 
    tv = (TextView) findViewById(R.id.textview);
    tv.setText(ss1);
    tv.setMovementMethod(LinkMovementMethod.getInstance());  

MyClickableSpan

   class MyClickableSpan extends ClickableSpan{     
   String clicked;
   public MyClickableSpan(String string)  
   {
    super();
    clicked =string;
   }
   public void onClick(View tv) 
   {
     // onclick of text in textview do something 
 Toast.makeText(MainActivity.this,clicked ,Toast.LENGTH_SHORT).show();
     //display a toast 
   }
   public void updateDrawState(TextPaint ds)
   {
     ds.setColor(Color.BLUE);//set text color 
     ds.setUnderlineText(true); // set to false to remove underline
   }
  } 

Resulting Snap Shot

enter image description here

EDIT:

Open a browser with the url on click on text in textview. You can also pass the url to a activity. Retrieve the url and load the url in webview.

 <uses-permission android:name="android.permission.INTERNET"/>

public void onClick(View tv) {
//do something

   Toast.makeText(MainActivity.this,clicked ,
        Toast.LENGTH_SHORT).show();
   String url = "http://www.example.com";
   Intent i = new Intent(Intent.ACTION_VIEW);
   i.setData(Uri.parse(url));
   startActivity(i);
}


                OR

In onClick()

   Intent t= new Intent(MainActivity.this,SecondActivity.class);
   t.putExtra("key","http://www.google.com");
   startActivity(t);

second.xml

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

  <WebView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/wv"></WebView>
  </LinearLayout>

Then in SecondActivty

public class SecondActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    WebView wv= (WebView) findViewById(R.id.wv);
    Bundle extras= getIntent().getExtras();
    if(extras!=null)
    {
        wv.loadUrl(extras.getString("key"));
    }   
} 
 }
like image 145
Raghunandan Avatar answered Jul 05 '26 23:07

Raghunandan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!