Mohammad
Mohammad

Reputation: 69

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();
    }
}

Upvotes: 2

Views: 2294

Answers (3)

Pztar
Pztar

Reputation: 4749

You can't expect to have match_parent on a child who's parent has wrap_content as a width. So set your main layout width as match_parent Your spinner also needs to be match_parent for width check my example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title Text"
        android:gravity="right"
        android:textSize="20sp"
        android:padding="10dp"/>

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="50dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Second Title"
        android:gravity="right"
        android:textSize="20sp"
        android:padding="10dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"/>

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="Bottom text"
        android:gravity="center"/>

</LinearLayout>

Upvotes: 0

drWisdom
drWisdom

Reputation: 512

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.

Upvotes: 2

Rohit Sharma
Rohit Sharma

Reputation: 2007

Set your parent liner layout height and width to match_parent and try:

      ?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="match_parent"
              android:layout_height="match_parent"

Upvotes: 0

Related Questions