Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize AlertDialog on the Keyboard display

I have a AlertDialog box with approximately 10 controls (text and TextView) on it. These controls are in a ScrollView with AlertDialog, plus I got 2 buttons positive and negative. The issue I have is when the soft keyboard pops up the two buttons are hidden behind the keyboard.

I was looking for something like redraw function on my inner View or the dialog box. Below is the screen shot of what I am talking about.

enter image description here

like image 249
Sanj Avatar asked Apr 11 '11 13:04

Sanj


2 Answers

If your dialog was an activity using one of the Dialog themes you could effect this behavior by setting the adjustResize flag for the windowSoftInputMode parameter of the activity.

I'm using:

android:windowSoftInputMode="adjustResize|stateHidden"

I think you can still use this flag with regular dialogs, but I'm not sure how to apply it. You may have to create your AlertDialog with a custom theme that inherits the right parent theme and also sets that flag, or you might have to use ContextThemeWrappers and stuff.

Or maybe you can just use Window#setSoftInputMode.

alertDialog.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
like image 166
nmr Avatar answered Oct 15 '22 14:10

nmr


I've found a best way to handle this. Because this is a dialog, So the code

alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

doesn't work very well.

Besides this code, you must set a dialog style for this dialog. The style should like below:

<style name="DialogStyle" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen">
    <item name="android:windowFullscreen">false</item>
    ......
    ......
</style>

NOTICE that the attribute parent is Theme.Black.NoTitleBar.Fullscreen like an activity's style. and the attribute android:windowFullScreen should be false.

Now, the dialog will be resized when the soft keyboard toggles.

like image 18
Nick Avatar answered Oct 15 '22 13:10

Nick