Sridhar
Sridhar

Reputation: 2228

Android: Edittext:: get Entered Character from the keyboard at any position

I need to find what is the new character inserted in the EditText. Actually I used TextChange Listener for finding the newly entered character in the textfield. For eg If I am typing the word "Success" one by one. I need to get newly entered character. If I insert X next to e, I need to detect that X character.

Please provide me best solution to do so

Upvotes: 1

Views: 2917

Answers (2)

Danish Siraj
Danish Siraj

Reputation: 1

You can use text watcher.

public class MainActivity extends Activity implements TextWatcher
{

    EditText gtex;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gtex=(EditText)findViewById(R.id.gtex);
        gtex.addTextChangedListener(this);
    }

    @Override
    public void afterTextChanged(Editable p1)
    {
        // TODO: Implement this method
    }

    @Override
    public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
    {
        // TODO: Implement this method
    }

    @Override
    public void onTextChanged(CharSequence s, int a1, int a2, int a3)
    {


        if(a1+a2>=0&&(a1+a2<s.length())){
            char ch=s.charAt(a1+a2);
        };

     //do stuff with ch

    }
}

Upvotes: 0

Md Hussain
Md Hussain

Reputation: 409

i think you have to try TextChangedListener on edittext for controlling on input of text

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtEdit = (EditText) findViewById(R.id.editText1);
    viewText = (TextView) findViewById(R.id.text);

     txtEdit.addTextChangedListener (new TextWatcher() {

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              Log.i("TC", "beforeTC " + s.toString() + " "
                      + s.subSequence(start, start + count).toString());
          }

          public void onTextChanged(CharSequence s, int start, int before, int count) {
              Log.i("TC", "onTC " + s.toString() + " "
                      + s.subSequence(start, start + count).toString());
          }

          public void afterTextChanged(Editable s) {
              Log.i("TC", "afterTC " + s.toString());
          }
     });
}

Upvotes: 4

Related Questions