Reputation: 936
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.
Regards Amit Sharma
Upvotes: 3
Views: 1678
Reputation: 13272
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:
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);
// ...
}
}
Upvotes: 2