Suppose I have n Button s. And I have to arrange them on the screen in a random order. They should not intersect or cover each other. After a little search for an answer, I found a solution here. But this did not help me much. Here is my code:

  setContentView(R.layout.activity_main); LinearLayout l = (LinearLayout)findViewById(R.id.root) ; DisplayMetrics displaymatrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymatrics); for(int i = 1 ; i<=n ; i++) { Button b = new Button(this) ; b.setText("A"); b.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(65),dpToPx(65))); Random R = new Random(); float dx = R.nextFloat() * displaymatrics.widthPixels; float dy = R.nextFloat() * displaymatrics.heightPixels; b.animate().x(dx).y(dy).setDuration(0).start(); l.addView(b); } } private int dpToPx(int dp) { return (int) (dp * Resources.getSystem().getDisplayMetrics().density); } 

But the problem is that a lot of buttons are hidden outside the screen or intersect at all. How to solve this kind of problem?

And also tried this method. But not all buttons are visible.

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout l = (LinearLayout)findViewById(R.id.root) ; DisplayMetrics displaymatrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymatrics); Log.d("tag" , "width and height: " + displaymatrics.widthPixels + " " + displaymatrics.heightPixels) ; for(int i = 1 ; i<=10 ; i++) { Button b = new Button(this) ; b.setText(i+" "); b.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(65),dpToPx(65))); Random R = new Random(); int dx = R.nextInt(displaymatrics.widthPixels - dpToPx(65)) ; int dy = R.nextInt(displaymatrics.heightPixels - dpToPx(65)); Log.d("tag","dx,dy:" + " " + dx + " " + dy ) ; b.animate().x(dx).y(dy).setDuration(0).start(); l.addView(b); } } 
  • one
    I have n Button s. And I have to place them on the screen in a random order. The first question is the size of the screen and the long loafs are such that the long loafs fit on the screen, if not scattered, or is it possible that the last (or even the last) long loaf will have nowhere to squeeze? The second question - should the position be completely random, or is some discreteness of the grid nodes valid? - Akina
  • @Akina The number of buttons is a maximum of 15 and their sizes are not so large. They are guaranteed to fit. The position should be completely random and the buttons should not intersect or cover each other or go beyond the screen. My parameters: displaymatrics.widthPixels = 720; displaymatrics.heightPixels = 1280; displaymatrics.widthPixels = 720; displaymatrics.heightPixels = 1280; Following the advice of @Asidert, he did as he and all the generated numbers are less than the parameters on top. But many buttons are not visible. Why? - abay
  • one
    To exclude intersections, you can use the approach of registration of occupied regions. For 720x1280, the array of busy regions is not very large. For the next loaf, one coordinate is generated - the ordinal linear number of the unoccupied region (from 1 to 720 * 1280 minus the number of occupied coordinates). After the next coordinate is generated, all the coordinates occupied by the loaf, as well as coordinates, when it is placed in one more loaf it intersects with the current one, are marked as inaccessible. - Akina

1 answer 1

To solve the problem of leaving the buttons behind the screen:

Change the LinearLayout to RelativeLayout , then:

 Random R = new Random(); int dx = R.nextInt(displaymatrics.widthPixels-dpToPx(65)); int dy = R.nextInt(displaymatrics.heightPixels-dpToPx(65)); //Это будут отступы от левого края и верхнего //65 - твои размеры кнопки RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(dpToPx(65), dpToPx(65)); params.setMargins(dx, dy, 0, 0); btn.setLayoutParams(params); 
  • Did not help. I wrote this: int dx = R.nextInt(displaymatrics.widthPixels - dpToPx(65)) ; int dy = R.nextInt(displaymatrics.heightPixels - dpToPx(65)); int dx = R.nextInt(displaymatrics.widthPixels - dpToPx(65)) ; int dy = R.nextInt(displaymatrics.heightPixels - dpToPx(65)); Why nextInt use? - abay
  • @ andy11 int or float doesn't really matter, but does it make sense to use fractional numbers where they are not needed? How exactly did you determine what didn't help? - Asidert
  • Of the 10 Button-s only 2-3 are visible. The rest of the screen. Some are half visible. Device width and height: 720x1280 And also all generated numbers do not exceed them. So what could be the problem? - abay
  • @andy11 show the whole code for adding buttons to the screen. - Asidert
  • updated the question. - abay