Monday, April 22, 2013

[android help] Does Linkify work for TextView in Android?


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.





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




android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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



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


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...