0

I have a radioGroup and four radiobutton, I just Want to arrange these radiobuttons randomly whenever the program is running.

Say i have radio buttons r1,r2,r3,r4 and a radiogroup. I want sometimes these radiobuttons arranged like r2,r3,r1,r4 (vertically) and some times r1,r2,r4,r3 and so on...

How can i Implement this?

Yoni
  • 1,346
  • 3
  • 16
  • 38
Shahin Ghasemi
  • 1,600
  • 1
  • 20
  • 38
  • you have to create radio groups programmatically and add it into your xml parent. hence you every time you can change their position(Randomly). – Pratik Tank Sep 16 '17 at 08:49
  • all of my widget created programmatically (radio button and groups) so i'm looking for a way to implement it automatically(maybe a function or method)@PratikTank – Shahin Ghasemi Sep 16 '17 at 08:50

3 Answers3

1

For implementing random RadioButton random sequence following code will help you:

In your layout.xml file add RadioGroup:

 <RadioGroup
        android:id="@+id/gul_radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

And in your java file:

final int NUMBER_OF_RADIOBUTTONS_TO_ADD = 4;//Change it for other number of RadioButtons
    RadioButton[] radioButton;
    RadioGroup radioGroup;


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

        radioGroup = (RadioGroup) findViewById(R.id.gul_radio_group);

        //Initializing the RadioButtons
        radioButton = new RadioButton[NUMBER_OF_RADIOBUTTONS_TO_ADD];
        for (int i = 0; i < NUMBER_OF_RADIOBUTTONS_TO_ADD; i++) {
            radioButton[i] = new RadioButton(this);

            //Text can be loaded here
            radioButton[i].setText("Button " + (i + 1));
        }

        //Random Swapping
        for (int i = 0; i < 4; i++) {//this loop is randomly changing values 4 times
            int swap_ind1 = ((int) (Math.random() * 10) % NUMBER_OF_RADIOBUTTONS_TO_ADD);
            int swap_ind2 = ((int) (Math.random() * 10) % NUMBER_OF_RADIOBUTTONS_TO_ADD);
            RadioButton temp = radioButton[swap_ind1];
            radioButton[swap_ind1] = radioButton[swap_ind2];
            radioButton[swap_ind2] = temp;
        }
        radioButton[0].setChecked(true);//This will make the top RadioButton selected by default


        //Adding RadioButtons in RadioGroup
        for (int i = 0; i < NUMBER_OF_RADIOBUTTONS_TO_ADD; i++) {
            radioGroup.addView(radioButton[i]);
        }
    }
0

Write a function that implements a random function algorithm (you can write your own or find one on the internet - they are very common and simple).

Then, programmatically add the RadioButtons to the RadioGroup using addView() to render them. Use the results of your random function to determine the "index" parameter of the addView() function.

Your random function would ensure that each time the radio buttons are rendered, their order remains random.

UPDATE

To make things clearer, suppose your random function returns the following order :-

2 1 4 3

Now, what you need to do is to call addView() four times with "index" parameter taking up the above values in order.

Ex :-

addView(radioButton2);
addView(radioButton1);
addView(radioButton4);
addView(radioButton3);
Rohan Stark
  • 2,346
  • 9
  • 16
  • Thanks !! so as the parameters i must have an array of radioButtons ? am i right ? – Shahin Ghasemi Sep 16 '17 at 09:07
  • Um, I don't think you can have an array as a parameter. You don't need an array anyway. Let me update my answer so it's clearer! – Rohan Stark Sep 16 '17 at 09:12
  • it seems it doesn't work, because generated numbers must inserted in order. like 0,1,2,3 otherwise it throw an unbound except – Shahin Ghasemi Sep 16 '17 at 13:24
  • Oh, well I was going with the documented approach in in the [addView()](https://developer.android.com/reference/android/widget/RadioGroup.html#addView(android.view.View,%20int,%20android.view.ViewGroup.LayoutParams)) description. But, you're right - a simple `addView(radioButton)` would be better. – Rohan Stark Sep 16 '17 at 14:47
0

Explanation of Random Swapping part of the post: https://stackoverflow.com/a/46252387/6195457

  //Random Swapping
        //This loop iterates the explained process 4 times which is inside it
        for (int i = 0; i < 4; i++) {

             /*0,1,2,3 can be output for ((int) (Math.random() * 10) % 4)
              *Math.random returns a number in between 0 to 1 
              *we multiply it with 10 to make the number from 0 to 10
              *Taking %4 will return a number from 0 to 3*/ 
            int swap_ind1 = ((int) (Math.random() * 10) % NUMBER_OF_RADIOBUTTONS_TO_ADD);

            //Getting second random index as above
            int swap_ind2 = ((int) (Math.random() * 10) % NUMBER_OF_RADIOBUTTONS_TO_ADD);

            //Swapping the RadioButtons at Random indexes above
            RadioButton temp = radioButton[swap_ind1];//storing value of first index in temp
            radioButton[swap_ind1] = radioButton[swap_ind2];//Putting second value at first index
            radioButton[swap_ind2] = temp;// putting first value at second index from temp
        }