Java – Does Linkify work with TextView in Android?

Does Linkify work with TextView in Android?… here is a solution to the problem.

Does Linkify work with TextView in Android?

My code works for the method that calls EditText, I tried using the same code for TextView but it doesn’t work. Text doesn’t turn into hyperlinks like in EditText, does anyone 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 works fine in EditText, I just need help doing the same in TextView

Solution

Have a clickable range

and set text with a clickable range. You can set custom colors for clickabke spans. When you click text in a 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
   }
  } 

Take a snapshot

enter image description here

Edit:

Click the text in the TextView to open a browser with the URL. You can also pass a URL to an activity. Retrieves the URL and loads 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);

The 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

The 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"));
    }   
} 
 }

Related Problems and Solutions