Search code examples
androidrandomandroid-relativelayoutimagebuttonabsolute

How to randomly position a button in a layout?


I was wondering how to randomly position an ImageButton anywhere around the screen.

I have tried using a RelativeLayout but nothing has worked so far.

Any suggestions?


Solution

  • Use a FrameLayout container and set the child view's position using setTop() and setLeft() to achieve an offset from the top left corner. If that doesn't work also try setting the marginTop and marginLeft on the child view's layout paramters.