Reputation: 31
So I'm new to java, and I have the basics of object placement down. I'm trying to design an app in which 5 objects are randomly placed in a certain activity. How would I go about randomly placing these objects?
Thanks!
Upvotes: 1
Views: 273
Reputation: 3210
you can programmatically add your buttons to your activity with random position without using xml layout and here an example set buttons in activity
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button okButton=new Button(this);
okButton.setText("some text");
Random r = new Random();
okButton.setPadding(r.nextInt(), r.nextInt(), r.nextInt(), r.nextInt())
ll.addView(okButton, layoutParams);
and here good thread to look at
Set margins in a LinearLayout programmatically
Upvotes: 0
Reputation: 7905
Create a custom view that overrides the onDraw method.
Use Math.random or some other random number generating scheme to determine the x and y values of the bitmaps you are going to draw.
Then use Canvas.drawBitmap
Upvotes: 1