Preparing customized radio group type of layout

Viewed 12077

I am trying to prepare custom radiogroup like layout in the below image. I have nearly 8-10 rows to do that. So, I prepared one linear layout which has horizontal orientation and added the imageview, textview and radiobutton programatically.

enter image description here

So if I check on one radio button, the other radio buttons should automatically unchecked. Before going to that task itself, I got another problem that if my radio buttons are checked once, then the radio button are not uncheckable though clicked on them. Below is my code.

public class MainActivity extends Activity{

RadioButton[] radioBtns = new RadioButton[10];

    String texts[] = {"text1", "text2", .... "text10"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        entireLayout = (LinearLayout)findViewById(R.id.main_layout);


        for(int i =0; i<10; i++)
        {
            LinearLayout rowLayout=new LinearLayout(this);
            radioBtns[i] = new RadioButton(this);

            radioBtns[i].setId(i);
            radioBtns[i].setOnCheckedChangeListener(cblistener);
            ImageView imageView = new ImageView(this);


            TextView tv = new TextView(this);
            tv.setText(texts[i]);

            rowLayout.addView(imageView);
            rowLayout.addView(tv);
                        rowLayout.addView(radioBtns[i]);

            entireLayout.addView(rowLayout);

                View line = new View(this); 
                line.setBackgroundColor(getResources().getColor(R.color.horizontallinecolor));
                entireLayout.addView(line, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 1));


        }
         }

CompoundButton.OnCheckedChangeListener cblistener = new CompoundButton.OnCheckedChangeListener()
    {
      public void onCheckedChanged(CompoundButton checkedbutton, boolean paramAnonymousBoolean)
      {

        switch (checkedbutton.getId())
        {

        case 0:
          Log.d("tag", "checked 0th position");

          break;
          ..........................
          ..........................
        }
      }
     };
}

I have observed through keeping logs. The control enters onCheckedChanged() for the first time when those are getting checked, but not when unchecked. I wonder how those are uncheckable.

OR According to my requirement I have one more idea to prepare this layout like I prepare one more layout for the row in xml. Then inflating the view 10 times. But then also, how can I check only one radio button so that other selected one gets unchecked. Can someone please suggest me how to achieve this kind of radiogroup through the best way?

Note: I have kept my above code because to show what I have tried. If it is completely wrong way, please go easy and please suggest me how to get that done.

7 Answers

I had the same layout as you but the number of rows in my case was limited by four. And I found out that the easiest way to make radioButtons work together is to use the simple method that will loop through array of radioButtons and check ALL of them OFF and then check ON the one which is needed.

And that's all folks (c)

You don't need to use regular RadioGroup neither creating custom radio group class, you don't need to setOnCheckChangedListeners.

    final RadioButton radio1 = (RadioButton) findViewById(R.id.radio_1);
    final RadioButton radio2 = (RadioButton) findViewById(R.id.radio_2);
    final RadioButton radio3 = (RadioButton) findViewById(R.id.radio_3);
    final RadioButton radio4 = (RadioButton) findViewById(R.id.radio_4);

    final RadioButton[] radios = {radio1, radio2, radio3, radio4};

    View.OnClickListener buttonListener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            for(RadioButton radio : radios){
                radio.setChecked(false);
            }

            switch (v.getId()){
                case R.id.button_1:
                    radio1.setChecked(true);
                    break;
                case R.id.button_2:
                    radio2.setChecked(true);
                    break;
                case R.id.button_3:
                    radio3.setChecked(true);
                    break;
                case R.id.button_4:
                    radio4.setChecked(true);
                    break;
            }
        }
    };


    RelativeLayout button1 = (RelativeLayout) findViewById(R.id.button_1);
    button1.setOnClickListener(buttonListener);

    RelativeLayout button2 = (RelativeLayout) findViewById(R.id.button_2);
    button2.setOnClickListener(buttonListener);

    RelativeLayout button3 = (RelativeLayout) findViewById(R.id.button_3);
    button3.setOnClickListener(buttonListener);

    RelativeLayout button4 = (RelativeLayout) findViewById(R.id.button_4);
    button4.setOnClickListener(buttonListener);

*Hint: Don't forget to set

android:text=""
android:checked="false"
android:clickable="false"

to your layout to each RadioButton. And set initial state programmatically for that radioButton which should be checked.

Related