Custom dialog too small
Asked Answered
P

11

23

I have an android activity that implements a custom dialog.The application is running ok but the dialog is too small,i want to display a bigger dialog.How can i achieve this? Here is my layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >

         <LinearLayout 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical"
         android:background="@drawable/view_more_borders">

         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:orientation="horizontal"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">

             <TextView
                 android:id="@+id/share_amount"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Company Name:"
                 android:textAppearance="?android:attr/textAppearanceSmall"
                 android:textColor="@color/nice_blue"
                 android:typeface="sans" />

            <TextView
                android:id="@+id/textViewCompanyName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.80"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceSmall" />

         </LinearLayout>


         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">

             <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Price per share:"

                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/nice_blue" />

            <TextView
                android:id="@+id/textViewprice_pershare"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:layout_alignBaseline="@+id/Yesterday"
                android:layout_alignBottom="@+id/Yesterday"
                android:layout_marginLeft="38dp"
                android:fontFamily="sans-serif"
                android:layout_toRightOf="@+id/company"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceSmall" />
         </LinearLayout>

         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">

              <TextView
                    android:id="@+id/textView4"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="Total cost:"

                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:textColor="@color/nice_blue" />

            <TextView
                android:id="@+id/textview_totalcost"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:layout_alignBaseline="@+id/Yesterday"
                android:layout_alignBottom="@+id/Yesterday"
                android:layout_marginLeft="38dp"
                android:fontFamily="sans-serif"
                android:layout_toRightOf="@+id/company"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceSmall" />
         </LinearLayout>


         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">

                 <TextView
            android:id="@+id/textView3"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Number of shares:"

                     android:textAppearance="?android:attr/textAppearanceSmall"
                     android:textColor="@color/nice_blue" />

            <EditText
                android:id="@+id/shareNumber"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:fontFamily="sans-serif"
                android:textSize="12sp"
                 >

            <requestFocus />
            </EditText>
         </LinearLayout>

         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">


              <TextView
        android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Payment method:"

                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/nice_blue" />

             <Spinner
                 android:id="@+id/spinner_paymentmode"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="2" />

         </LinearLayout>   
     </LinearLayout>
     <Button
    android:id="@+id/button_buy_shares"
    style="@style/ButtonText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/blue_button"
    android:text="Buy" />

And here is my output.I want to increase the width and height of the dialog. enter image description here

And my java file

public class MyDialogFragment extends DialogFragment {

public MyDialogFragment() {

}

private EditText mEditText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialogfragment, container);
    // mEditText = (EditText) view.findViewById(R.id.txt_your_name);
    // getDialog().setTitle("Hello");
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

    return view;
}
}
Peppel answered 2/10, 2013 at 9:27 Comment(6)
Post your java file. you have to set width and height of your dialog in java file.Transposal
@Andrain i have posted it.Peppel
@mungaihkamau did you resolved your problem?Recombination
@JaAd no,am still trying to figure out something from googlePeppel
@mungaihkamau try again my piece of code.Recombination
Add RelativeLayout as the root element for your custom dialog layout, It worked for me!Archie
R
58

Change your dialog dimension on runtime:

yourDialog.show();
yourDialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT);

You can do that for both dimension, in my example i only changed the width. Hope it helps!

EDIT

I forgot to mention where i took width:

DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

EDIT 2

Try this code:

Dialog yourDialog = dialogFragment.getDialog();
yourDialog.getWindow().setLayout((6 * width)/7, (4 * height)/5);
Recombination answered 2/10, 2013 at 9:34 Comment(6)
how have you you initialised width?Peppel
@Chris the code simply "tells" to the dialog to take 6/7 of your screen width and 4/5 of your screehn height.Recombination
but if im am giving match parent as width, still the issue why?Punnet
@ParthAnjaria sorry, I didn't noticed your comment. I don't know, but maybe it is not allowed anymore to have a dialog with this parameter. I think that even if this is useful, having a big dialog is not a good solution (design). Even if my solution is working, maybe it is not perfect anymore. If you should show a lot of info, then re-think it and use a Fragment or an Activity.Recombination
This is it. Thank you! Was using getActivity().getWindow() instead of getDialog().getWindow() and note this must be called in onStart() when the ui is ready and not in onCreate().Antipole
Just an additional information, if you want the height to wrap content, you can use dialog.getWindow().setLayout(yourWidth, WindowManager.LayoutParams.WRAP_CONTENT) .Uneventful
C
21

Setting android:minWidth and android:minHeight in the custom layout does the trick for me.

Condominium answered 25/2, 2015 at 8:39 Comment(0)
M
9

put below line in onCreate

setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);

original answer

Mcwherter answered 5/6, 2017 at 10:15 Comment(1)
Yes, it works, but 1) dialog expands to whole width, 2) this theme is deprecated, you may use Theme_DeviceDefault_Dialog_MinWidth, Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth or any material. Also setting style or android:theme in layout doesn't solve, so we should use this method setStyle().Unapproachable
P
6

