Parashuram
Parashuram

Reputation: 303

how to add custom views to alertdialog in android?

I am trying to add custom views(chekbox and two radio buttons) as shown in image below in alertdialog but not succeded.

enter image description here

Please suggest me a way to get views as shown in the image.

Thanks in advance!!

Upvotes: 0

Views: 2664

Answers (2)

Volodymyr Yatsykiv
Volodymyr Yatsykiv

Reputation: 3211

Use DialogFragment instead of AlertDialog.

public class CustomDialogFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View viewRoot = inflater.inflate(R.layout.dialog_view, null);

        //do something with your view

        builder.setView(viewRoot);

        return builder.create();
    }
}

R.layout.dialog_view - it's your view, which you want to display

Upvotes: 2

Kuba Łapuszyński
Kuba Łapuszyński

Reputation: 356

You can build dialog with custom layout.

Here's some tutorial how to do that:
http://www.mkyong.com/android/android-custom-dialog-example/

Upvotes: 0

Related Questions