0

Here is the case : i have 4 radioButtons that are not members of a radioGroup. Thats because RadioGroup places children in vertical order by default and cannot place them relative to each other.Right now , i am check all the radioButtons. What i want to achieve is to control their checked state , so that when one is checked(true) the others checked state is set automatically to false. There widgets are independent layout elements , so i guess i have to find a way to group them all together. I tried it by coding 4 RadioButton variables with a simple if..else if function using isChecked and setChecked built-in methods. Then i tried grouping them in an Radiobutton[] array and looping to the array.length with a switch-case loop. Nothing works. Stack for 2 days now. Any help appreciated.

Thanks for reply , i explain further. I have 4 imageViews and 4 radioButtons exactly below each one , in a RelativeLayout parent , so that the user can make a choise which image he prefers. The imageViews are not vertically placed , so are the radioButtons. Thats why i dont want to use radioGroup. I could use LinearLayout and place imageViews in vertical orientation and have the radioButtons in a group just aside them. Every radioButton checked state is false by default and when i check it , becomes true. The problem is that i can check all of the radioButtons . What i want is when i select one RadioButton , the others are automatically unchecked , so that only one may be checked at any time. Here is the snippet:

    private RadioButton rbtnWater;
    private RadioButton rbtnRoad;
    private RadioButton rbtnBoy;
    private RadioButton rbtnLeft;

    rbtnWater = (RadioButton) findViewById(R.id.rbtnWater);
    rbtnRoad = (RadioButton) findViewById(R.id.rbtnRoad);
    rbtnBoy = (RadioButton)findViewById(R.id.rbtnBoy);
    rbtnLeft = (RadioButton) findViewById(R.id.rbtnLeft)

          //method called at onClick attribute (XML)
      public void checkState(View v) {

      if (rbtnWater.isChecked()) {
          rbtnLeft.setChecked(false);
          rbtnBoy.setChecked(false);
          rbtnRoad.setChecked(false);
      }
      else if (rbtnLeft.isChecked()) {
          rbtnBoy.setChecked(false);
          rbtnWater.setChecked(false);
          rbtnRoad.setChecked(false);
      }
      else if (rbtnBoy.isChecked()) {
          rbtnWater.setChecked(false);
          rbtnRoad.setChecked(false);
          rbtnLeft.setChecked(false);
      }
      else if (rbtnRoad.isChecked()) {
          rbtnWater.setChecked(false);
          rbtnBoy.setChecked(false);
          rbtnLeft.setChecked(false);
      }
    }

1 Answers1

1
  • It not really clear what is not working, please add details
  • That being said I suggest you change the technique and use a RadioGroup anyway, just extend it and modify to what you need or take it's source code from AOSP and instead of having it extend a LinearLayout you can extend something else you would rather work with
Shai Levy
  • 715
  • 9
  • 25