DialogFragment not resizing when keyboard shown
Asked Answered
F

10

30

I'm trying to use a SherlockDialogFragment to ask some input from the user. Everything works fine on my phone (Galaxy Nexus, 4.2), but on a smaller phone (emulator 2.3.3), when the keyboard shows up, it covers the two buttons of the DialogFragment, like this:

dialog covered by keyboard

My layout is inside a ScrollView, and I'm changing the softInputMode to SOFT_INPUT_ADJUST_RESIZE on my onViewCreated. I also tried SOFT_INPUT_ADJUST_PAN, and it didn't work

MyCustomDialog.java

public class AddTaskDialog extends SherlockDialogFragment implements OnDateSetListener{
//...
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        this.inflater =  getActivity().getLayoutInflater();
        View mainView =inflater.inflate(R.layout.custom_dialog, null);
        builder.setView(mainView);
        this.taskNote = (EditText) mainView.findViewById(R.id.ET_taskNote);
        this.taskText = (EditText) mainView.findViewById(R.id.ET_taskText);
        this.taskValue = (EditText) mainView.findViewById(R.id.ET_taskValue);
        /*
         * Other stuff
         */
        builder.setTitle(getString(R.string.new_task, hType.toString()))
               .setPositiveButton(R.string.dialog_confirm_button, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                    //...
                    }
               })
               .setNegativeButton(R.string.dialog_cancel_button, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

And here is my layout:

custom_dialog.xml

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@color/abs__background_holo_light">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin">
        <TextView
            android:id="@+id/TV_taskText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/task_text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <EditText
            android:id="@+id/ET_taskText"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="@string/create_task_hint"
            android:inputType="textNoSuggestions"
            android:singleLine="true" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin" >
        <TextView
            android:id="@+id/TV_taskNote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/task_note"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/ET_taskNote"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:minLines="2"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textMultiLine"
            android:hint="@string/task_note_hint">

        </EditText>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/repeat_days"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_gravity="top"
        android:orientation="horizontal"
        android:visibility="gone"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin">
        <!-- Day buttons are put here programatically -->
    </LinearLayout>
</LinearLayout>

So, could you help me, and guide me on how to show those buttons? Either to PAN the view or let it resize...

Frederico answered 2/6, 2013 at 17:21 Comment(0)
U
32

Set the windowSoftInputMode property to adjustNothing in the AndroidManifest.xml of the activities that use the dialog fragment.

<activity
    ...
    android:windowSoftInputMode="adjustNothing">
...

and onCreateDialog to hide the soft input:

...
Dialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
return dialog;
}

FYI: https://developer.android.com/training/keyboard-input/visibility.html#ShowOnStart

Unofficial answered 25/6, 2013 at 16:46 Comment(3)
Since it was a DialogFragment, it didn't have any associated activity in the manifest, so the first point is useless, but the other one fixed it. Thanks.Frederico
I edited my answer to be more specific about edition of the manifest code.Entrench
Why without dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); dialog is not re-sized? This looks like bug.Applewhite
O
51

I just use the following line in my DialogFragment:

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

And nothing else, see here full example:

    public class TextEditor extends DialogFragment {

    public TextEditor () {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_text_editor, container);

        //set to adjust screen height automatically, when soft keyboard appears on screen 
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        //[add more custom code...]
        return view;
    }
}
Omasum answered 17/10, 2014 at 22:45 Comment(1)
For me it was crititcal to call setSoftInputMode() from onCreateView() and nowhere else, for example calling it from onCreateDialog() did not workAntoninus
U
32

Set the windowSoftInputMode property to adjustNothing in the AndroidManifest.xml of the activities that use the dialog fragment.

<activity
    ...
    android:windowSoftInputMode="adjustNothing">
...

and onCreateDialog to hide the soft input:

...
Dialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
return dialog;
}

FYI: https://developer.android.com/training/keyboard-input/visibility.html#ShowOnStart

Unofficial answered 25/6, 2013 at 16:46 Comment(3)
Since it was a DialogFragment, it didn't have any associated activity in the manifest, so the first point is useless, but the other one fixed it. Thanks.Frederico
I edited my answer to be more specific about edition of the manifest code.Entrench
Why without dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); dialog is not re-sized? This looks like bug.Applewhite
Y
11

Be sure that the layout is inside a scroll view:

<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  -->your layout here 
</ScrollView>

and follow Dirk comment:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_text_editor, container);

//add this line 
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

      //[add more custom code...]
      return view;
    }
