简体   繁体   中英

How to stop alert dialog from shifting upwards when keyboard is shown

I have ran into some issue with android. The situation is currently, when I tap the editText field in the alert dialog, the dialog will shift up to make way for the keyboard. However, I have calculated that there is enough space between my dialog and the keyboard. The reason behind is to have the dialog box hide the content that is underneath it.

Question

Is there a way to stop the alert dialog box from shifting upwards ?

Add or change in AndroidManifest.xml for that activity tag.

<activity
        android:name=".YourActivity"
        android:windowSoftInputMode="adjustNothing" />

As long as you always need to show the keyboard immediately once the dialog opens rather than once a specific form widget inside gets focus (for instance, if your dialog just shows an EditText and a button), you can do the following:

    AlertDialog alertToShow = alert.create();
    alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    alertToShow.show();

Rather than calling .show() on your builder immediately, you can instead call .create() which allows you to do some extra processing on it before you display it onto the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM