Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestFocus for TextView on Jelly Bean slow

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

like image 382
Paul Avatar asked Oct 31 '12 14:10

Paul


1 Answers

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.

like image 158
neteinstein Avatar answered Sep 20 '22 11:09

neteinstein