44

My problem: starting from the second time the software keyboard is shown on the screen, it entirely hides my EditText.

Attribute android:windowSoftInputMode="adjustPan" has been specified in the AndroidManifest.xml, but it works only the first time.

I have the following layout:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="464dp"
        android:gravity="top"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp" >

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="10"
            android:background="#E3E3E2"
            android:gravity="top" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/addListText"
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:layout_weight="10"
                android:contentDescription="@string/addItemContentDescription"
                android:gravity="bottom|center_horizontal"
                android:inputType="textLongMessage"
                android:textColor="#E3E3E2"
                android:visibility="gone" />

            <ImageButton
                android:id="@+id/addTextListButton"
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:layout_weight="1"
                android:layout_gravity="right"
                android:background="@android:color/transparent"
                android:contentDescription="@string/addItemButton"
                android:scaleType="fitCenter"
                android:src="@drawable/plus_add"
                android:visibility="gone" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="bottom|center_horizontal"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingRight="16dp" >

        <ImageButton
            android:id="@+id/syncListsButton"
            android:layout_width="64dp"
            android:layout_height="match_parent"
            android:src="@android:drawable/ic_popup_sync" />

        <ImageButton
            android:id="@+id/showOrHide"
            android:layout_width="64dp"
            android:layout_height="match_parent" />

        <ImageButton
            android:id="@+id/delListButton"
            android:layout_width="64dp"
            android:layout_height="match_parent"
            android:src="@android:drawable/ic_menu_delete" />
    </LinearLayout>
</LinearLayout>

The "showOrHide" button shows/hides the "addListText"/"addTextListButton" combo.

When the EditText is shown for the first time and I touch it, the soft keyboard appears on the screen and the "addListText"/"addTextListButton" combo is panned correctly. When I hide the keyboard and then show it again, the keyboard covers completely my addListText editbox!

Any idea on what's going on? I'm testing it on Android 4.2.2.

Please help! Thanks :-)

edit: I've also tried to put the first LinearLayout inside a ScrollView but it doesn't work!

bilbo20
  • 443
  • 1
  • 4
  • 5

8 Answers8

80

This may seem a bit silly but I ran into this problem when I set the property gravity of my EditText to either 'center_horizontal' or 'center'. The same problem occurs when using textAlignment 'center'. Remove it and you won't run into the problem of the keyboard hiding the EditText the second time (and subsequent ones) when the keyboard comes out.

pczern
  • 441
  • 3
  • 17
dbar
  • 1,740
  • 1
  • 16
  • 20
54

subclassed EditText and overridden the method onKeyPreIme(int keyCode, KeyEvent event) like this:

   @Override
   public boolean onKeyPreIme(int keyCode, KeyEvent event)
   {
      if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            clearFocus();
        }
   return super.onKeyPreIme(keyCode, event);
   }

Now when the back key is pressed, the EditText lost the focus. Then tapping it again adjustpan will work.

Chirag Shah
  • 2,058
  • 2
  • 20
  • 30
  • I'm using editText.clearfocus(), (without subclassing anything) and it does not seem to re adjustPan. The editText remains hidden below the soft keyboard.... What can I do to adjustPan on the fly? – SudoPlz Jun 02 '14 at 15:01
  • 4
    Weird but does not work for me. Maybe it's because the EditText is inside a fragment… will have to investigate :( – dst Oct 16 '14 at 07:33
  • 5
    Looks like I found the solution: there has to be at least one parent that is focusable, otherwise of course, the focus cannot be cleared ;) – dst Oct 16 '14 at 07:42
  • 3
    @alopix it works if you put this to the parent view `android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true"` – Borja Mar 08 '17 at 08:20
  • Thank you chirag. – KunalK Jul 19 '18 at 08:57
9

The problem you're experiencing is now a known platform bug as described here: https://code.google.com/p/android/issues/detail?id=182191

The fix is not backported and so will not take effect until Android N. Until Android N is your minimum SDK, the following solution is a workaround.

You can create a custom class extending EditText and override the method onKeyPreIme(int keyCode, KeyEvent event) like this:

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event)
{
   if(keyCode == KeyEvent.KEYCODE_BACK)
     {
         clearFocus();
     }
return super.onKeyPreIme(keyCode, event);
}

You should also ensure a higher level layout has the ability to be focused so the EditText's focus may be cleared successfully. The following attributes may work for you:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

Now when the back key is pressed, the EditText loses the focus. Then when you tap the button again, adjustpan will work as intended. (Posting as an answer since the edit was refused.)

colintheshots
  • 1,992
  • 19
  • 37
1

Here is the code I was originally using that ran into this problem:

    <EditText
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:gravity="center" />

When I removed the center gravity line from the EditText, the panning worked:

android:gravity="center"

I worked around it by wrapping the EditText in a FrameLayout and centering the entire EditText in the FrameLayout. The one issue it causes is that the EditText will be at least the width of the hinttext, so the first few characters/words typed won't be centered. This was fine for me but your mileage may vary :)

<FrameLayout
    android:layout_width="250dp"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</FrameLayout>

EDIT: this workaround breaks if the user is clicking on the very last word in the edittext. I'm now using the onKeyPreIme workaround

Mihai
  • 296
  • 2
  • 12
1

For me overriding onKeyPreIme doesn't work. So I override dispatchKeyEventPreIme instead.

@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
     clearFocus();
    }
    return super.dispatchKeyEventPreIme(event);
}
0

In EditText android:layout_gravity="center" Create this type of problem. If you want to text become in center then you can do this way

Step1: For Layout

 <LinearLayout
                android:id="@+id/linerPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/imageView2"
                android:gravity="center">

                <EditText
                    android:id="@+id/editText2"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:background="@null"
                    android:hint="*****"
                    android:layout_marginLeft="-40dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:inputType="textPassword"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textSize="18sp" />
            </LinearLayout>

step2: Some thing by programatically

 linerUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(mETUserName, 0);
            mETUserName.requestFocus();    
        }
    });

Finally work very well. I found this solution for adjust softKeyboard

Binesh Kumar
  • 2,763
  • 1
  • 16
  • 23
0

Usually the adjust pan doesn't work after the first time when we press the back button try to do that following way:- (can be implemented for the EditText, AutoCompleteTextView or any other view in which misbehaves)

 @Override
    public void onBackPressed() {
        editText.clearFocus();
        editText.setCursorVisible(false);
    }

Hope It helps someone

cammando
  • 596
  • 7
  • 20
0

After Trying many solutions in Manifest file, I solved it by below, adjustPan with Relative layout does not work,

Your parent Layout has to be LinearLayout

Hopes this saves some time for people.

Rishabh Dugar
  • 606
  • 5
  • 16