-4

I want to make an alert dialog that contains a single-choice list item which will help the user choose the theme of the app. I have read on developer's website(Developers Website) how to make the multiple-choice items but I don't quite get how to make the single-choice list item Thanks in Advance.

//MY CODE

public class ThemeDialog extends DialogFragment {

ArrayList mSelectedItems = new ArrayList();  // Track the selected items

    public static ThemeDialog newInstance(){
        ThemeDialog f = new ThemeDialog();
        return f;
    }
    
    //Single-choice Item code
    


}
James Ola
  • 3
  • 3

4 Answers4

0
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());

        dialog.setTitle("Please Select");
        dialog.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
             @Override
            public void onClick(DialogInterface dialog, int which) {
                //Action when cancel is clicked
                dialog.dismiss();
            }
        });

        CharSequence[] cs = new CharSequence[]{"Item-1", "Item-2","Item-3"}; 
        dialog.setSingleChoiceItems(cs, defaultSelectedPosition, new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Action when any item is clicked
                dialog.dismiss();
            }
        });

        return dialog.create();
    }

dialog.setSingleChoiceItems(cs, position, selectItemListener); will create the single choice dialog.

VishnuSP
  • 599
  • 5
  • 16
0

create xml layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorWhite"
android:orientation="vertical">


<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@color/colorPrimary">

</View>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorWhite"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="please select a theme"
        android:textColor="@color/colorRed"
        android:textSize="18sp" />


    <RadioGroup
        android:id="@+id/cancle_booking_radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <RadioButton
            android:id="@+id/theme1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:buttonTint="@color/colorPrimary"
            android:text="theme1" />

        <RadioButton
            android:id="@+id/theme2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:buttonTint="@color/colorPrimary"
            android:text="theme2" />

        <RadioButton
            android:id="@+id/theme3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:buttonTint="@color/colorPrimary"
            android:text="theme3" />

        <RadioButton
            android:id="@+id/theme4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:buttonTint="@color/colorPrimary"
            android:text="theme4" />

        <RadioButton
            android:id="@+id/theme5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:buttonTint="@color/colorPrimary"
            android:text="theme5" />


    </RadioGroup>

</LinearLayout>

now create custom dialog

 final Button btnCancelbooking;
    final TextView tvHideCancelDialog;
    final RadioButton theme1, theme2, theme3, theme4, theme6;
    final Dialog cancelDialog = new Dialog(BookingConfirmClass.this, R.style.DialogSlideAnim);
    //cancelDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  /*use when not set in style file*/
    cancelDialog.setContentView(R.layout.custom_cancel_booking);
    getWindow().setGravity(Gravity.BOTTOM);
    cancelDialog.show();



   theme1 = (RadioButton) cancelDialog.findViewById(R.id.theme1);
    theme2 = (RadioButton) cancelDialog.findViewById(R.id.theme2);
    theme3 = (RadioButton) cancelDialog.findViewById(R.id.theme3);
    theme4 = (RadioButton) cancelDialog.findViewById(R.id.theme4);
    theme6 = (RadioButton) cancelDialog.findViewById(R.id.theme5);


    final RadioGroup radioGroup = (RadioGroup) cancelDialog.findViewById(R.id.cancle_booking_radio_group);


    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            View radioButton = radioGroup.findViewById(checkedId);
            int index = radioGroup.indexOfChild(radioButton);
            switch (index) {
                case 0:
                   //do  your action for theme 1
                    break;
                case 1:
                   //do  your action for theme 2
                    break;
                case 2:
                    //do  your action for theme 3
                    break;
                case 3:
                    //do  your action for theme 4
                    break;
                case 4:
                    //do  your action for theme 5
                    break;
            }
        }
    });
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

Using a final variable obviously won't work (since it can only be assigned once, at declaration time). So-called "global" variables are usually a code smell (especially when they become part of an Activity class, which is usually where AlertDialogs are created). The cleaner solution is to cast the DialogInterface object to an AlertDialog and then call getListView().getCheckedItemPosition(). Like this:

new AlertDialog.Builder(this)
        .setSingleChoiceItems(items, 0, null)
        .setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
                int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
                // Do something useful withe the position of the selected radio button
            }
        })
        .show();

To make it more customize look at this -How to design Single Selection Dialog?

Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
-1

You need to add Radio buttons inside the RadioGroup Tag in ur dialog xml file.

custom_dialog.xml

 <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/RGroup">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Blue Theme"
                android:id="@+id/bluetheme"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Black Theme"
                android:id="@+id/blacktheme"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Brown Theme"
                android:id="@+id/browntheme" />                                        

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Orange Theme"
                android:id="@+id/orangetheme"/>


        </RadioGroup>

Just attach this xml with dialog fragment...this is just simple example..u can use it in ur way also

public class DFragment extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.custom_dialog, container,
                false);
        getDialog().setTitle("DialogFragment Tutorial");        
        // Do something else
        return rootView;
    }
}
Lokesh Desai
  • 2,607
  • 16
  • 28