0

Is it possible to give different OnClickListeners to different words of the same TextView? If not can you show me a way to do such a thing, keeping in mind that i need to lay words out correctly? I mean I just can't add a TextView for every word because the text I have to show may be very long, and no layout will put them to a new line when one line ends. Thank you =)

ddfra
  • 2,413
  • 14
  • 24
  • http://stackoverflow.com/a/10697453/1898809 – Breadbin Dec 12 '14 at 10:33
  • This [SO thread](http://stackoverflow.com/questions/8612652/select-a-word-on-a-tap-in-textview-edittext) might help you. Take a look, hope that helps – Shiv Dec 12 '14 at 10:34

1 Answers1

1

For this you have to make spannable String,below is my code to do so-. in first method makeTagLinks pass text that you want to split and second parameter is your text view.Second method for click listener.

 private void makeTagLinks(final String text, final TextView tv) {
        tv.setText("");
        String split[] = text.split("(?= )");
        int mColor = mContext.getResources().getColor(R.color.font_color);
        SpannableString ss1 = null;
        for(int i=0;i<split.length;i++){
              ss1=  new SpannableString(split[i]);     
              int l = split[i].length();
              String tag = split[i].trim();
              ss1.setSpan(new MyClickableSpan(tag), 0, l,  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

              if(tag.startsWith("#")){
                  ss1.setSpan(new ForegroundColorSpan(mColor),0,l,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
              }
              else{
                  if(i==0){
                      ss1.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, l, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                      ss1.setSpan(new ForegroundColorSpan(mColor),0,l,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                  }
                  else{
                      ss1.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 0, l, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                      ss1.setSpan(new ForegroundColorSpan(Color.BLACK),0,l,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                  }

              }
              tv.append(ss1);
        }
        tv.setMovementMethod(LinkMovementMethod.getInstance());
}

 class MyClickableSpan extends ClickableSpan{   
        String clickedText = "";
        public MyClickableSpan(String string) {
            super();
            clickedText = string; 
        }
        public void onClick(View tv) {
         if(clickedText.startsWith("#")){
                try{
                    if(dialog!=null){
                      dialog.dismiss();
                    }
                }
                catch(Exception e){
                    e.printStackTrace();
                }
                FragmentTransaction ft = mFragmentManager.beginTransaction();
                Fragment mFragment = MyFragment.getInstance(mFragmentManager,clickedText);
                ft.replace(R.id.container,mFragment);
                ft.addToBackStack(null);
                ft.commit();
         }
        }
        @Override
        public void updateDrawState(TextPaint ds) {

        }
} 

Hope it will help you.

Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68