Mohammad Mohammad -3 years ago 177
Android Question

AlertDialog Doesn't wrap Content

My alertDialog doesn't wrap its content,I try many solution but didn't work I changed the style, used Dialog and ...
I want AlertDialog to wraps its content.

I'm getting this

enter image description here

but I want this

enter image description here

custom layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settings_ll"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="5dp"
android:layout_margin="10dp"
>
<TextView
android:id="@+id/font_selection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="انتخاب قلم:"
android:textColor="#000000"
android:padding="10dp"
android:background="#eeeeee"/>
<Spinner
android:id="@+id/font_selection_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
/>
<TextView
android:id="@+id/size_selection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="اندازه قلم"
android:textColor="#000000"
android:padding="10dp"
android:background="#eeeeee"/>
<LinearLayout
android:id="@+id/select_font_size_ll"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/settings_small_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ریز"/>
<Button
android:id="@+id/settings_normal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="معمولی"/>
<Button
android:id="@+id/settings_big_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="درشت"/>
</LinearLayout>
<TextView
android:id="@+id/sample_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_gravity="center"
android:gravity="center"
android:text="این یک جمبه ی نمونه است"
/>



AlertDialog.java:

public class SettingsDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View view = inflater.inflate(R.layout.settings_layout, null);
builder.setView(view);

return builder.create();
}
}

Answer Source

In android dialog usually takes the width of the view with the smallest width. Set your Spinner's layout_width attribute to match_parent and you will get your desired result.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download