0

I have set up a radio group dynamically like this:

In my XML I have:

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:id="@+id/user_accounts_radios"
       android:layout_gravity="left"
       android:layout_marginLeft="20dp"
       android:layout_height="wrap_content"
       android:orientation="vertical">

In my Java code, I have

RadioGroup radioGroup;
protected void onCreate(){
    radioGroup = (RadioGroup) findViewById(R.id.user_accounts_radios);
    setupUsers();
}

on the setupUsers

void displayOpts(){
    List<UserAccountDetailsModel> accounts = userAccountDetailsSqliteModel.getAccounts();

    RadioGroup account_radios = new RadioGroup(this);
    account_radios.setOrientation(LinearLayout.VERTICAL);
    for (UserAccountDetailsModel user : accounts){
        CompanyLocationsModel company = companyLocationSqliteModel.getCompany(user.getCompany_id());
        RadioButton rdbtn = new RadioButton(this);
        rdbtn.setTextSize(17);
        rdbtn.setId(company.getId());
        rdbtn.setText(user.getFirst_name() + " "+user.getLast_name() + " ---- " + company.getName());
        account_radios.addView(rdbtn);
    }
    radioGroup.addView(account_radios);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
           // checkedId is the RadioButton selected
            Log.i("test", "Checked is "+checkedId);
        }
    });
}

The above displays the RadioGroup but I have the following issues:

  1. Whenever I click on the second RadioButton the first one cannot be clicked again

  2. the log on the selected listener doesn't log

I dont know what is wrong, because the RadioButtons are displayed correctly.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Geoff
  • 6,277
  • 23
  • 87
  • 197
  • Why you are creating another `RadioGroup` and why don't you add your all `RadioButton` directly inside your main `radioGroup` – AskNilesh Feb 06 '19 at 09:20
  • 2
    Why do you add one `RadioGroup` into another? remove this `RadioGroup account_radios = new RadioGroup(this);` add all in `radioGroup` and try again. – ardiien Feb 06 '19 at 09:20
  • @YaroslavOvdiienko thanks this now works I see my stupid error – Geoff Feb 06 '19 at 09:35
  • That's OK, next time just think consistently and keep a code clean) – ardiien Feb 06 '19 at 11:29

1 Answers1

2

You are creating a new RadioGroup which you put into the existing one. This is wrong. Just use the existing one like this:

void displayOpts(){
    List<UserAccountDetailsModel> accounts = userAccountDetailsSqliteModel.getAccounts();
    //deleted line
    //next line changed
    radioGroup.setOrientation(LinearLayout.VERTICAL);
    for (UserAccountDetailsModel user : accounts){
        CompanyLocationsModel company = companyLocationSqliteModel.getCompany(user.getCompany_id());
        RadioButton rdbtn = new RadioButton(this);
        rdbtn.setTextSize(17);
        rdbtn.setId(company.getId());
        rdbtn.setText(user.getFirst_name() + " "+user.getLast_name() + " ---- " + company.getName());
        //next line changed
        radioGroup.addView(rdbtn);
    }
    //deleted line
    //next line changed
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
           // checkedId is the RadioButton selected
            Log.i("test", "Checked is "+checkedId);
        }
    });
}
leonardkraemer
  • 6,573
  • 1
  • 31
  • 54