How to Run Android Radio Group on Java

26 Views Asked by At

I want to use Radio Group with buttons on Android studio

I'm developing an Android application where I need to capture user input using RadioGroup within an AlertDialog. Below is the code snippet I'm using:

import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    Button bt;
    RadioGroup g1;
    RadioGroup g2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt = findViewById(R.id.button);
        g1 = findViewById(R.id.radioGroup);
        g2 = findViewById(R.id.radioGroup2);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
                alertDialogBuilder.setTitle("Alert");
                alertDialogBuilder.setMessage("Click Yes to Submit").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int ans1 = g1.getCheckedRadioButtonId();
                        int ans2 = g2.getCheckedRadioButtonId();

                        String selectedOptionQ1 = "";
                        String selectedOptionQ2 = "";

                        if(ans1 != -1) {
                            View selectedRadioButtonViewQ1 = g1.findViewById(ans1);
                            if(selectedRadioButtonViewQ1 != null && selectedRadioButtonViewQ1 instanceof RadioButton) {
                                selectedOptionQ1 = ((RadioButton) selectedRadioButtonViewQ1).getText().toString();
                            }
                        }

                        if(ans2 != -1) {
                            View selectedRadioButtonViewQ2 = g2.findViewById(ans2);
                            if(selectedRadioButtonViewQ2 != null && selectedRadioButtonViewQ2 instanceof RadioButton) {
                                selectedOptionQ2 = ((RadioButton) selectedRadioButtonViewQ2).getText().toString();
                            }
                        }

                        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                        intent.putExtra("selectedOptionQ1", selectedOptionQ1);
                        intent.putExtra("selectedOptionQ2", selectedOptionQ2);
                        startActivity(intent);
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
    }
}

I have an AlertDialog in this code with two RadioGroup instances (g1 and g2) containing multiple RadioButton options each. Upon clicking the "Yes" button in the dialog, I want to capture the selected options from both RadioGroups and pass them to another activity using intent.

The code seems to work fine, but I'm wondering if there's a more efficient or concise way to handle the selection of radio buttons within a RadioGroup in Android. Are there any best practices or alternative methods I should consider for achieving this functionality? Any insights or suggestions would be greatly appreciated!

2

There are 2 best solutions below

0
Harry Raj On

Main Activity 2

package com.example.lab4q1;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        tv = (TextView)findViewById(R.id.textViewRes);

        String selectedOptionQ1 = getIntent().getStringExtra("selectedOptionQ1");
        String selectedOptionQ2 = getIntent().getStringExtra("selectedOptionQ2");

        int s=0;
        if("Option 1".equals(selectedOptionQ1))
        {
            s = s + 1;
        }
        if("Option 2".equals(selectedOptionQ2))
        {
            s = s + 1;
        }

        tv.setText(String.valueOf(s));
        // tv.setText(selectedOptionQ2);
    }
}

0
ganjaam On

An alternative solution can be adding a listener for each of the radio buttons. Whenever a radio button is clicked, you change the value of a global variable. In the alert dialog's listener, you can simply use that global variable instead of adding lengthy codes to check which radio button has been clicked.

Here's a sample code:


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioButton1_1.setOnClickListener { buttonView, isChecked ->
            if (isChecked) selectedOptionQ1 = radioButton1_1.getText().toString();
        }

        radioButton1_2.setOnClickListener { buttonView, isChecked ->
            if (isChecked) selectedOptionQ1 = radioButton1_2.getText().toString();
        }

        radioButton2_1.setOnClickListener { buttonView, isChecked ->
            if (isChecked) selectedOptionQ2 = radioButton2_1.getText().toString();
        }

        radioButton2_2.setOnClickListener { buttonView, isChecked ->
            if (isChecked) selectedOptionQ2 = radioButton2_2.getText().toString();
        }

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
                alertDialogBuilder.setTitle("Alert");
                alertDialogBuilder.setMessage("Click Yes to Submit").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                        intent.putExtra("selectedOptionQ1", selectedOptionQ1);
                        intent.putExtra("selectedOptionQ2", selectedOptionQ2);
                        startActivity(intent);
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
    }

Here, selectedOptionQ1 and selectedOptionQ2 are global variables. Also, I'm assuming that you can select only one radio button of a radio button group.