Martin Berger
Martin Berger

Reputation: 1708

Android ListView with EditText loses focus when the keyboard displays. Keyboards appear randomly

I am an iOS developer, and I have been given task to fix this. After digging and googling, I have no idea what to do.

We have this screen:

enter image description here

This screen is Activity with custom Fragment, containing ListView fed by ArrayAdapter. The adapter produces views by getView it seems. Every row has seven EditText controls.

Problem 1: When screen is shown first time, and you tap on any EditText, nothing happens. Why? When you tap second time, and any subsequent time, focus works, keyboard appears, and you can type.

Problem 2: This is hard to describe. On some devices, tapping any EditText causes keyboards to switch between some numeric and alphanumeric types, and this goes on and on and then stops. Why?

After trying various stuff: getExtractedText on inactive InputConnection warning on android onClick event is not triggering | Android Android EditText doesn't show the Keyboard ...

I have no idea what is the problem.

According to one of SO posts, you should not have both onClick and onTouch, yet here they are. Why?

        editText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    view.requestFocus();
                    break;
            }
            return false;
        }
    });

    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((EditText) v).selectAll();
        }
    });
    editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                if (event.getAction() == KeyEvent.ACTION_UP) {
                    InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return false;
        }
    });
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                double amount = Double.parseDouble(((EditText)v).getText().toString());
                DecimalFormat df = new DecimalFormat("0.0");
                String result = df.format(amount);
                ((EditText)v).setText(result);
            }
        }
    });
}

I am still digging, commenting out various code, but I am in dark, please help.

Whole code: https://gist.github.com/MartinBergerDX/473c47b008b6b0570466794f221eca31

Update 1:

I have removed all listeners, onTouch, onFocus, and text watchers.

And this is still happening:

enter image description here

Update 2:

It seems this is related to keyboard covering ListView. Not sure why, but when I set less than 10 items in data source, keyboard does not cover content and ping pong effect does not happen.

Upvotes: 1

Views: 745

Answers (2)

Manoj Mohanty
Manoj Mohanty

Reputation: 370

I am not sure if you have xml for this view or you are generating these edittext programatically if you have xml then for each edit text do android:inputType="number" if you are generating these editext programatically then do this for each edittext.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER); with this only number key willl be shown for user input next just add addTextChangedListener to each editText

editText.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) {
//handle your changes here
    }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

Update

Android ListView with EditText loses focus when the keyboard displays The issue is when you have a ListView with input capable fields that displays the soft keyboard on focus, the EditText loses its focus for the first time but the second time it just works fine. The reason it happens is all the views are getting rendered again, so the EditText field object in the item row that used to be focused is now a completely different object. android:descendantFocusability="beforeDescendants" in your ListView and android:windowSoftInputMode="adjustPan" for your activity in the app Manifest

<ListView android:id="@+id/productList" android:layout_width="match_parent"
 android:layout_height="wrap_content" android:layout_alignLeft="@+id/search" 
android:layout_below="@+id/search" 
android:padding="5dp" 
android:descendantFocusability="beforeDescendants"/>
<activity android:name=".myActivity" 
android:label="@string/app_name" android:screenOrientation="sensorPortrait" android:windowSoftInputMode="adjustPan"/> 

Reference https://www.mysamplecode.com/2013/02/android-edittext-listview-loses-focus.html

Upvotes: 1

malekiamir
malekiamir

Reputation: 129

maybe this is useful for you: How can I detect a click in an onTouch listener?
Try deleting onClick and using onTouch only, wish it helps.

Upvotes: 0

Related Questions