rainman rainman - 2 months ago 29
Android Question

How to change DialogFragment size

I have a dialogFragment in my Android application with some views as you can see in the image below:

enter image description here

I am trying to change its size without success, in the Java class I am trying to do this:

getDialog().getWindow().setLayout(800,800);
getDialog().setTitle("Please tell us");
as you can see in the code below:

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

Log.d(TAG, "onCreateView(LayoutInflaterm ViewGroup, Bundle) - Ini ");

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

ageSpinner = (Spinner) view.findViewById(R.id.age_spinner_dialog);
ageSpinner.setSelected(false);
ageSpinner.setAdapter(populateAgeSpinner());
ageSpinner.setOnItemSelectedListener(this);

radioDialogMale = (RadioButton) view.findViewById(R.id.radioDialogM);
radioDialogMale.setSelected(false);
radioDialogMale.setOnClickListener(this);
radioDialogFemale = (RadioButton) view.findViewById(R.id.radioDialogF);
radioDialogFemale.setSelected(false);
radioDialogFemale.setOnClickListener(this);


okButton = (Button) view.findViewById(R.id.dialog_ok);
okButton.setOnClickListener(this);

getDialog().getWindow().setLayout(800,800);
getDialog().setTitle("Please tell us");

communicator = (LoginActivity) getActivity();

setCancelable(false);

Log.d(TAG, "onCreateView(LayoutInflaterm ViewGroup, Bundle) - Fi ");

return view;
}


but nothing changes.

here is the layout file:

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


<!-- <TextView
android:id="@+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please tell us your age and gender"
android:textSize="22dp"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="#FFF"
android:textStyle="bold"
android:layout_marginBottom="30dp"/> -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="Age"
android:textSize="18dp"
android:gravity="center"/>



<Spinner
android:id="@+id/age_spinner_dialog"
android:layout_width="80dp"
android:spinnerMode="dropdown"
android:layout_height="30dp"
android:layout_marginLeft="18pt"
android:foregroundTint="@color/com_facebook_button_send_background_color"
android:backgroundTint="#FFF"/>


<TextView
android:id="@+id/text_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50pt"
android:text="Gender"
android:textColor="#FFF"
android:textSize="18dp"
android:layout_alignTop="@+id/radioGroup"
android:layout_toEndOf="@+id/age_spinner" />


<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.70"
android:layout_marginEnd="25dp"
android:layout_marginLeft="20dp"
android:id="@+id/radioGroup"
android:layout_alignParentEnd="true">

<RadioButton
android:layout_width="50dp"
android:layout_height="25dp"
android:text="M"
android:layout_marginLeft="12dp"
android:id="@+id/radioDialogM"
android:layout_marginBottom="30dp"
android:textColor="@color/com_facebook_button_send_background_color"
android:backgroundTint="@color/com_facebook_button_send_background_color"
android:buttonTint="@color/com_facebook_button_send_background_color"/>

<RadioButton
android:layout_width="50dp"
android:layout_height="25dp"
android:text="F"
android:id="@+id/radioDialogF"
android:layout_marginRight="5pt"
android:textColor="@color/com_facebook_button_send_background_color"
android:backgroundTint="@color/com_facebook_button_send_background_color"
android:buttonTint="@color/com_facebook_button_send_background_color"/>

</RadioGroup>

/>

<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:id="@+id/dialog_ok"
android:background="@color/com_facebook_button_send_background_color"
android:textColor="#FFF"
android:text="OK"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />

</RelativeLayout>


help please !!!

Answer

You can try to set the width and height for dialog fragment from onResume() method. like below:

public void onResume()
{
    super.onResume();
    Window window = getDialog().getWindow();
    window.setLayout(width, height);
    window.setGravity(Gravity.CENTER);
    //TODO:
}

I found that solution here: Setting the size of a DialogFragment