Ahmad Al ALloush
Ahmad Al ALloush

Reputation: 385

How to set the cursor to the editText

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

Upvotes: 3

Views: 128

Answers (5)

Valdio
Valdio

Reputation: 109

Try using the .requestFocus(); method

Upvotes: 2

Fakhriddin Abdullaev
Fakhriddin Abdullaev

Reputation: 4910

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

Upvotes: 1

Sunisha Guptan
Sunisha Guptan

Reputation: 1605

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

Upvotes: 2

AskNilesh
AskNilesh

Reputation: 69689

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

Upvotes: 2

sumit
sumit

Reputation: 1087

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

Upvotes: 2

Related Questions