How to set cursor in editText

I have two EditText, how can I set the cursor to 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 Electronic EditText is empty, position the cursor in the Electronic EditText. and the same for the EditText password

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;
    }

      

+3


source to share


5 answers


you can use requestFocus () method ; 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;
}

      

+2


source


Try using the method .requestFocus();



+2


source


Try the following:

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;
    }

      

+2


source


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 also; check it

+2


source


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;
}

      

+1


source







All Articles