0

I am using RadioGroup with two radiobuttons in it. I want to show a a dialog popup with a button when a radiobutton is clicked but without checking the radiobutton and get it checked only when the button inside the dialog is pressed. Using onCheckedChangeListener() already checks the radiobutton. I am trying to show the preview of theme before user applies it. How I can handle this situation? Here is the xml just in case its needed:

<RadioGroup
    android:id="@+id/checkButtonGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkedButton="@+id/first"
    android:orientation="horizontal">
    <RadioButton
        android:id="@+id/first"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="100dp"
        android:background="@drawable/theme_light_selector"
        android:layout_margin="10dp"
        android:button="@null"
        android:text="Radio 1"/>
    <RadioButton
        android:id="@+id/second"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:background="@drawable/theme_light_selector"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:button="@null"
        android:text="Radio 2"/>
</RadioGroup>

1 Answers1

0

Use setOnClickListener() on Radiobutton for this purpose, like i implement below for one of radio button.

 RadioButton first= findViewById(R.id.first);
    first.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(Testing.this)
                    .setTitle("Change theme?")
                    .setMessage("Are you sure you want to change theme of button?")

                    // Specifying a listener allows you to take an action before dismissing the dialog.
                    // The dialog is automatically dismissed when a dialog button is clicked.
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                        first.setChecked (true);
                        first.setBackground (getDrawable (R.drawable.your_no_theme_name));

                        }
                    })


                    .setNegativeButton (android.R.string.no, new DialogInterface.OnClickListener () {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            first.setChecked (false);
                            first.setBackground (getDrawable (R.drawable.your_yes_theme_name));
                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();
        }
    });
Ghulam Qadir
  • 471
  • 3
  • 12