Run time - Radio Buttons

Put your problem here if it does not fit any of the other categories.

Run time - Radio Buttons

Postby Chitra » Mon Feb 20, 2012 12:54 pm

Hi,

I need to create below style with Radio buttons in single Radio Group (Run time).

Radio1 Radio2
Radio3 Radio4

I have tried with Table Layout. But Radio Group won't allow TableLayout inside.

Can any one plz help me, how to design??
Thanks,
Chitra
User avatar
Chitra
Master Developer
Master Developer
 
Posts: 213
Joined: Mon Mar 01, 2010 7:59 am
Location: Bangalore

Top

Re: Run time - Radio Buttons

Postby Phyll » Mon Feb 20, 2012 2:31 pm

Hi Chitra,

No real easy way but here is one way. Maybe you can improve on it.

First the layout:

Code: Select all
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">       
   
        <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio 1"
        />
   
         <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/radio1"
        android:text="Radio 2"
        />   
   
        <RadioButton
        android:id="@+id/radio3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radio1"
        android:text="Radio 3"
        />   
   
        <RadioButton
        android:id="@+id/radio4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radio1"
        android:layout_toRightOf="@+id/radio3"
        android:text="Radio 4"
        />   
 
</RelativeLayout>


Then some code to initialize it:

Code: Select all
RadioButton[] rb = new RadioButton[4];
   

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
       
        setContentView(R.layout.main);
       
        rb[0] = (RadioButton)findViewById(R.id.radio1);
        rb[1] = (RadioButton)findViewById(R.id.radio2);
        rb[2] = (RadioButton)findViewById(R.id.radio3);
        rb[3] = (RadioButton)findViewById(R.id.radio4);
       
        int i = 0;
        while (i<4) {
        rb[i].setOnClickListener(new View.OnClickListener() {   
             public void onClick(View view) {
                  int k = 0;
                while (k<4) {
                    if (rb[k].isChecked()) rb[k].setChecked(false);
                   k++;
                    }//while k
                ((CompoundButton) view).setChecked(true);
                 }//onclick
             });//setonclicklistener     
         i++;
         }//while
}


Use another loop to find out which one is checked when you need to know.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Run time - Radio Buttons

Postby Chitra » Tue Feb 21, 2012 7:44 am

Hey Phyll,

Thanks for your quick reply. I have seen your code. That have static Radio Buttons. But i need to create run time in one Single Radio Group. Mns, sometimes i have to create 5radio buttons, some times i have to create 7 that also one row want to align two buttons. For that sample only i have given lik,

Radio1 Radio2
Radio3 Radio4
: :
: :
: :
Radio n Radio m

Below is my code.,
Code: Select all
RadioGroup rGroup = (RadioGroup) findViewById(R.id.radiogroup);
for (int i = 0; i < 5; i++) {
      RadioButton rb = new RadioButton(this);
      rb.setText("Radio"+i);
      rb.setOnCheckedChangeListener(this);
        
      rGroup.addView(rb);
}


Here radio buttons are aligned vertically. But i need that above mentioned sample.
Thanks,
Chitra
User avatar
Chitra
Master Developer
Master Developer
 
Posts: 213
Joined: Mon Mar 01, 2010 7:59 am
Location: Bangalore

Re: Run time - Radio Buttons

Postby Phyll » Tue Feb 21, 2012 1:14 pm

Hi Chitra,

Ok, to do it programatically you could do something like this:

The xml main:

Code: Select all
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/relativegroup">       
   

</RelativeLayout>


The Java code:

Code: Select all
   RadioButton[] rb = new RadioButton[4];
   String[] rbText = {"Radio 1", "Radio 2", "Radio 3", "Radio 4"};
   RelativeLayout rl;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
       
        setContentView(R.layout.main);
       
        rl = (RelativeLayout) findViewById(R.id.relativegroup);
        int i = 0;
        int d = 0;
        while (i<4) {
           rb[i] = new RadioButton(this);
           rb[i].setText(rbText[i]);
           d = i/2;
           rb[i].setId(i+1);
          RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                  RelativeLayout.LayoutParams.WRAP_CONTENT,
                                        RelativeLayout.LayoutParams.WRAP_CONTENT);
           if (i>0) {
              Log.i("RADIO","i="+i+" d="+d+" mod="+(i%2));
              if (d > 0) lp.addRule(RelativeLayout.BELOW, rb[d].getId());
              if ((i%2) != 0) lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                  else lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
              
              //Could also use something like this instead of the two ALIGNs above
              //if ((i%2) != 0) lp.addRule(RelativeLayout.RIGHT_OF, rb[i-1].getId());
              
               }//relative position
            rb[i].setOnClickListener(new View.OnClickListener() {   
                public void onClick(View view) {
                     int k = 0;
                   while (k<4) {
                       if (rb[k].isChecked()) rb[k].setChecked(false);
                      k++;
                       }//while k
                   ((CompoundButton) view).setChecked(true);
                    }//onclick
                });//setonclicklistener
           
            rl.addView(rb[i],lp);
            i++;
            }//while


I suppose you could do something like this with a tablelayout too.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests