1

So what I want to achieve is to change the position of my buttons. this is my sample picture:

Note:
My buttons have background drawable, for example sake, I just uploaded without the background images on buttons.

Default
de

When shuffled:

enter image description here

So this is the code I'm working on:

        List<Integer> objects = new ArrayList<Integer>();
        objects.add(0);
        objects.add(1);
        objects.add(2);
        objects.add(3);
        objects.add(4);

    // Shuffle the collection
    Collections.shuffle(objects);

    List<Button> buttons = new ArrayList<Button>();

    buttons.add((Button) findViewById(R.id.bObject1Stage2_1));
    buttons.add((Button) findViewById(R.id.bObject2Stage2_1));
    buttons.add((Button) findViewById(R.id.bObject3Stage2_1));
    buttons.add((Button) findViewById(R.id.bObject4Stage2_1));
    buttons.add((Button) findViewById(R.id.bObject5Stage2_1));

    Collections.shuffle(buttons);

    for (int i = 0; i < objects.size(); i++) {
        //buttons.get(i).setText(objects.get(i).toString());
        buttons.get(i);
    }

But it's not changing the position of buttons. What am I missing in here? Any help is truly appreciated. Thanks.

neknek mouh
  • 1,802
  • 8
  • 27
  • 58

1 Answers1

1

Use RelativeLayout.LayoutParams to switch values between buttons You wish to move and apply them using setLayoutParams method

This will allow You to switch position of 2 buttons. To shuffle them You have to prepare some algorithm to make number of switches.

RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) b1.getLayoutParams();
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) b2.getLayoutParams();

b1.setLayoutParams(params2);
b2.setLayoutParams(params1);

This will work only if these button are not relative to each other. If for example b2 was relative to b1 so it is below it You would have to set b1 to be below b1 using addRule method and removeRule on b2 so it is not relative to b1 any more.

for example You have in xml

 <Button
android:id="@+id/bObject2Stage2_1"
/*...*/
android:layout_toRightOf="@+id/bObject1Stage2_1" />

to set this rule You would write

params.addRule(RelativeLayout.RIGHT_OF, R.id.bObject1Stage2_1);
Gustek
  • 3,680
  • 2
  • 22
  • 36
  • can you give me tutorials so that could get me started with switching position of 2 buttons? – neknek mouh Apr 29 '13 at 12:32
  • I don't have link to any tutorial but this is simple. Inmost cases You just have to [getLayoutParams](http://developer.android.com/reference/android/view/View.html#getLayoutParams%28%29) for each of button and pass it to other one. Because You use RelativeLayout in case when these buttons will be realtive to each other You will have to edit relativeTo params before setting them to view – Gustek Apr 29 '13 at 12:39
  • Check it now, hope this helps. – Gustek Apr 29 '13 at 13:06
  • Thanks, if you say that i have to add addRule in relativelayout, do you think I should change my layout to linearlayout? – neknek mouh Apr 29 '13 at 13:09
  • I don't know, choose one that fits Your needs and You feel comfortable with. – Gustek Apr 29 '13 at 13:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29115/discussion-between-neknek-mouh-and-gustek) – neknek mouh Apr 29 '13 at 13:12