0

So I'm trying to appear 3 buttons on a random position. First im giving them a random position based on the screen size using this code:

    float x = new Random().nextInt(metrics.widthPixels / 2);
    float y = new Random().nextInt(metrics.heightPixels / 2);
    lbutton1.setX(x);
    lbutton1.setY(y);
    float x1 = new Random().nextInt(metrics.widthPixels / 2);
    float y1 = new Random().nextInt(metrics.heightPixels / 2);
    lbutton2.setX(x1);
    lbutton2.setY(y1);
    float x2 = new Random().nextInt(metrics.widthPixels / 2);
    float y2 = new Random().nextInt(metrics.heightPixels / 2);
    lbutton3.setX(x2);
    lbutton3.setY(y2);

Buttons at this point are set GONE in .xml file. When I trying to call their method so they become VISIBLE im doing it with using this code:

    lbutton1.setVisibility(View.VISIBLE);
    lbutton2.setVisibility(View.VISIBLE);
    lbutton3.setVisibility(View.VISIBLE);

Problem is that while button1 appears in the position set by setX(x),setY(y), the other 2 buttons X position (from what i can tell) does not take the value of x1,x2 but it takes random values that I cant explain where they are taken from. All buttons Y position is ok.. Any ideas why could this be happening?

Bith
  • 43
  • 4
  • `View.GONE` by definition will completely remove the view instead of just hiding it. I do believe that it is meant not to respond to view changed when in this state. You should be using `View.INIVISIBLE` if you want them hidden but capable of updates if I am remembering correctly. – Jay Snayder Mar 27 '15 at 11:25
  • If GONE was the problem shouldn't I have it for lbutton1 too? This is the weird thing. lbutton1 works fine.. Also I just tried View.INVISIBLE but nothing changed – Bith Mar 27 '15 at 11:33
  • Basically setX and setY doesn't really work the way people thing it works. Here: http://stackoverflow.com/questions/17411221/why-imageviews-getx-gety-setx-and-sety-do-not-represent-the-actual-value-in-t –  Mar 27 '15 at 11:40
  • @JaySnayder View.GONE makes View invisible plus this View doesn't take any space in layout anymore. but View is NOT removed, so you can change it, and then make vivible. – Yuriy Vasylenko Apr 03 '15 at 12:52

1 Answers1

0

Check your activity_main.xml. Maybe you forgot to delete button's positioning attributes: android:layout_below, android:layout_alignParentLeft, android:layout_alignParentStart etc. + You should use RelativeLayout.

Yuriy Vasylenko
  • 3,031
  • 25
  • 25