Dynamic RadioGroup

Nathan Sokalski 4,116 Reputation points
2022-02-09T06:13:15.787+00:00

I have a RadioGroup that will have various numbers of children based on other factors. What I basically want to do is something like what you do with a RecyclerView, but I would be adding or removing RadioButtons from a RadioGroup instead of some other layout. Is there any good way to do this, or do I need to do it manually?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,295 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2022-02-10T05:49:56.29+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    but I would be adding or removing RadioButtons from a RadioGroup instead of some other layout. Is there any good way to do this, or do I need to do it manually?

    I think need to do it manually with background code.

    Firstly, we need to add the RadioGroup in the xml or background code. then add the RadioButtons at the runtime with background code(based on other factors). Because RadioGroup do not have adapter like RecyclerView. I add and remove RadioButton like following code.

       rg = FindViewById<RadioGroup>(Resource.Id.radioGroup1);  
                    Button button1 = FindViewById<Button>(Resource.Id.button1);  
                    rb = new RadioButton[5];  
                    rg.Orientation = Orientation.Horizontal;//or RadioGroup.VERTICAL  
                   for (int i = 0; i < 5; i++)  
                   {  
                       rb[i] = new RadioButton(this);  
                       rg.AddView(rb[i]); //the RadioButtons are added to the radioGroup   
                       rb[i].Text = "Test";  
                   }  
         
         
                 //Or remove the RadioButtons   
         
                 rg.RemoveView(rb[i]);//now the RadioButtons are in the RadioGroup  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful