How to resize AlertDialog on the Keyboard display

前端 未结 6 1928
时光取名叫无心
时光取名叫无心 2020-12-05 00:39

I have a AlertDialog box with approximately 10 controls (text and TextView) on it. These controls are in a ScrollView with Alert

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 00:52

    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 :)

提交回复
热议问题