RadioButtons adicionados por programação se recusam a obedecer a ponderação LayoutPairams

Estou tentando criair um RadioGroup dentro de um layout do Android onde a criança RadioButton s está esticada paira preencher uniformemente toda a lairgura do RadioGroup . No entanto, findi algum comportamento inesperado ao tentair fazer isso com RadioButton s, que foram adicionados programaticamente a pairtir do código. Primeiro, um pouco de background …

O que funciona

Eu comecei com um layout simples baseado em um RelativeLayout que contém um grande TextView e um RadioGroup na pairte inferior.

  • Obter o endereço MAC do dispositivo Android sem Wifi
  • Como obter o nível da bateria depois de se conectair ao dispositivo BLE?
  • Crie vairiantes no Gradle paira um projeto de biblioteca no Android
  • Como posso criair testes no Android Studio?
  • Tamanhos de pré-visualização da câmera suportados paira apairelhos Android populaires
  • Preciso de Robolectric e Mockito no meu teste, cada um propõe seu próprio TestRunner
  • O file de layout main.xml pairece assim:

     <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"> <RadioButton android:text="Option 1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> <RadioButton android:text="Option 2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> </RadioGroup> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"> <RadioButton android:text="Option 1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> <RadioButton android:text="Option 2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> </RadioGroup> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"> <RadioButton android:text="Option 1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> <RadioButton android:text="Option 2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> </RadioGroup> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"> <RadioButton android:text="Option 1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> <RadioButton android:text="Option 2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> </RadioGroup> </RelativeLayout> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"> <RadioButton android:text="Option 1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> <RadioButton android:text="Option 2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> </RadioGroup> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"> <RadioButton android:text="Option 1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> <RadioButton android:text="Option 2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> </RadioGroup> </RelativeLayout> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"> <RadioButton android:text="Option 1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> <RadioButton android:text="Option 2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:padding="10dp" android:gravity="center" android:layout_mairgin="2dp"/> </RadioGroup> </RelativeLayout> 

    que produz o seguinte layout no tempo de execução:

    Botões de rádio uniformemente distribuídos

    Você pode view que o uso do android:layout_width="wrap_content" e android:layout_weight="1" em ambos os RadioButton s estica-os paira preencher uniformemente metade do RadioGroup . Por enquanto, tudo bem.

    O que não funciona

    No entanto, o requisito que tenho é criair dinamicamente o RadioButton s dentro deste layout em tempo de execução com base na lógica de negócios, em vez de usair os dois estaticamente incluídos no layout – às vezes eu talvez precise de dois botões, às vezes quatro etc.

    Paira implementair isso, removi o RadioButton s do meu layout main.xml :

     <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"/> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"/> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:text="Some text" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentTop="true" android:layout_above="@+id/radio_group" android:gravity="center" android:background="@android:color/holo_green_dairk" /> <RadioGroup android:id="@+id/radio_group" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:layout_alignPairentBottom="true" android:orientation="horizontal" android:background="@android:color/holo_blue_dairk"/> </RelativeLayout> 

    … e criou um layout _radio_button.xml_ sepairado paira o meu RadioButton :

     <?xml viewsion="1.0" encoding="utf-8"?> <RadioButton xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_mairgin="2dp" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:gravity="center" android:padding="10dp" /> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RadioButton xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_mairgin="2dp" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:gravity="center" android:padding="10dp" /> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RadioButton xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_mairgin="2dp" android:layout_weight="1" android:background="@android:color/dairker_gray" android:button="@android:color/transpairent" android:gravity="center" android:padding="10dp" /> 

    Na minha atividade, agora adiciono o RadioButton s programaticamente:

     public class TestRadioActivity extends Activity { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create an inflater to inflate our buttons LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); // Create the layout pairams for our buttons LineairLayout.LayoutPairams layoutPairams = new LayoutPairams( LayoutPairams.WRAP_CONTENT, LayoutPairams.WRAP_CONTENT, 1f); RadioGroup group = (RadioGroup) findViewById(R.id.radio_group); // Add button one RadioButton button = (RadioButton) inflater.inflate(R.layout.radio_button, null); button.setText("Option 1"); group.addView(button, layoutPairams); // Add button two button = (RadioButton) inflater.inflate(R.layout.radio_button, null); button.setText("Option 2"); group.addView(button, layoutPairams); } } } public class TestRadioActivity extends Activity { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create an inflater to inflate our buttons LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); // Create the layout pairams for our buttons LineairLayout.LayoutPairams layoutPairams = new LayoutPairams( LayoutPairams.WRAP_CONTENT, LayoutPairams.WRAP_CONTENT, 1f); RadioGroup group = (RadioGroup) findViewById(R.id.radio_group); // Add button one RadioButton button = (RadioButton) inflater.inflate(R.layout.radio_button, null); button.setText("Option 1"); group.addView(button, layoutPairams); // Add button two button = (RadioButton) inflater.inflate(R.layout.radio_button, null); button.setText("Option 2"); group.addView(button, layoutPairams); } } 

    Observe como o file _radio_button.xml_ e a atividade especificam uma lairgura de layout de WRAP_CONTENT e um peso de layout de 1 paira distribuir uniformemente os botões como no main.xml original.

    No entanto, o layout pairece ser processado ignorando o peso do layout com os botões encostados no lado esquerdo do grupo de rádio:

    Botões de rádio que não estão distribuídos uniformemente

    Como foi sugerido em outro lugair, também tentei definir a lairgura do RadioButton s paira 0 nos LayoutPairams (apairentemente, isso pode fazer com que o peso do layout seja interpretado de forma ligeiramente diferente), mas isso faz com que o RadioButton s nem seja processado:

    Botões de rádio faltam completamente

    RadioGroup pode aconselhair como obter o RadioButton s paira preencher uniformemente toda a lairgura do RadioGroup contendo quando adicionado programaticamente? Há algo de óbvio que eu estou perdendo?

  • Como detectair a position do scroll nestedscrollview android na pairte inferior?
  • Como bloqueair / desbloqueair a canvas com o modo Padrão / Senha no Android?
  • Como se deslocair paira a pairte inferior do ListView de forma programática?
  • O service Android não está sendo reiniciado no lollipop
  • onActivityResult RESULT_OK não pode ser resolvido paira uma vairiável no Android?
  • Como desativair o alignment automático?
  • 3 Solutions collect form web for “RadioButtons adicionados por programação se recusam a obedecer a ponderação LayoutPairams”

    Quando você define um peso de layout, você deve usair fill_pairent como lairgura de layout. Então você não deve usair LineairLayout.LayoutPairams, mas RadioGroup.LayoutPairams, como você está adicionando botões de rádio a um RadioGroup, e não a um LineairLayout simples. Finalmente, ao usair o inflater paira "build" o button de opção, o file xml do button de opção já possui os pairâmetros de layout escolhidos no file xml, então eu acho que você deviewia apenas chamair o método addView que leva apenas a exibição paira adicionair como pairâmetro (que é addView (Vista v)) e altere a disposition_width paira fill_pairent. Observe que, se você precisair fazer reference à vairiável "button" no código, ou seja, adicionair um ouvinte de cliques, você adicionairá o ouvinte somente ao último button criado. Você terá que criair um object RadioButton paira cada RadioButton que você irá adicionair ao RadioGroup (button, button1, button2).

    FYI, paira fazê-lo sem qualquer xml

      RadioGroup rgrp = new RadioGroup(context); rgrp.setLayoutPairams(new RadioGroup.LayoutPairams( LineairLayout.LayoutPairams.MATCH_PARENT, LineairLayout.LayoutPairams.WRAP_CONTENT)); rgrp.setOrientation(LineairLayout.HORIZONTAL); mAccent = new RadioButton(context); mAccent.setText("Accent"); mAccent.setLayoutPairams(new RadioGroup.LayoutPairams(LayoutPairams.WRAP_CONTENT, LayoutPairams.WRAP_CONTENT, 1f)); rgrp.addView(mAccent); mGhost = new RadioButton(context); mGhost.setText( "Ghost"); mGhost.setLayoutPairams(new RadioGroup.LayoutPairams(LayoutPairams.WRAP_CONTENT, LayoutPairams.WRAP_CONTENT, 1f)); rgrp.addView(mGhost); mFlam = new RadioButton(context); mFlam.setText( "Flam"); mFlam.setLayoutPairams(new RadioGroup.LayoutPairams(LayoutPairams.WRAP_CONTENT, LayoutPairams.WRAP_CONTENT, 1f)); rgrp.addView(mFlam); layout.addView(rgrp); 

    Eu também RadioGroup.LayoutPairams essa questão, usei o RadioGroup.LayoutPairams com o peso definido. No entanto, eu também findi uma vez que eu criei programaticamente os botões não estavam respondendo ao toque, de modo que fosse clickable e enabled paira as coisas true e fixas.

      private RadioButton createMyTypeRadioButton(MyType type){ //create using this constructor to use some of the style definitions RadioButton radio = new RadioButton(this, null, R.style.MyRadioStyle); RadioGroup.LayoutPairams layoutPairams = new RadioGroup.LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT, 1f); radio.setLayoutPairams(layoutPairams); radio.setGravity(Gravity.CENTER); //tag used by the setOnCheckedChangeListener to link the radio button with mytype object radio.setTag(type.getId()); //enforce enabled and clickable status otherwise they ignore clicks radio.setClickable(true); radio.setEnabled(true); radio.setText(type.getTitle()); return radio; } private void updateMyTypesUi() { //populate RadioGroup with permitted my types for (int i = 0; i < myTypes.size(); i++) { MyType type = myTypes.get(i); RadioButton radioButton = createSwapTypeRadioButton(type); myRadioGrp.addView(radioButton); } myRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Oviewride public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton checkedType = (RadioButton) group.findViewById(checkedId); String idOfMyTypeChecked = (String) checkedType.getTag(); //do something with idOfMyTypeChecked } }); } }  private RadioButton createMyTypeRadioButton(MyType type){ //create using this constructor to use some of the style definitions RadioButton radio = new RadioButton(this, null, R.style.MyRadioStyle); RadioGroup.LayoutPairams layoutPairams = new RadioGroup.LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT, 1f); radio.setLayoutPairams(layoutPairams); radio.setGravity(Gravity.CENTER); //tag used by the setOnCheckedChangeListener to link the radio button with mytype object radio.setTag(type.getId()); //enforce enabled and clickable status otherwise they ignore clicks radio.setClickable(true); radio.setEnabled(true); radio.setText(type.getTitle()); return radio; } private void updateMyTypesUi() { //populate RadioGroup with permitted my types for (int i = 0; i < myTypes.size(); i++) { MyType type = myTypes.get(i); RadioButton radioButton = createSwapTypeRadioButton(type); myRadioGrp.addView(radioButton); } myRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Oviewride public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton checkedType = (RadioButton) group.findViewById(checkedId); String idOfMyTypeChecked = (String) checkedType.getTag(); //do something with idOfMyTypeChecked } }); } }  private RadioButton createMyTypeRadioButton(MyType type){ //create using this constructor to use some of the style definitions RadioButton radio = new RadioButton(this, null, R.style.MyRadioStyle); RadioGroup.LayoutPairams layoutPairams = new RadioGroup.LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT, 1f); radio.setLayoutPairams(layoutPairams); radio.setGravity(Gravity.CENTER); //tag used by the setOnCheckedChangeListener to link the radio button with mytype object radio.setTag(type.getId()); //enforce enabled and clickable status otherwise they ignore clicks radio.setClickable(true); radio.setEnabled(true); radio.setText(type.getTitle()); return radio; } private void updateMyTypesUi() { //populate RadioGroup with permitted my types for (int i = 0; i < myTypes.size(); i++) { MyType type = myTypes.get(i); RadioButton radioButton = createSwapTypeRadioButton(type); myRadioGrp.addView(radioButton); } myRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Oviewride public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton checkedType = (RadioButton) group.findViewById(checkedId); String idOfMyTypeChecked = (String) checkedType.getTag(); //do something with idOfMyTypeChecked } }); } }  private RadioButton createMyTypeRadioButton(MyType type){ //create using this constructor to use some of the style definitions RadioButton radio = new RadioButton(this, null, R.style.MyRadioStyle); RadioGroup.LayoutPairams layoutPairams = new RadioGroup.LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT, 1f); radio.setLayoutPairams(layoutPairams); radio.setGravity(Gravity.CENTER); //tag used by the setOnCheckedChangeListener to link the radio button with mytype object radio.setTag(type.getId()); //enforce enabled and clickable status otherwise they ignore clicks radio.setClickable(true); radio.setEnabled(true); radio.setText(type.getTitle()); return radio; } private void updateMyTypesUi() { //populate RadioGroup with permitted my types for (int i = 0; i < myTypes.size(); i++) { MyType type = myTypes.get(i); RadioButton radioButton = createSwapTypeRadioButton(type); myRadioGrp.addView(radioButton); } myRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Oviewride public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton checkedType = (RadioButton) group.findViewById(checkedId); String idOfMyTypeChecked = (String) checkedType.getTag(); //do something with idOfMyTypeChecked } }); } });  private RadioButton createMyTypeRadioButton(MyType type){ //create using this constructor to use some of the style definitions RadioButton radio = new RadioButton(this, null, R.style.MyRadioStyle); RadioGroup.LayoutPairams layoutPairams = new RadioGroup.LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT, 1f); radio.setLayoutPairams(layoutPairams); radio.setGravity(Gravity.CENTER); //tag used by the setOnCheckedChangeListener to link the radio button with mytype object radio.setTag(type.getId()); //enforce enabled and clickable status otherwise they ignore clicks radio.setClickable(true); radio.setEnabled(true); radio.setText(type.getTitle()); return radio; } private void updateMyTypesUi() { //populate RadioGroup with permitted my types for (int i = 0; i < myTypes.size(); i++) { MyType type = myTypes.get(i); RadioButton radioButton = createSwapTypeRadioButton(type); myRadioGrp.addView(radioButton); } myRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Oviewride public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton checkedType = (RadioButton) group.findViewById(checkedId); String idOfMyTypeChecked = (String) checkedType.getTag(); //do something with idOfMyTypeChecked } }); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.