2

How to highlite specific words which user select in EditText. For example I have one EditText in which user enter sentence "Hello how are you ?", Now User can select words Like this Image. And I have to display that selected words as highlited in entire sentence on TextView.

I already refer this, and this but not understood. Please help me.

Community
  • 1
  • 1
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113

4 Answers4

1

You can do this by using replace() method then Html.fromHtml() like below

yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
    @Override
    public void onFocusChange(View v, boolean hasFocus){
        if (hasFocus){
             if (str.contains("Hello how are you ?") == true) 
                {                     
                    str =  ((EditText)v)getText.ToString().replaceAll("Hello how are you ?", "<font color='red'>Hello how are you ?</font>");

                     ((EditText)v).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE);
                }
        }
    }
});

[Edit 1]

 yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
        @Override
        public void onFocusChange(View v, boolean hasFocus){
            if (hasFocus){
            // But the words in an ArrayList then use them  
            for(String s : arrLst){
            if (str.contains(s) == true)                     {                     
             str =  ((EditText)v)getText.ToString().replaceAll(s, "<font     color='red'>Hello how are you ?</font>");
              ((EditText)v).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE);                                     }

            }
          }
        }
    });

[Edit 2]

 yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
        @Override
        public void onFocusChange(View v, boolean hasFocus){
            if (hasFocus){

                 int sSelection = ((EditText)v)getText.ToString().getSelectionStart(); 
                 int eSelection = ((EditText)v)getText.ToString().getSelectionEnd(); 
                 String sString = string.substring(sSelection, eSelection);

                 str =  ((EditText)v)getText.ToString().replaceAll(sString , "<font color='red'>"+sString +"</font>");
                 ((EditText)v).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE);

            }
        }
    });
Talha
  • 12,673
  • 5
  • 49
  • 68
  • s2 can be any word for example "Hello how are you ?" you will decide it, which word you want to change, you can use as s2. I edited my answer – Talha Mar 18 '13 at 10:19
  • But word is not fix user can select words which he/she want to highlite. – Hardik Joshi Mar 18 '13 at 10:20
  • Where do you store the words that will be changed ? You can put them to an arraylist then using for each you can change them. – Talha Mar 18 '13 at 10:21
  • No..actually my requirement is user enter string in edittext, now he can able to select words which he like to highlite after pressing OK button I want to display that entire string with selected words in a textview with highlited. – Hardik Joshi Mar 18 '13 at 10:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26367/discussion-between-hardik-and-talhakosen) – Hardik Joshi Mar 18 '13 at 11:02
1

Try this May be Help you.

"android:textColorHighlight="#00f000"
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
Prashant09
  • 347
  • 3
  • 17
1

You can achieve that using spannable

taking a look at EditText there is an api to get Selection : Selection in EditText

and you can set multicolor text in a TextView using SpannableString

so combining both in a code like this:

edit.setOnEditorActionListener(
     new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
        actionId == EditorInfo.IME_ACTION_DONE ||
        event.getAction() == KeyEvent.ACTION_DOWN &&
        event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
    if (!event.isShiftPressed()) {
       // the user is done typing. 
//edit is the EditText
         Spannable spann = new SpannableString(edit.getText());        
      spann.setSpan(new ForegroundColorSpan(Color.BLUE), edit.getSelectionStart(),       edit.getSelectionEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     textView.setText(spann); // TextView 
       return true; // consume.
    }                
}
return false; // pass on to other listeners. 
}
 });

Should give you the effect you want

the end editing detection code is from here :SO Answer

Community
  • 1
  • 1
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
  • hello thanks for reply... When I enter text in edittext, select text and press ok it not display selected string. Plz help me. – Hardik Joshi Mar 18 '13 at 11:07
0

Use html like this :

 Spanned s = Html.fromHtml(string.replace(selectedString, "<font color=\"red\">" + selectedString + "</font>"));

edittext.text(s);
Sanket Pandya
  • 1,095
  • 7
  • 21
  • how i know selectedString of user ? – Hardik Joshi Mar 18 '13 at 10:15
  • int startSelection = et.getSelectionStart(); int endSelection = et.getSelectionEnd(); String selectedString = string.substring(startSelection, endSelection); You can directly : str="font COLOR=\"RED\">"+yourString"; textDisplayedBottom.setText(Html.fromHtml(str)); – Sanket Pandya Mar 18 '13 at 10:24