Reputation: 39558
My request is quite simple: I just want to display some text at a random place of the screen.
I was expecting, as I used:
int x = r.nextInt(width - layout.getWidth());
and
int y = r.nextInt(height - layout.getHeight());
that my layout would perfectly fit the screen.
Unfortunately, my text is sometimes at the right of the screen and appear in multiple lines!
Here is my code
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
layout.addView(clock);
layout.addView(txtView);
Random r = new Random();
int x = r.nextInt(width - layout.getWidth());
int y = r.nextInt(height - layout.getHeight());
layout.setPadding(x, y, 0, 0);
setContentView(layout);
Upvotes: 2
Views: 120
Reputation: 39558
The big problem I had was that
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
returns a value including the top statusBar (or notification, don't know how to call it) and the software buttons at the bottom.
The solution was to create a dummy layout in background and measure it.
Upvotes: 0
Reputation: 75629
Use RelativeLayout
as your container instead of LinearLayout
as this is what is is designed for.
Upvotes: 1