Yielding answered 29/3, 2016 at 21:39 Comment(2)
Absolutely the ONLY thing that worked for me when trying to get the SOFT_INPUT_ADJUST_PAN behavior.Naval
Yeah this should be the accepted answer thanks @YieldingPhosphocreatine
L
9

Even though it is a little late for a reply, since the question is in DialogFragment, the following code solves my issue.

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    // Setting STYLE_NO_FRAME allows popup dialog fragment to resize after keyboard is shown
    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.theme_popupdialog_style);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setCanceledOnTouchOutside(false);

    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    return dialog;
}

As for the style theme, I applied the following code

/** must put parent="@android:style/Theme.Dialog for it to work */
<style name="theme_popupdialog_style" parent="@android:style/Theme.Dialog">
    <item .... >...</item>
</style>
Lawless answered 3/5, 2017 at 10:43 Comment(0)
E
9

If someone is having similar problem for BottomSheetDialog. This solution works like a charm.

Inside styles:

<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowSoftInputMode">adjustResize</item>
    <item name="android:statusBarColor">@android:color/transparent</item> 
</style>

android:windowIsFloating should be false

android:windowSoftInputMode must be adjustResize.

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/rounded_corner_dialog</item>
</style>

Wrap layout inside NestedScrollView

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

     <--Rest of the layout-->
</androidx.core.widget.NestedScrollView>

On some devices this solution wasn't enough. Adding this to code solved my problem completely.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog?.setOnShowListener {
        val dialog = it as BottomSheetDialog
        val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
        bottomSheet?.let { sheet ->
            dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
            sheet.parent.parent.requestLayout()
        }
    }
}
Electroencephalograph answered 17/6, 2019 at 13:8 Comment(1)
thanks man, after wasting two days on trying different things , you solution worked for me but only first two steps. I did'nt need any nested scroolviewAbbacy
P
7

This can also be caused by:

<item name="android:windowTranslucentStatus">true</item>

Try to remove it from your theme.

Pomatum answered 18/1, 2018 at 18:56 Comment(4)
do you know why is it?Genie
I think it's a bug.Pomatum
Thanks. Btw, do you know ho to "fix" it with transluent status enabled?Inoue
I had the same issue. I used <item name="android:statusBarColor">#someTranslucentColor</item> as a workaround. It seems to work fine with fitssystemwindows and stuffBordelon
I
5

Apart the changes mentioned in other answers also check the theme of dialogfragment
From my experiments, "android:windowIsFloating" attribute seems to affect how the window reacts to the soft input.

It you set this to false, the window won't slide up when the keyboard becomes visible.

Indiscipline answered 23/3, 2017 at 7:24 Comment(1)
This did the magic, after spending so many hours, this answer saved the day!Royceroyd
A
4

To work properly with AutoCompleteTextView DialogFragment must not be set as FullScreen. Instead, you can set width as match_parent in your style. Below example code:

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
    return inflater.inflate(R.layout.dialog_filter, container)
}

override fun getTheme(): Int {
    return R.style.AlertDialog_FullWidth
}

Styles:

<style name="AlertDialog" parent="android:Theme.Dialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.8</item>
    <item name="android:windowAnimationStyle">@style/PauseDialogAnimation</item>
</style>

<style name="AlertDialog.FullWidth" parent="AlertDialog">
    <item name="android:layout_width">match_parent</item>
    <item name="android:windowIsFloating">false</item>
</style>
Aime answered 18/9, 2020 at 9:13 Comment(0)
J
0

As already mentioned, android:windowSoftInputMode="adjustResize" and dialog.getWindow().setSoftInputMode(WIndowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); are the right way of doing this.

BUT. If your view is not resizeable at all, then your buttons in the bottom will still be hidden. In my case, this hack was enough:

KEYBOARD HIDDEN

I set android:layout_weight for top views, so that when keyboard opens and dialog is resized — top views would be hidden: KEYBOARD SHOWN

Jessamyn answered 13/8, 2018 at 11:36 Comment(3)
Hi @soshial, I know this is very old but the dialog crushes the content when I try this!Moorfowl
Yes, content gets minimized — this is the only quick workaround that I found working.Jessamyn
I'm using the MaterialDialog library ;)Moorfowl
R
-1

For DialogFragment, it seems that applying SoftInputMode only works if it setted from inside the DialogFragment class, not the caller class:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);    

}

Also, for onStart method, I added the following to expand dialog layout horizontally:

@Override
public void onStart() {
    super.onStart();
    getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
Reddin answered 1/6, 2018 at 3:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.