40

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

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Sanj
  • 3,770
  • 6
  • 26
  • 31

7 Answers7

118

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);
apricot
  • 3,599
  • 3
  • 19
  • 20
nmr
  • 16,625
  • 10
  • 53
  • 67
  • 9
    The last line works for me, ideally i suppose that was the question here and this is the correct answer. The selected answer is more of a design change suggestion than a correct answer. +1 for the answer – Jayshil Dave Jan 03 '12 at 16:43
  • 7
    if you have a custom style, you can also make it an element of the style definition `` – ChristophK Aug 17 '12 at 13:37
  • 1
    This answer should've been the accepted one. The last line of code did the trick for me. Thanks a lot ! – Bogdan Zurac Apr 18 '13 at 10:59
  • 1
    Works with FragmentDialog too! – Bojan Radivojevic Aug 30 '13 at 13:16
  • A million times this. You need to set the SoftInputMode of the Dialog itself. – Eugen Timm Jan 19 '15 at 17:10
  • The last line of code did the trick, I tried doing it on the Activity's onCreate and seting it on the manifest, but I guess the do not share the same window. – Eric Van Der Dijs Oct 21 '20 at 22:49
18

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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Nick
  • 1,614
  • 1
  • 12
  • 10
  • 1
    this worked for me but I used `parent="Theme.AppCompat.Light.Dialog.Alert"` but setting false to `"android:windowFullscreen"` did do the trick...thanks. – CrandellWS Sep 18 '19 at 16:50
4

Nothing worked for me except adjustPan

as per the documentation

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

So just simply use it in your onCreate() or onCreateView() method like:

getDialog().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

Or simply put android:windowSoftInputMode="adjustPan" in manifest for the Activiry in which we are playing with dialogs

and use android:windowSoftInputMode="adjustResize|stateHidden" in each edittext which will help the user to navigate to next textbox easily.

Point to remember

Never use MATCH_PARENT to make the dialog full screen as adjustPan will not work here. If anyone wants to make the dialog to fit the screen, just use points till 0.96 (not more than this) for the height, so the keyboard will properly reach to the edittext. I did like below :

@Override
public void onStart()
{
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null)
    {
        //int height = ViewGroup.LayoutParams.MATCH_PARENT;
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        //int width = (int)(size.x * 0.96);
        int h = (int)(size.y * 0.96);

        dialog.getWindow().setLayout(width, h);
    }
}

Look, If I will use the total height (MATCH_PARENT) then soft_keyboard will squize the dialog. But if I will use points for the height (here 0.96 which is almost near to match_parent), then it will properly work.

Hope it will help someone :)

Ranjit
  • 5,130
  • 3
  • 30
  • 66
2

maybe you don't need to resize Dialog

add android:imeOptions="actionNext" to EditText(all but last) (it will add "Next" button to the keyboard - go to next EditText)

and add android:imeOptions="actionDone" to last EditText ("Done" button - hide keyboard)

now user should be able to click buttons

if you're creating textboxes in code use EditText#setImeOptions function

HTH

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Selvin
  • 6,598
  • 3
  • 37
  • 43
  • 2
    Thanks for the info but that is not quite how the user will behave in this case. That is they have the freedom to only fill one or more of the textbox not all – Sanj Apr 11 '11 at 14:29
0

to show keyboard immediately and adjust size:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        }
    }
});
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Samad
  • 1,776
  • 2
  • 20
  • 35
0

Are you forced to have it as a popup? The popup looks so large, that you may just want to have it as a separate activity. In general, popups are used to provide a brief question or statement with a few options, not a full blown data entry form. Since you can't see much behind the large popup, you're not exposing any underlying controls anyways.

Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
  • 3
    While your answer sounds true to me, it doesn't address edge cases. What about a medium sized Dialog on a tablet? – nmr Sep 01 '11 at 16:47
  • True, I was thinking handheld, given the screenshot. If you're doing a seperate UI for a tablet, I suppose that a medium sized dialog for a tablet may work, but I think in general, a Dialog should be used for very small data entry, and notifications only. If you're trying to make something a form, have it as a seperate activity. – Stealth Rabbi Sep 06 '11 at 14:28
  • 4
    But this does not answer the question. – ViliusK Jun 06 '13 at 08:15
  • 1
    @ViliusK I know it doesn't answer his exact question, but I offered an alternative UI suggestion that makes things cleaner for the user and easier to implement. If he did not like the suggestion, then it's up to him as the poster to downvote or ignore it. – Stealth Rabbi Jun 06 '13 at 12:33
0

To those who are in the same situation as me.

in my case, the problem was activity having these attributes in style

<style name="SomeStyleName">
   <item name="android:windowTranslucentStatus">true</item>
   <item name="android:windowTranslucentNavigation">true</item>
</style>

If windowTranslucentStatus and windowTranslucentNavigation both are true, the keyboard came up as it overlay dialog.

So I override those values to false, only for materialAlertDialog. (maybe AlertDialog or Dialog in your case)

<style name="SomeStyleName">
   <item name="android:windowTranslucentStatus">true</item>
   <item name="android:windowTranslucentNavigation">true</item>
   <item name="materialAlertDialogTheme">@style/TranslucentMaterialAlertDialogTheme</item>
</style>

<style name="TranslucentMaterialAlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
   <item name="android:windowTranslucentStatus">false</item>
   <item name="android:windowTranslucentNavigation">false</item>
</style>
yeonseok.seo
  • 2,696
  • 3
  • 13
  • 14