Vinod
Vinod

Reputation: 32861

How to set focus to right of text in EditText for android?

In my application when users click or touch on the Edit Text view the focus is sometimes set to the beginning. For example if the existing text is "Hi". Users wants to click on it and change it to "H1 How are you?" by adding the text "How are you?". But since the focus is in the beginning, it becomes "How are you?Hi". So I always want to set to focus to the right most of the text when selected. How do I do this. Please let me know with some sample if possible. Thank you for your time and help.

Upvotes: 24

Views: 25455

Answers (8)

Sam Chen
Sam Chen

Reputation: 8857

Inside the addTextChangedListener():

@Override
public void afterTextChanged(Editable editable) {
    viewHolder.editText.setSelection(viewHolder.editText.getText().length());       //set cursor to the end
}

Upvotes: 0

   public void ontextchangelistner(){

     if(s.toString().startsWith("0")) {
        if (s.length() == 2) {
            if (s.toString().contains("00")) {

                editText.setText(s.toString().substring(1));
                int pos = editText.getText().length();
                editText.setSelection(pos);
            }
        }
   }

If you enter first time zero in edit text, it shows; but in second time pressing zero, this zero is not shown. I hope it will help you.

Upvotes: 1

Rodriquez
Rodriquez

Reputation: 1001

On the new api u can use something like this:

 //buttons setups
private void buttonsSetupForRecommendations() {
    button2.setNextFocusRightId(R.id.ok_button);
    button2.setNextFocusLeftId(R.id.ok_button);
    button2.setNextFocusDownId(R.id.cancel_button);
    button2.setNextFocusUpId(R.id.cancel_button);
    button.setVisibility(View.GONE);
    button2.setText(R.string.cancel_text);
    button1.setText(R.string.clear_text);
    button1.setNextFocusRightId(R.id.cancel_button);
    button1.setNextFocusLeftId(R.id.cancel_button);
    button1.setNextFocusUpId(R.id.ok_button);
    button1.setNextFocusDownId(R.id.ok_button);
    button1.requestFocus();
}

this work with button and other items. U can add this on java code ^^ or xml

Upvotes: 0

Unu
Unu

Reputation: 771

setSelection wasn't working for me, but this works like a charm. Works on afterTextChanged as well.

      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) 
      {
          edittext.requestFocus(edittext.getText().length());
      }

Upvotes: 3

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

A more prominent way would be where you don't need to take care of setting selection manually all the time. So Use CustomEditText instead of EditText in your layout.

public class CustomEditText extends EditText {

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * This is a Fix of buggy behavior of android, where after setting text using setText(...), selection remain on
     * first character and not at last character.
     */
    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        if (text != null && ! (text.toString().trim().length() == 0 )) {
            final int textLength = text.length();
            if (getSelectionEnd() != textLength) {
                setSelection(textLength);
            }
        }
    }

}

Upvotes: 0

Ankita Dodia
Ankita Dodia

Reputation: 11

Above solution is not working.

Here I give you new solution for set focus to right of text in edittext for android. And its working fine.

My code is:

    EditText edt_ans = (EditText)findViewById(R.id.edt_answer);

    edt_ans.addTextChangedListener(new TextWatcher() 
    { 
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) 
          {
               // TODO Auto-generated method stub
                edt_ans.setSelection(edt_ans.getText().length());
          }
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count,int after) 
          {
               // TODO Auto-generated method stub

          }
          @Override
          public void afterTextChanged(Editable s) 
          {
              // TODO Auto-generated method stub

          }
   });

Happy Coding....:)

Upvotes: 1

Something more especific about that you ask, you can use the next code:

EditText editText = (EditText) findViewById(R.id.textId);    
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            editText.setSelection(editText.getText().length());
        }
    }
});

the method setOnFocusChangeLister() is used for detect when the editText receive the focus.

Upvotes: 8

inazaruk
inazaruk

Reputation: 74780

You can explicitly put caret to last position in text:

EditText editText = (EditText) findViewById(R.id.textId);
int pos = editText.getText().length();
editText.setSelection(pos);

Upvotes: 51

Related Questions