Search code examples
androidradio-buttonappearance

Radio Buttons don't appear on screen when added programatically


I have the following code in a button click handler:

LinearLayout linearLayoutParent = FindViewById<LinearLayout>(Resource.Id.linearLayoutParent);
LinearLayout linearLayoutFileTransferVia = new LinearLayout(this);
TextView labelFileTransferViaInfo = new TextView(this);
labelFileTransferViaInfo.LayoutParameters = lp;
labelFileTransferViaInfo.Text = "Choose file transfer via to FTP Server";
labelFileTransferViaInfo.SetTextAppearance(Android.Resource.Style.TextAppearanceLarge);
RadioButton rbWiFi = new RadioButton(this);
RadioButton rb3G = new RadioButton(this);
RadioGroup radioGroupFileTransferType = new RadioGroup(this);
radioGroupFileTransferType.Orientation = Orientation.Horizontal;
rbWiFi.Text = "Wi-Fi";
rb3G.Text = "3G";
radioGroupFileTransferType.AddView(rbWiFi);
radioGroupFileTransferType.AddView(rb3G);
linearLayoutFileTransferVia.AddView(labelFileTransferViaInfo);
linearLayoutFileTransferVia.AddView(radioGroupFileTransferType);
linearLayoutParent.AddView(linearLayoutFileTransferVia);

When I click the button labelFileTransferViaInfo appears on screen but radioGroupFileTransferType doesn't. What do you think is the problem for that to happen?


Solution

  • I think you need to add LayoutParams just like that

                LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                    RadioGroup.LayoutParams.MATCH_PARENT,
                    RadioGroup.LayoutParams.WRAP_CONTENT);
    
    radioGroupFileTransferType.AddView(rbWiFi, layoutParams);
    radioGroupFileTransferType.AddView(rb3G, layoutParams);