Tucker
Tucker

Reputation: 188

Android - RadioButtons squishing up in RadioGroup - formatting issues

I am trying to split a TableRow into 4 equal sections. The first is a TextView, and the last three are RadioGroups. I have successfully split the TableRow into these equal sections. But, each row is dynamically built, and could possibly have up to 5 RadioButtons per RadioGroup. When building a TableRow with 5 RadioButtons in each RadioGroup everything displays and functions, but the RadioButtons are squished together. Is there some way to have the RadioGroup have multiple lines when this happens, but still remain in the same section of the same TableRow? Basically like a TextView will become multiline when given the proper parameters?

Java code for this case:

RadioButton rb1, rb2, rb3, rb4, rb5;
RadioGroup rg;
TextView tv;
TableLayout tl = (TableLayout) this.findviewById(R.string.tl);
LayoutInflater inflater = getLayoutInflater();

//set up TableRow and TextView
TableRow tr = (TableRow)inflater.inflate(R.layout.tablerow3q, tl, false);
tr.setBackgroundColor(Color.LTGRAY);
tv = (TextView)tr.findViewById(R.id.tv);
tv.setText("Some text");

//loop sets up 1 RadioGroup per increment, each with 5 RadioButtons
for(int i = 0; i < 3; i++) {
    rb1 = new RadioButton(this);
    rb2 = new RadioButton(this);
    rb3 = new RadioButton(this);
    rb4 = new RadioButton(this);
    rb5 = new RadioButton(this);

    rb1.setText("test1");
    rb2.setText("test2");
    rb3.setText("test3");
    rb4.setText("test4");
    rb5.setText("test5");

    //without these layouts, only four buttons are displayed, the other is cut off
    rb1.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    rb2.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    rb3.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    rb4.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    rb5.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);

    //add RadioButtons to correct RadioGroup based on loop increment
    if(i == 0) {
        rg1 = (RadioGroup)tr.findViewById(R.id.rg1);
        rg1.addView(rb1);
        rg1.addView(rb2);
        rg1.addView(rb3);
        rg1.addView(rb4);
        rg1.addView(rb5);
    }
    if(i == 1) {
        rg1 = (RadioGroup)tr.findViewById(R.id.rg2);
        rg1.addView(rb1);
        rg1.addView(rb2);
        rg1.addView(rb3);
        rg1.addView(rb4);
        rg1.addView(rb5);
    }
    if(i == 2) {
        rg1 = (RadioGroup)tr.findViewById(R.id.rg3);
        rg1.addView(rb1);
        rg1.addView(rb2);
        rg1.addView(rb3);
        rg1.addView(rb4);
        rg1.addView(rb5);
    }
}
tl.addView(tr);

XML layout for TableRow that is being inflated:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/tv"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_margin="1dip"
        android:layout_weight="0.25"
        android:background="#ffffff"/>
    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="0dip"
        android:orientation="horizontal"
        android:layout_margin="1dip"
        android:layout_weight="0.25"
        android:background="#ffffff"/>
    <RadioGroup
        android:id="@+id/rg2"
        android:layout_width="0dip"
        android:orientation="horizontal"
        android:layout_margin="1dip"
        android:layout_weight="0.25"
        android:background="#ffffff"/>
    <RadioGroup
        android:id="@+id/rg3"
        android:layout_width="0dip"
        android:orientation="horizontal"
        android:layout_margin="1dip"
        android:layout_weight="0.25"
        android:background="#ffffff"/>
</TableRow>

Upvotes: 0

Views: 2032

Answers (1)

acj
acj

Reputation: 4821

RadioGroup extends LinearLayout, which doesn't wrap its contents. There are a couple of options for dealing with the problem, though:

  1. Copy the source code for the RadioGroup class and modify it to extend a different type of Layout, such as a TableLayout or RelativeLayout. You'll need to manage how the radio buttons are arranged.

  2. Dynamically build a nested TableLayout and use OnClickListeners to handle selecting/deselecting the various radio buttons.

Upvotes: 2

Related Questions