Reputation: 71
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
Upvotes: 3
Views: 3728
Reputation: 133560
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
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"));
}
}
}
Upvotes: 2
Reputation: 13540
Just try below code. It works fine for me.
TextView tv = ....
tv.setMovementMethod(LinkMovementMethod.getInstance());
String content = tv.getText().toString();
List<String> links = new ArrayList<String>();
Pattern p = Patterns.WEB_URL;
Matcher m = p.matcher(content);
while (m.find()) {
String urlStr = m.group();
links.add(urlStr);
}
SpannableString f = new SpannableString(content);
for (int i = 0; i < links.size(); i++) {
final String url = links.get(i);
f.setSpan(new InternalURLSpan(new OnClickListener() {
public void onClick(View v) {
Context ctx = v.getContext();
String urlToOpen = url;
if (!urlToOpen.startsWith("http://") || !urlToOpen.startsWith("https://"))
urlToOpen = "http://" + urlToOpen;
openURLInBrowser(urlToOpen, ctx);
}
}), content.indexOf(url), content.indexOf(url) + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(f);
Upvotes: 2