Search code examples
androidandroid-alertdialogandroid-radiobuttonandroid-radiogroup

Get value of radio button in custom alert dialog


I have created a custom alertdialog with a radiogroup, radiobuttons, and edittext.

The goal is to trying to get the text value from the selected radiobutton and the edittext. At this point, I can only get the value entered in the edittext.

I have seen the following link and tried to adjust the code to adapt, but it seems that the code still doesn't work.

Android getting value from selected radiobutton

http://www.mkyong.com/android/android-radio-buttons-example/

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<RadioGroup
    android:id="@+id/radioPersonGroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RadioButton
        android:id="@+id/gButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_weight="1"
        android:text="G" />
    <RadioButton
        android:id="@+id/kButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_weight="1"
        android:text="K" />

</RadioGroup>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="16dp"
    android:layout_weight="1"
    android:ems="10"
    android:hint="$10.00"
    android:inputType="numberDecimal" />
</LinearLayout>

java file

private RadioButton radioSelectedButton;
...

FloatingActionButton fab = findViewById(R.id.fab);
final RadioGroup group = findViewById(R.id.radioPersonGroup);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        final View dialogView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
        builder.setTitle("Who Paid")
            .setView(dialogView)
            .setCancelable(false)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    int selectedId = group.getCheckedRadioButtonId();
                    radioSelectedButton = (RadioButton) findViewById(selectedId);
                    Toast.makeText(MainActivity.this,
            radioSelectedButton.getText(), Toast.LENGTH_SHORT).show();

                            EditText input = dialogView.findViewById(R.id.editText2);
                            Toast.makeText(MainActivity.this,input.getText().toString(), Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.cancel();
                        }
                    });

            AlertDialog alert = builder.create();
            alert.show();
        }
    });

When I click on either buttons followed by the Ok to submit the dialog the following exception occurs.

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference

enter image description here


Solution

  • You're getting a NullPointerException in this line

    int selectedId = group.getCheckedRadioButtonId();
    

    because you tried to "find" the RadioGroup in the wrong View when you wrote

    final RadioGroup group = findViewById(R.id.radioPersonGroup);
    

    The RadioGroup is part of the Dialog, so you need to look for it in the View tree of dialogView:

    // inside onClick()
    final View dialogView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
    final RadioGroup group = dialogView.findViewById(R.id.radioPersonGroup);
    

    Similarly, you need to "find" selectedRadioButton in a ViewGroup which contains it. e.g.

    radioSelectedButton = (RadioButton) dialogView.findViewById(selectedId);
    

    or

    radioSelectedButton = (RadioButton) group.findViewById(selectedId);