Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clickable word inside TextView in android

I have TextView with text that changed dynamically. This text contain strings like <a href='myWord'>myWord</a>. I want that after click to this "link" myWord appear in the EditText in the same activity.

This is my code:

txt.setText(Html.fromHtml("...<a href='link'>link</a>...")); txt.setMovementMethod(LinkMovementMethod.getInstance()); 

It's work well for URLs inside href attribute, but there is an error for another format.

I found a lot of similar questions on the StackOverflow but all of them were about url links. In my app I want create "link" inside activity. In general, I can change tag to some another if it's depend...

Please help me! Thank you!

-----SOLVED----- Thank you Jacob Phillips for idea!

May it will be interesting someone in future. This is a code:

//This is my string; String str = "<b>Text</b> which contains one <a href='#'>link</a> and another <a href='#'>link</a>"; //TextView; TextView txt = new TextView(this); //Split string to parts:                                         String[] devFull = data[v.getId()][1].split("<a href='#'>"); //Adding first part: txt.append(Html.fromHtml(devFull[0])); //Creating array for parts with links (they amount always will devFull.length-1): SpannableString[] link = new SpannableString[devFull.length-1]; //local vars: ClickableSpan[] cs = new ClickableSpan[devFull.length-1]; String linkWord; String[] devDevFull = new String[2];  for(int i=1; i<devFull.length; i++){     //obtaining 'clear' link     devDevFull = devFull[i].split("</a>");     link[i-1] = new SpannableString(devDevFull[0]);     linkWord = devDevFull[0];     cs[i-1] = new ClickableSpan(){         private String w = linkWord;         @Override         public void onClick(View widget) {             // here you can use w (linkWord)         }     };     link[i-1].setSpan(cs[i-1], 0, linkWord.length(), 0);     txt.append(link[i-1]);     try{         txt.append(Html.fromHtml(devDevFull[1]));     }     catch(Exception e){} } 
like image 713
lubart Avatar asked Apr 02 '12 01:04

lubart


People also ask

How do I make part of a TextView clickable?

The target=”_blank” attribute is just set to open a new tab in the browser. In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.


1 Answers

This should do the trick. Just change your edittext's text in the OnClickListener. It may be able to be reduced but this should work.

private void foo() {     SpannableString link = makeLinkSpan("click here", new View.OnClickListener() {                   @Override         public void onClick(View v) {             // respond to click         }     });      // We need a TextView instance.             TextView tv = new TextView(context);         // Set the TextView's text          tv.setText("To perform action, ");      // Append the link we created above using a function defined below.     tv.append(link);      // Append a period (this will not be a link).     tv.append(".");      // This line makes the link clickable!     makeLinksFocusable(tv); }  /*  * Methods used above.  */  private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) {     SpannableString link = new SpannableString(text);     link.setSpan(new ClickableString(listener), 0, text.length(),          SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);     return link; }  private void makeLinksFocusable(TextView tv) {     MovementMethod m = tv.getMovementMethod();       if ((m == null) || !(m instanceof LinkMovementMethod)) {           if (tv.getLinksClickable()) {               tv.setMovementMethod(LinkMovementMethod.getInstance());           }       }   }  /*  * ClickableString class   */  private static class ClickableString extends ClickableSpan {       private View.OnClickListener mListener;               public ClickableString(View.OnClickListener listener) {                       mListener = listener;       }               @Override       public void onClick(View v) {           mListener.onClick(v);       }         } 
like image 167
Jacob Phillips Avatar answered Oct 13 '22 13:10

Jacob Phillips