3

I have two EditText, how can I set the cursor in the right EditText

 EditText emailE = (EditText) findViewById(R.id.editTextEmailLogin);
 EditText passwordE = (EditText)findViewById(R.id.editTextPasswordLogin);
 String email = emailE.getText().toString().trim();
 String password = passwordE.getText().toString().trim();

if the user pressing Singin and the email EditText is empty, set the cursor in the email EditText. and the same for password EditText

if (TextUtils.isEmpty(email)){
        Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
        //set cursor in Email editText
        emailE.setSelection(0);
        return;
    }
    if(TextUtils.isEmpty(password)){
        Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
        //set cursor in Password editText
       passwordE.setSelection(0);
        return;
    }
Ahmad Al ALloush
  • 330
  • 3
  • 10

5 Answers5

2

Try using the .requestFocus(); method

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Valdio
  • 101
  • 1
  • 6
2

Try this:

if (TextUtils.isEmpty(email)){
        Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
        //set cursor in Email editText
        emailE.requestFocus();
        return;
    }
    if(TextUtils.isEmpty(password)){
        Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
        //set cursor in Password editText
       passwordE.requestFocus();
        return;
    }
sumit
  • 1,047
  • 1
  • 10
  • 15
2

you can use requestFocus(); method of edittext like this

if (TextUtils.isEmpty(email)){
    Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
    //set cursor in Email editText
    emailE.requestFocus();
    return;
}
if(TextUtils.isEmpty(password)){
    Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
    //set cursor in Password editText
   passwordE.requestFocus();
    return;
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
2
etext1.setSelection(Your position)

or

 EditText etext1 = (EditText)findViewById(R.id.etext1 );
    etext1.setSelection(etext1.getText().length());  

or

etext1 .requestFocus(Your_Text.length());

Try this one also; Check this

Sunisha Guptan
  • 1,555
  • 17
  • 44
1
EditText editText = (EditText) findViewById(R.id.myTextViewId);


if (TextUtils.isEmpty(email)){
    Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
    //set cursor in Email editText
    emailE.requestFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(emailE, InputMethodManager.SHOW_IMPLICIT);
    return;
}
if(TextUtils.isEmpty(password)){
    Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
    //set cursor in Password editText
    passwordE.requestFocus();
    InputMethodManager imm2 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm2.showSoftInput(passwordE, InputMethodManager.SHOW_IMPLICIT);
    return;
}
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37