Ksenia
Ksenia

Reputation: 3731

Adjusting android dialog size

I created DialogFragment with custom view, inflated by LayoutInflater :

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        return new AlertDialog.Builder(getActivity(), R.style.MyDialogTheme)
                .setView(inflater.inflate(R.layout.used_libraries, null))
                .setPositiveButton(R.string.close, null)
                .create();
}

and I expected that it will occupy only part of screen, as usual DialogFragment does. But instead it occupies almost full screen in height and full screen in width:

enter image description here

The reason seems to be in custom style:

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <!--item RadioButton or CheckBox color-->
        <item name="colorControlNormal">@android:color/white</item>
        <item name="colorControlActivated">@color/colorAccent</item>
        <!--item text color-->
        <item name="android:textColorAlertDialogListItem">@android:color/white</item>
        <!--buttons color-->
        <item name="colorAccent">@color/colorAccent</item>
        <!--title and message color-->
        <item name="android:textColorPrimary">@android:color/white</item>
        <!--dialog background-->
        <item name="android:windowBackground">@color/colorPrimary</item>
        <!--toolbar overflow popup background-->
        <item name="android:background">@color/colorPrimary</item>

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

But how to make this dialog to have size that equals to default android dialog size (approximately 70% of the width), rather than to occupy almost full screen height?

Upvotes: 0

Views: 1314

Answers (1)

B.Cakir
B.Cakir

Reputation: 607

Use this to set the minimum width.

<item name="android:windowMinWidthMajor">70%</item>
<item name="android:windowMinWidthMinor">70%</item>

There is no explicit way to set a maximum, as the dialog is designed to be as wide as is needed to (i.e. the default of the dialog is set to "wrap_content"). You could try to set the width of your views and make it smaller through a custom layout, so that it fits inside the dialog, but there is no guarentee that it will be 70% on smaller screens.

Upvotes: 3

Related Questions