Soft keyboard covers an EditText in a PopupWindow

I’ve thrown together a simple test project that displays a PopupWindow containing an EditText (on Android 2.2). When I tap the EditText, the soft keyboard is shown, as I would expect. However, the soft keyboard covers the EditText, and I cannot get the screen to pan to keep the EditText in view in the way I would have thought it should. My code:

TestAdjustPanActivity.java:

  • Android - getting from a Uri to an InputStream to a byte array?
  • Android - How to start third party app with package name?
  • Android unit test with ActivityInstrumentationTestCase2, getActivity() hangs when previous test case causes another Activity to be launched
  • Convert magnetic field X, Y, Z values from device into global reference frame
  • “The prefix ”xliff“ for element ”xliff:g“ is not bound”
  • Can you get to a command line on the G1 phone?
  • package com.example;
    
    import android.app.Activity;
    import android.graphics.drawable.BitmapDrawable;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.widget.PopupWindow;
    
    public class TestAdjustPanActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            final PopupWindow pw = new PopupWindow(this);
            pw.setContentView(getLayoutInflater().inflate(R.layout.popup, null, false));
            pw.setWidth(400);
            pw.setHeight(600);
            pw.setFocusable(true);
    
            pw.setOutsideTouchable(true);
            pw.setTouchable(true);
            pw.setBackgroundDrawable(new BitmapDrawable());
    
            // This is the line referred to in the edit:
            pw.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    
            findViewById(R.id.main).post(new Runnable() {
                public void run() {
                pw.showAtLocation(findViewById(R.id.main), Gravity.NO_GRAVITY, 0, 0);
                }
            });
        }
    }
    

    main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    
    </LinearLayout>
    

    popup.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#666666"
            android:orientation="vertical" >
    
            <EditText
                android:id="@+id/current_password"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="40dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="300dp" />
        </LinearLayout>
    
    </ScrollView>
    

    …and my AndroidManifest.xml does contain the line android:windowSoftInputMode="stateUnspecified|adjustPan" in the tag for the one and only activity in the app.

    Any ideas? None of the other posts on SO related to this issue have done it for me. What am I missing that’s keeping the view from panning the way I would expect it to?

    EDIT: I tried adding a line in TestAdjustPanActivity as indicated, which caused the screen to pan perfectly on an Android 3.2 device I have. However, it still doesn’t pan on my Android 2.2 device, which makes this even more confusing.

    Related posts:

    Resize ImageButton?
    ProgressBar Circle shows no progress on android L (API 21)
    View getX() and getY() return 0.0 after they have been added to the Activity
    Proper way to handle Android Studio&#039;s NullPointerException lint warning
    What does FragmentManager and FragmentTransaction exactly do?
    Data Binding class not generated
  • Accessibility function implementation problems in Android
  • Hide a EditText & make it visible by clicking a menu
  • How to compress image size?
  • Select + copy text in a TextView?
  • getOnClickListener() in Android views
  • Android. Find view by tag
  • 5 Solutions collect form web for “Soft keyboard covers an EditText in a PopupWindow”

    For anyone stumbling upon this in the future, I ended up just using a Dialog instead of a PopupWindow, and panning to keep the EditText in view works fine on 2.2 with a Dialog.

    There is simpler way. Just provide alternative layout for active keyboard.

    1. Left click on project select: “Android tools/New Resource File…” .
    2. Chose layout, give file name “main” (don’t worry about conflicts).
    3. Click “next”. Then on list on the left select “keyboard” and move it to right (click “->”).
    4. On right side select keyboard state.
    5. Click finish.
    6. Now copy content of your main.xml located in “res/layout” to new file in res/layout-keyssoft”.
    7. Correct new layout in such way that keyboard is not in that way. Remember to maintain same “id”s for respective components in those two layouts (that is why copy paste was needed).
    8. Enjoy how it works

    Read about configuration changes to understand how it works. Note that EVERY configuration change (orientation change, language change, …) will cause recreation of Activity (in such case argument of onCreate is not null) so you can provide different layouts for different cases.

    I doubt the problem is because of using that ScrollView in XML,

    There has been another problem similar to this, You can check all different answers there, and may work for you.

    change this

    android:windowSoftInputMode="stateUnspecified|adjustPan"
    

    to

    android:windowSoftInputMode="adjustPan"
    

    only in your manifest file

    You should change

    android:windowSoftInputMode="stateUnspecified|adjustPan 
    

    to

    android:windowSoftInputMode="stateHidden|adjustPan"
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.