-2

I need to get the typed words from textbox for further use.

So i am using the TextWatcher and at onTextChanged event i am getting the edittext box Content. It gives the full content of the text box.

But i need the typed word not the full content of the textbox. I need the last typed word in a string variable once the user press the spacebar.

My code is here and temptype holding the full content.

tt = new TextWatcher() {
            public void afterTextChanged(Editable s){
                 et.setSelection(s.length());
            }
            public void beforeTextChanged(CharSequence s,int start,int count, int after){} 

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                et.removeTextChangedListener(tt);
                typed = typed + et.getText().toString();
                String temptype;
                temptype = et.getText().toString(); 
                if (temptype == " "){
                    showToast("Word: "+typed);
                }
                et.addTextChangedListener(tt);
            }
        };
        et.addTextChangedListener(tt);
binu j
  • 399
  • 1
  • 3
  • 18
  • 5
    Add your code here... You can get last token form that string, using sub string methods. – Pankaj Kumar Oct 09 '14 at 07:04
  • 1
    Split String and get last Array Object – Top Cat Oct 09 '14 at 07:05
  • 1
    @Top Cat What if i edit the text in middle of a sentence? – binu j Oct 09 '14 at 07:06
  • My app is an translitration app, in which i need to send the typed word to the server for translitration. After receiving the translitrated text from the server i want to replace the typed word with this translitrated word. – binu j Oct 09 '14 at 07:10
  • The point still remains that if you're given the whole string you obviously have the last one typed; you just have to get it. – ChiefTwoPencils Oct 09 '14 at 07:17

1 Answers1

2
int selectionEnd = et.getSelectionEnd();
String text = et.getText().toString();
if (selectionEnd >= 0) {
    // gives you the substring from start to the current cursor
    // position
    text = text.substring(0, selectionEnd);
}
String delimiter = " ";
int lastDelimiterPosition = text.lastIndexOf(delimiter);
String lastWord = lastDelimiterPosition == -1 ? text : 
    text.substring(lastDelimiterPosition + delimiter.length());
// do whatever you need with the lastWord variable
Eugene Popovich
  • 3,343
  • 2
  • 30
  • 33