murty murty - 5 months ago 13
Android Question

How can I change AlertDialog width and how can I close it when clicked out of alertdialog?

I want to change width of this DialogFragment and also I want to close it when I clicked outside of dialogbox.

Here is my code

public class MyDialog extends DialogFragment {
LayoutInflater inflater;
View v;


@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

inflater = getActivity().getLayoutInflater();

v = inflater.inflate(R.layout.custom,null);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v).setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});



return builder.create();


}
}


and here is my custom.xml and here is my custom.xml
vand here is my custom.xml

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

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"

xmlns:android="http://schemas.android.com/apk/res/android"
android:fillViewport="false">



<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/grafikler"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#ffffff"
android:baselineAligned="false">




<ImageView
android:layout_width="50dp"
android:src="@drawable/pozitif"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView3"
android:layout_toEndOf="@+id/imageView3" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/negatif"
android:id="@+id/imageView3"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/duz"
android:id="@+id/imageView10"
android:layout_below="@+id/imageView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/oo_golge"
android:id="@+id/imageView12"
android:layout_below="@+id/imageView10"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/os_golge"
android:id="@+id/imageView13"
android:layout_below="@+id/imageView12"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/tg_gunes"
android:id="@+id/imageView14"
android:layout_below="@+id/imageView13"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/tg_golge"
android:id="@+id/imageView15"
android:layout_below="@+id/imageView14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/tas"
android:id="@+id/imageView16"
android:layout_below="@+id/imageView15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/expres"
android:id="@+id/imageView17"
android:layout_below="@+id/imageView16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/expres2"
android:id="@+id/imageView18"
android:layout_below="@+id/imageView17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/expres3"
android:id="@+id/imageView19"
android:layout_below="@+id/imageView18"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/exprescok"
android:id="@+id/imageView20"
android:layout_below="@+id/imageView19"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Negatif Yüzey"
android:id="@+id/textView"
android:layout_above="@+id/imageView3"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Pozitif Yüzey"
android:id="@+id/textView2"
android:layout_above="@+id/imageView10"
android:layout_toRightOf="@+id/imageView3"
android:layout_toEndOf="@+id/imageView3" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Düz Yüzey"
android:id="@+id/textView5"
android:layout_above="@+id/imageView12"
android:layout_toRightOf="@+id/imageView10"
android:layout_toEndOf="@+id/imageView10" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Öğleden Önce Gölge"
android:id="@+id/textView6"
android:layout_above="@+id/imageView13"
android:layout_toRightOf="@+id/imageView12"
android:layout_toEndOf="@+id/imageView12" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Öğleden Sonra Gölge"
android:id="@+id/textView7"
android:layout_above="@+id/imageView14"
android:layout_toRightOf="@+id/imageView13"
android:layout_toEndOf="@+id/imageView13" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Tüm Gün Güneş"
android:id="@+id/textView8"
android:layout_above="@+id/imageView15"
android:layout_toRightOf="@+id/imageView14"
android:layout_toEndOf="@+id/imageView14" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Tüm Gün Gölge"
android:id="@+id/textView9"
android:layout_above="@+id/imageView16"
android:layout_toRightOf="@+id/imageView15"
android:layout_toEndOf="@+id/imageView15" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Taş Düşme Riski"
android:id="@+id/textView10"
android:layout_above="@+id/imageView17"
android:layout_toRightOf="@+id/imageView16"
android:layout_toEndOf="@+id/imageView16" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="5-10 Express"
android:id="@+id/textView11"
android:layout_above="@+id/imageView18"
android:layout_toRightOf="@+id/imageView17"
android:layout_toEndOf="@+id/imageView17" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="10-20 Express"
android:id="@+id/textView12"
android:layout_above="@+id/imageView19"
android:layout_toRightOf="@+id/imageView18"
android:layout_toEndOf="@+id/imageView18" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="20-30 Express"
android:id="@+id/textView13"
android:layout_above="@+id/imageView20"
android:layout_toRightOf="@+id/imageView19"
android:layout_toEndOf="@+id/imageView19" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="30-40 Express"
android:id="@+id/textView14"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/imageView20"
android:layout_toEndOf="@+id/imageView20" />

</RelativeLayout>
</ScrollView>

Answer

To change the width of the dialog, you can override onStart() method of the MyDialog class.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);    
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    return dialog;
}

@Override
public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null) {
        int width = CUSTOM_WIDTH;
        int height = CUSTOM_HEIGHT;
        dialog.getWindow().setLayout(width, height);
    }
}

To dismiss dialog when clicking outside, please use this code: DialogFragment.getDialog().setCanceledOnTouchOutside(true);

Must be called in onCreateView.

Hope this code will be of help to you

Comments