Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: RadioButton within Nested Layout under RadioGroup falls out of Group

I'm trying to get some sensible positioning of my radiobuttons which are part of a RadioGroup. I was having trouble as my RadioGroup was sitting on top of the rest of my layout design, then I use margins to push the buttons around, the problem here is this works on one or two device layouts only. I thought I struck gold when I discovered I could define my RadioGroup then put in a RelativeLayout below which amongst other things contained one of the RadioButtons, then I repeat twice more. this gives me the layout I desire. Problem is, when I run the code, the RadioButtons act link standalone buttons :(

So two questions,

  • can I link these buttons back to the Group?

  • Is there a decent way to be able to define the layout of the RadioGroup Radiobuttons independently of the Group.

I am thinking an alternative may be independent RadioButtons and use code to enable/disable them, but that does kind of defeat the usefulness of RadioGroup.

thanks.

Here is my XML layout should you be curious.

    <ScrollView android:id="@+id/scrollview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"

       android:layout_below="@id/HDDResultsBox"
        >   

    <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:windowSoftInputMode="stateHidden"
    > 


    <RadioGroup android:id="@+id/calcBy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:focusable="true" 
        android:focusableInTouchMode="true"
        android:layout_below="@id/HDDResultsBox"

        >

  <RelativeLayout android:id="@+id/intervalBox"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/backgroundbox"
       android:layout_margin="5dp"
       android:padding="5dp"


        >
          <TextView  android:id="@+id/intervalHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Interval"
            android:gravity="left"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:layout_marginLeft="5dp"
            />    

         <TextView  android:id="@+id/intervalHelpText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Interval help text"
            android:gravity="left"
            android:textColor="#ffffff"
            android:textSize="8dp"
            android:layout_toRightOf="@id/intervalHeader"
            android:layout_marginLeft="10dp"
            /> 

           <LinearLayout  android:id="@+id/interval2ndBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@id/intervalHeader"
            android:layout_marginLeft="10dp"
            >
                <RadioButton android:id="@+id/byInterval"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    />

                <EditText  android:id="@+id/intervalValue"
                    android:layout_width="75dp"
                    android:layout_height="40dp"
                    android:text="50"
                    android:textSize="14sp"
                    android:gravity="center"
                    android:inputType="number" 
                    android:layout_marginLeft="20dp"
                    />    

                <Spinner android:id="@+id/intervalType"
                    android:layout_width="match_parent"
                    android:layout_height="42dp"
                    android:drawSelectorOnTop="false"
                    android:layout_marginTop="1dp"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="10"
               android:layout_marginRight="2dp"
                    /> 

              <SeekBar android:id="@+id/intervalSeek"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:max="100"
               android:progress="50"
               android:layout_marginLeft="5dp"
               android:layout_marginRight="5dp"
               android:layout_marginTop="8dp"
               android:layout_weight="7"
           /> 
            </LinearLayout>  

        </RelativeLayout>


 <RelativeLayout android:id="@+id/recordBox"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/backgroundbox"
       android:layout_margin="5dp"
       android:padding="5dp"


        >
          <TextView  android:id="@+id/recordHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Record Duration"
            android:gravity="left"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:layout_marginLeft="5dp"
            />    

         <TextView  android:id="@+id/recordHelpText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Record help text"
            android:gravity="left"
            android:textColor="#ffffff"
            android:textSize="8dp"
            android:layout_toRightOf="@id/recordHeader"
            android:layout_marginLeft="10dp"
            /> 

           <LinearLayout  android:id="@+id/record2ndBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@id/recordHeader"
            android:layout_marginLeft="10dp"
            >
                <RadioButton android:id="@+id/byrecord"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    />

                <EditText  android:id="@+id/recordValue"
                    android:layout_width="75dp"
                    android:layout_height="40dp"
                    android:text="50"
                    android:textSize="14sp"
                    android:gravity="center"
                    android:inputType="number" 
                    android:layout_marginLeft="20dp"
                    />    

                <Spinner android:id="@+id/recordType"
                    android:layout_width="match_parent"
                    android:layout_height="42dp"
                    android:drawSelectorOnTop="false"
                    android:layout_marginTop="1dp"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="10"
               android:layout_marginRight="2dp"
                    /> 

              <SeekBar android:id="@+id/recordSeek"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:max="100"
               android:progress="50"
               android:layout_marginLeft="5dp"
               android:layout_marginRight="5dp"
               android:layout_marginTop="8dp"
               android:layout_weight="7"
           /> 
            </LinearLayout>  

        </RelativeLayout>

 <RelativeLayout android:id="@+id/playBackBox"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/backgroundbox"
       android:layout_margin="5dp"
       android:padding="5dp"


        >
          <TextView  android:id="@+id/playBackHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Video Length"
            android:gravity="left"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:layout_marginLeft="5dp"
            />    

         <TextView  android:id="@+id/playBackHelpText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="playBack help text"
            android:gravity="left"
            android:textColor="#ffffff"
            android:textSize="8dp"
            android:layout_toRightOf="@id/playBackHeader"
            android:layout_marginLeft="10dp"
            /> 

           <LinearLayout  android:id="@+id/playBack2ndBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@id/playBackHeader"
            android:layout_marginLeft="10dp"
            >
                <RadioButton android:id="@+id/byplayBack"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    />

                <EditText  android:id="@+id/playBackValue"
                    android:layout_width="75dp"
                    android:layout_height="40dp"
                    android:text="50"
                    android:textSize="14sp"
                    android:gravity="center"
                    android:inputType="number" 
                    android:layout_marginLeft="20dp"
                    />    

                <Spinner android:id="@+id/playBackType"
                    android:layout_width="match_parent"
                    android:layout_height="42dp"
                    android:drawSelectorOnTop="false"
                    android:layout_marginTop="1dp"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="10"
               android:layout_marginRight="2dp"
                    /> 

              <SeekBar android:id="@+id/playBackSeek"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:max="100"
               android:progress="50"
               android:layout_marginLeft="5dp"
               android:layout_marginRight="5dp"
               android:layout_marginTop="8dp"
               android:layout_weight="7"
           /> 
            </LinearLayout>  

        </RelativeLayout>    


    </RadioGroup>           
</RelativeLayout>
        </ScrollView>
like image 455
Purplemonkey Avatar asked Mar 24 '12 12:03

Purplemonkey


People also ask

How to add RadioButton in android?

Open “res/layout/main. xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout . Radio button selected by default. To make a radio button is selected by default, put android:checked="true" within the RadioButton element.

How to group radio buttons together?

You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.

How to use RadioButton android?

RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.

How to group radio buttons in android studio?

To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup . By grouping them together, the system ensures that only one radio button can be selected at a time.


1 Answers

After looking around for a solution it looks the only way to do this is to use individual radio buttons and then programmically control there on/off states.

like image 174
Purplemonkey Avatar answered Oct 17 '22 04:10

Purplemonkey