Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus next textview automatically

I have a TextView with android:maxLength set to 3 and another with android:maxLength set to 7.

I want focus to automatically move to the second TextView once the 3 chars of the first TextView are filled up. How to achieve this without inheriting TextView and writing a custom implementation?

like image 519
Naseer Avatar asked May 31 '10 11:05

Naseer


2 Answers

I haven't tried but this might work.

Setup a listener to fire when the text was changed, for this use

addTextChangedListener(TextWatcher watcher)

Then based on the text length you can request the focus change to the next field.

If you want to find out the next field you can call View.focusSearch(View.FOCUS_FORWARD) Find the nearest view in the specified direction that can take focus. This does not actually give focus to that view.

like image 72
Pentium10 Avatar answered Sep 30 '22 21:09

Pentium10


For future readers expecting to COPY/PASTE

int MAX_LENGTH = 3;

txt1.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.length() == MAX_LENGTH) {
            txt2.requestFocus(View.FOCUS_DOWN);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});
like image 33
RMalke Avatar answered Sep 30 '22 23:09

RMalke