Dialogs are sized by their content. You can add padding and margins on the outer layout to consume more space, but that will not redistribute the views inside.

Polygynist answered 2/10, 2013 at 9:32 Comment(1)
This is the best explanation! In your custom layout you can't use match parent, because it's always wrap_content used by Dialog itselfMeagre
V
6
<style name="full_screen_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:minWidth" type="dimen">600dp</item>
</style>

Use this in style.xml file and use this style in dialog class

Valdes answered 14/3, 2014 at 5:49 Comment(1)
the only thing that works for me here is the minWidth=600dp. In my case I didn't need the other style elementsAccepted
J
3

The right solution is to override the onCreateDialog method rather than onCreateView, and create your dialog with a AlertDialog.Builder, as explained in the doc: http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog

Juvenescent answered 18/4, 2016 at 7:0 Comment(1)
If DialogFragment doesn't use AlertDialog.Builder, it won't help.Unapproachable
C
3

Add android:minWidth / android:minHeight to your root view. See example below:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="300dp"
android:minHeight="500dp">
Conservation answered 18/5, 2017 at 1:20 Comment(0)
H
1

The trick worked for me

  override fun onStart() {
        super.onStart()
        val metrics: DisplayMetrics = resources.displayMetrics
        val width: Int = metrics.widthPixels
        dialog?.window?.setLayout(width - 58, 
        ViewGroup.LayoutParams.WRAP_CONTENT)
    }
Hirsute answered 24/7, 2021 at 19:34 Comment(0)
T
0

Try this

For setting dialog width and height as per device screen size

    Display display;
    int DisplayWidth, DisplayHeight, DialogWidth, DialogHeight;
    Dialog dialog;

display =((WindowManager)activity_context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayWidth = display.getWidth();
        DisplayHeight = display.getHeight();

        if(DisplayHeight > DisplayWidth)
        {
            DialogWidth = (6 * DisplayWidth) / 7 ;
            DialogHeight = (4 * DisplayHeight) / 5 ;
        }
        else
        {
            DialogWidth = (6 * DisplayWidth) / 9 ;
            DialogHeight = (4 * DisplayHeight) / 5 ;
        }

        dialog = new Dialog(activity_context);

          // Set your dialog width and height dynamically as per your screen.

        Window window = dialog.getWindow();
        window.setLayout(DialogWidth,DialogHeight);
        window.setGravity(Gravity.CENTER);

        dialog.show();
Transposal answered 2/10, 2013 at 9:36 Comment(1)
would you be willing to assist me edit my code as per your solution?Peppel
E
0

try to set android:layout_height to some value for base LinearLayout

for example

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"

<!-- right here -->
    android:layout_height="100dp"

android:background="@color/white"
android:orientation="vertical" >
Expert answered 17/1, 2016 at 22:22 Comment(0)
S
0

I configured my project so that the default dialog theme for DialogFragments has a minimum width.

set the app's theme to your custom theme in the AndroidManifest.xml....alternatively, set the theme of the Activity hosting the DialogFragment to your app's custom theme.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld">

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">     <!-- set the app's theme to your custom theme -->

        .....

    </application>

</manifest>

define your custom theme in values/styles.xml, and override the default theme used for dialog fragments with one you define yourself.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="Theme.AppCompat">

        <!-- override the default theme for DialogFragments -->
        <item name="android:dialogTheme">@style/AppTheme.Dialog</item>

    </style>

    <!--
        configure your custom theme for DialogFragments...
        use a theme that has MinWidth, so that the dialog is not "too small"
    -->
    <style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog.MinWidth">

        <!-- override the default theme for DialogFragments spawned by this DialogFragment -->
        <item name="android:dialogTheme">@style/AppTheme.Dialog</item>

        <!--
            OPTIONAL: override the background for the dialog...i am using a dark theme,
            and for some reason, there is no themes for dialogs with dark backgrounds,
            so, i made my own.
        -->
        <item name="android:windowBackground">@drawable/dialog__window_background</item>

        <!--
            add the title to the dialog's theme. you can remove it later by using
            DialogFragment.setStyle()
        -->
        <item name="android:windowNoTitle">false</item>
        <item name="windowNoTitle">?android:windowNoTitle</item>

    </style>

    .....

</resources>

if you use a dark theme, and overrode android:windowBackground like i did in AppTheme.Dialog, then add a drawable/dialog__window_background.xml file with the contents:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="16dp"
    android:insetTop="16dp"
    android:insetRight="16dp"
    android:insetBottom="16dp">
    <shape android:shape="rectangle">
        <corners android:radius="?dialogCornerRadius" />
        <solid android:color="?android:colorBackground" />
    </shape>
</inset>
Shawndashawnee answered 21/11, 2020 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.