3

Please let me know that how i can add the views like images or button inside a relative layout. But on random position , Max number of images can be 10.

Please help me out. I am attaching a view how i want the final output.

enter image description here

Regards Amit Sharma

Amit Sharma
  • 926
  • 2
  • 10
  • 35
  • 1
    Hello Amit you can try out this. https://stackoverflow.com/questions/22144690/how-to-display-a-button-in-random-screen-position – Deepak Rana Jul 31 '17 at 13:34
  • Hello Deepak , Thanks for the answer but i need inside a layout i have tried this but it is not helpful for me thanks for the help – Amit Sharma Jul 31 '17 at 13:35

1 Answers1

2

In this case you should consider an AbsoluteLayout, it would make more sense. Since this way you can randomly generate and x and an y position for each child.

There are a ton of examples on the net, here is the first one found by a Google search. The gist is something like this:

To plot something like this on the screen:

AbsoluteLayout

You can have an AbsoluteLayout declared like this in your activity xml file:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   
<!--  Declare your shapes --> 

</AbsoluteLayout>

And then in your Activity or Fragment you will add your Shape objects at random positions, with something like:

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // ...
  // Establish the working area
  DisplayMetrics displayMetrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
  int widthArea = displayMetrics.widthPixels;
  int heightArea = displayMetrics.heightPixels;
  //Init the random generator
  Random r = new Random();
  // Then for each Shape  
  // ...
  AbsoluteLayout.LayoutParams pos = 
   (AbsoluteLayout.LayoutParams)shape.getLayoutParams();
  pos.x =  r.nextInt(widthArea);
  pos.y =  r.nextInt(heightArea);
  shape.setLayoutParams(pos);
  // ...
 }  
}
dan
  • 13,132
  • 3
  • 38
  • 49
  • Thanks sir , Do you have any working sample or a chunk of code so that i can work on that. – Amit Sharma Jul 31 '17 at 13:36
  • Hi @AmitSharma, I updated my answer with some more details, hope it helps. – dan Jul 31 '17 at 13:54
  • Thanks sir let me check , Is shape variable is the absolute layout ? – Amit Sharma Jul 31 '17 at 14:00
  • sorry got it shape is the view that we want to add , am i rihgt ? – Amit Sharma Jul 31 '17 at 14:02
  • @AmitSharma Not sure what do you mean with: "Is shape variable is the absolute layout", but if you asked if the shaper are declared in the `xml`, that is one option, you can declare them programmatically in your `Activity` and use `addView(shape, pos);` method, to add them at runtime. – dan Jul 31 '17 at 14:02
  • @AmitSharma Yes, `shape` is the view that you are trying to add randomly on the screen. – dan Jul 31 '17 at 14:03
  • i have just added a answer but it is not working , Please let me know what i am doing wrong – Amit Sharma Jul 31 '17 at 14:07
  • Caused by: java.lang.NullPointerException at com.abc.view.activities.splash.SplashActivity.onCreate(SplashActivity.java:46) pos.x = r.nextInt(widthArea); on this line – Amit Sharma Jul 31 '17 at 14:08
  • @AmitSharma Shape should be your `view` not another `AbsoluteLayout`. `AbsoluteLayout` is a view too, but probably your shape is not an `AbsoluteLayout`. In your sample the issue is probably because you are using a `button` variable that is not initialized, instead of the `shape` variable, when creating the `pos` object. – dan Jul 31 '17 at 14:11
  • I Got your point , it is working now but absolute layout has no boundries i think so sometimge view go out of the view – Amit Sharma Jul 31 '17 at 14:15
  • @AmitSharma Yes, glad it worked. To constraint your shaped within the screen you should ensure that the random position is not to close to the edge or outside. You can use the reminder (`%`) of the division of `x` and `y` with `widthArea` and `heightArea`. – dan Jul 31 '17 at 14:18