requestFocus for TextView on Jelly Bean slow
Asked Answered
H

1

18

I am developing an application that has 4 text fields for entering data into and I have come across a performance issue when moving focus from one to the other. When a field has a character entered into it I use the addTextChangedListener to monitor the text and move the focus to the next text field. This was working fine on versions of android before 4.1.1 but since testing on 4.1.1 there is a noticeable lag when you press a key before the focus appears in the next field. This means if the user types rapidly, key presses can be lost.

I have a simple app using the following code

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    one = (EditText)findViewById(R.id.editText1);
    two = (EditText)findViewById(R.id.editText2);

    one.addTextChangedListener(new TextWatcher() {


        @Override
        public void afterTextChanged(Editable s) {
            two.requestFocus();

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }
    });
}

that highlights the issue. When run on a 4.0.4 based device everything is fine, but on 4.1.1 it takes a while to move the focus.

I have tested this on 2 different Samsung Galaxy s3's one with 4.0.4 and one with 4.1.1.

Has anyone else seen this?

Many thanks

Paul

Haddock answered 31/10, 2012 at 14:36 Comment(2)
I am also experiencing this. Any update on this?Kingship
Does Traceview offer any clues as to where the time is being taken up?Heckelphone
W
12

I don't know if there is a solution for that problem... but I might have a "hack" that gives an alternative solution while the problem exists:

  1. Put an EditText out of the screen. (I normally set it to the right of the right margin with a RelativeLayout).

  2. Set onTouchListener to your visible EditText (and set the EditText to not be clickable). The onTouchListener should the focus to the hidden EditText.

  3. On the hidden EditText set a addTextChangedListener which for each character added or removed makes the proper changes on the visible EditTexts.

Example:

If I have 4 EditTexts for PIN with IDs: A, B, C and D and a EditText out of the screen with id hidden:

Whenever I receive the first char on hidden I write A.

Whenever I receive the second char on hidden I write B.

Whenever I receive a delete on the second char of hidden I delete on B.

...

I'm doing something similar on one of my apps, without problems.

Windproof answered 12/12, 2012 at 18:58 Comment(3)
very creative solutionWahoo
it sounds really a little bit weird, but had the same idea, did it like described above, works like a charm. thx... has anyone any issues reported on some devices?Moccasin
I believe this problem is also there in jetpack compose, Link to the problem - #74697749Stockbroker

© 2022 - 2024 — McMap. All rights reserved.