Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText loses focus when keyboard appears; requires touching twice to edit

I have been designing an application which holds an expandable list. At the end of every list, an empty EditText is ready to receive comments. I have the following problem; when I touch the EditText, the screen resizes slightly (not a problem as the resizing does not always happen, depending on my layout and the position of the EditText int he list) and the soft-keyboard appears. However, during these events, the EditText loses focus and does not regain it. This is quite inconvenient as without focus, despite the keyboard being available, the input from the keyboard does not reach the EditText. Once I touch it again, the EditText behaves as expected.

It gets even stranger. With the keyboard still displayed, I can touch a different EditText and the same happens; it loses focus. However, once I passed the initial loss of focus, I can move freely between previously touched EditTexts without any focus related issues. Only when I hide the soft-keyboard will the "state" disappear and I would need to tap EditTexts twice before being able to edit them.

Here are excerpts from my code. First, the empty_comment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<EditText
    android:id="@+id/blank_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textCapSentences"
    android:imeOptions="actionDone"
    android:hint="@string/hint"
    android:layout_marginLeft="30dip" >
</EditText>
</LinearLayout>

Here an excerpt from the fragment where I use the layout in question:

convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_empty, null);
final EditText editText = (EditText) convertView.findViewById(R.id.blank_box);
editText.setOnEditorActionListener(new OnEditorActionListener()
{   
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
    {
        if(actionId == EditorInfo.IME_ACTION_DONE)
        {
            // Unrelated code
        }
        return true;
    }
});

I am not specifying anything input specific in my manifest file but I could provide it if deemed helpful.

Update:

Adding a line to the Activity to adjust the pan android:windowSoftInputMode="adjustPan" does partially solve the problem. It replaces the current unexpected behaviour with the problem that EditText views might get hidden by the soft-keyboard.

As of now, no ideal solution has been found but approached. Have a look at the comments in the accepted answer, they might prove useful to you!

like image 733
Eric Tobias Avatar asked Feb 06 '13 10:02

Eric Tobias


4 Answers

You need to change in your AndroidManifest.xml

Add android:windowSoftInputMode="adjustPan" in the activity holding the listview. This will solve your problem.

<activity android:name=".MyEditTextInListView"           android:label="@string/app_name"           android:windowSoftInputMode="adjustPan"> 
like image 193
jlopez Avatar answered Sep 25 '22 22:09

jlopez


A solution is to post a delayed runnable that checks to see if the EditText view is still focused, and if it is not, focus it.

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(final View v, boolean hasFocus) {
        if (hasFocus) {
            // Request focus in a short time because the
            // keyboard may steal it away.
            v.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (!v.hasFocus()) {
                        v.requestFocus();
                    }
                }
            }, 200);
        }
    }
});
like image 30
Tonic Artos Avatar answered Nov 08 '22 11:11

Tonic Artos


I had the same problem...I solved it by extending EditText.

I created an onClickListener and in this method I've got the following code:

public OnClickListener clickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        EditTextInput.this.setFocusable(true);
        EditTextInput.this.setFocusableInTouchMode(true);
        EditTextInput.this.requestFocus();
    }
};       

I then placed the following code in the onKeyPreIme callback. You'll have to extend EditText to get access to it.

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK & event.getAction() == KeyEvent.ACTION_UP) {
        EditTextInput.clearFocus();
        EditTextInput.this.setFocusable(false);
        EditTextInput.this.setFocusableInTouchMode(false);            
        return false;
    }
    return super.dispatchKeyEvent(event);
}

I find I have to toggle the focusable flag in order to bring the keyboard up on the very first click. Hope this helps...works for me.

like image 2
Houston Avatar answered Nov 08 '22 09:11

Houston


You could try something like :

editText.requestFocus();

You could also set this :

android:windowSoftInputMode="stateHidden"

in your manifest file

like image 1
lokoko Avatar answered Nov 08 '22 09:11

lokoko