How to generate text at random position on the screen?

+2 Milan Obrenovic · June 26, 2015
In android, how can I generate a bunch of TextView widgets, randomly positioned on the screen? For now, I have this in onCreate  method:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);

for (int i = 0; i < 10; i++) {
TextView tv = new TextView(this);
params.setMargins(getRandomPosition(), getRandomPosition(), 0, 0);
tv.setLayoutParams(params);
tv.setTextSize(24);
tv.setText("Random Text");
arrayList.add(tv);

mainLayout.addView(arrayList.get(i), params);

Toast.makeText(MainActivity.this, "Added " + i, Toast.LENGTH_SHORT).show();
}

And a method that should be generating random numbers:
private int getRandomPosition() {
Random r = new Random();

// This isn't supposed to be an actual range, it's just for testing
return r.nextInt(150) + 100;
}

This generates text at the random position, but all at the same position. 10 TextView's in the exact same position.

Why? Shouldn't the 
params.setMargins(getRandomPosition(), getRandomPosition(), 0, 0);

be calling the method over again, picking up a new random number?

Post a Reply

Replies

Oldest  Newest  Rating
0 Kuroodo Ditory · June 29, 2015
Here ya go


Random r = new Random();


r.setSeed(System.nanoTime());

int x = r.nextInt();


You can also set a bound/range where you can get a number from 0-n (n being the range).

So if you want a number from 1-6, you do 
int x = r.nextInt(6);

Atleast I think it's 1-6 (might be 0-6 not sure lol).


But looks like the issue isn't the random generation so oops my bad!
0 Milan Obrenovic · June 27, 2015
@Mantas

As I said, that doesn't work. It generates 160 as a random number every time.

@Kenneth

Yeah you're right, but still, it generates it in the exact same place. It isn't off the window. The loop, for example, generates 10 TextView's and the text is really bold on the output, because there's a stack of them on top of each other. If the loop runs only once, then the text is light (normal, standard android text view widget).

My guess is the  TextView tv = new TextView(this);

Maybe I need to make a new textview object every time the loop runs? If so, how?
Or, maybe the  params  object needs to be created every time the loop runs, because by this, it seems like it's picking up the same text view and the same params objects when the loop ran the first time, so it's just doing the exact same thing.
0 Kenneth Malicay · June 26, 2015
@Milan Obrenovic

If I'm not mistaken, your 
r.nextInt(150) + 100;

Generates numbers between 100 - 249.
Maybe you're displaying way off the window or something? I'm not really sure.
0 Mantas Nolife · June 26, 2015
use :
Random r = new Random(Math.random()*99);

to generate random number
0 Milan Obrenovic · June 26, 2015
@Kenneth Malicay

Without that piece of code, it prints the numbers randomly. When I add that code, it is always prints the same number, which is for some reason always 160 (don't know how can there be 160 when the maximum limit i put is 150).. Weird
0 Kenneth Malicay · June 26, 2015
I'm not very sure but try and check if r.nextInt() gets a new random everytime by asigning it to an int variable and printing it in the log. System.out.println() works (at least for me).
If it gets the same random number everytime then try this.
Random r = new Random(Math.random()*99);

I'm not sure this is the best solution and I haven't tried it either so just try if it works for you until you find a better solution.
  • 1

Java / Android Development

107,256 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator