Android Dynamic RadioGroup

I am trying to create an equalizer for RadioGroup. And I want to dynamically add a switch for presets.

Here is my XML layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/rgEqualizerPreset"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </RadioGroup>

</LinearLayout>

And here is the Java code:

RadioGroup rgEqualizer;
List<RadioButton> radioButtonList;
MediaPlayer mPlayer;
Equalizer mEqualizer;
LinearLayout.LayoutParams layoutParams;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.equalizer);
    rgEqualizer = (RadioGroup) findViewById(R.id.rgEqualizerPreset);

    mPlayer = new MediaPlayer();
    mEqualizer = new Equalizer(0, mPlayer.getAudioSessionId());
    rgEqualizer = new RadioGroup(this);
    radioButtonList = new ArrayList<RadioButton>();

    fillRadioGroupWithRadioButtons();
    rgEqualizer.setOnCheckedChangeListener(this);
}

private void fillRadioGroupWithRadioButtons() {

    Short noPresets = mEqualizer.getNumberOfPresets();
    int i = 0;
    while (i < noPresets) {
        RadioButton rb = new RadioButton(this);
        rb.setText(mEqualizer.getPresetName((short) i));
        layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.FILL_PARENT,
                RadioGroup.LayoutParams.FILL_PARENT);
        rgEqualizer.addView(rb, layoutParams);
        i++;
    }
}

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
    // TODO Auto-generated method stub

}

When this action is opened, only a blank layout screen is displayed. What am I missing?

thank

+3
source share
1 answer

The problem is that rgEqualizer is not really a RadioGroup in your layout. You probably meant:

rgEqualizer = (RadioGroup) this.findViewById(R.id.rgEqualizerPreset);

instead:

rgEqualizer = new RadioGroup(this);

So, your radio buttons turn out just fine, they just have nothing to do with your layout. I feel your pain.

+1
source

All Articles