no_profile
no_profile

Reputation: 374

How can I get the selected radio button from an intent?

I am making an app that let's the user create a discount for the items and I am letting them choose what type. Either by Percentage or Amount. I have them in my radiogroup.

I have successfully added them and now I want somehow users to update it. What I want to do is to get the checked radio button and display it to my Edit Discount activity. How can I set the checked radio button to the selected one?

Thanks for any help.

Here is how I get the selected item from my AddDiscountActivity.java

public void addRadioGroupListener(){
    int radioid = disctype.getCheckedRadioButtonId();
    discbtn = (RadioButton) findViewById(radioid);
    selecteddisc = discbtn.getText().toString();
}

This is my intent

    public void onClick(View v) {
        String disctype = list.get(viewHolder.getAdapterPosition()).getDisc_type();

        Intent edit = new Intent(v.getContext(), EditDiscount.class);
        edit.putExtra("discounttype", disctype);
        v.getContext().startActivity(edit);

Upvotes: 0

Views: 197

Answers (1)

Hossein Yousefpour
Hossein Yousefpour

Reputation: 4953

In your first Activity write something like this:

RadioGroup radioGroup = activity.findViewById(R.id.radioGroup);
int selectedRadioID = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = activity.findViewById(selectedRadioID);
String selectedRadioText = radioButton.getText().toString();
Intent edit = new Intent(context, EditDiscount.class);
edit.putExtra("discounttype", selectedRadioText);
context.startActivity(edit);

And in your second activity get the text from intent like this:

String selectedRadioTxt = getIntent().getExtras().getString("discounttype");
if(selectedRadioTxt.equals(yourRadioButton.getText()))
     yourRadioButton.setChecked(true);

Upvotes: 1

Related Questions