-1

im very new to java & android development, I want a textfield that changes 50% to "You win!" or 50% to "You Lose!" when i click/tap a button.

And a image rotating randomly, like it is 0° then it will get a number between 0 and 100 (higher then 50 is clockwise lower then 50 is counter-clockwise)

i might be asking to much xD but i really dont know how i do this. How do i do this ?

I actually dont want very advanced answers because im very new to java. Theres is pretty much nothing in my code except for a button and a textfield.

Julian
  • 3
  • 2

2 Answers2

0

Math.random() method gives you a floating point number between 0-1 but not included 1.

All you have to do is:

int randomNumber = (int)(Math.random()*101);
if(randomNumber < 50)
    ...
else
    ...
Burak Uyar
  • 119
  • 1
  • 5
0

Here a little help to start off. First you should add an OnClickListener to your button:

yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            handleButtonClick();
        }
    });

The handleButtonClick()-method could look like this:

private void handleButtonClick() {
    // Here we get a random integer between 0 and 99 (including 0 and 99)
    int randomNumber = new Random().nextInt(100);
    // Here we check if the random number is even
    boolean isEven = randomNumber % 2 == 0;
    // Now you can do stuff depending on the integer itself (rotation)
    // and depending on whether it's even or not.
    if(randomNumber < 50){
      // do this
    } else {
      // do that
    }

    if(isEven){
      // do this
    } else {
      // do that
    }

}

To change the text you use:

yourText.setText(...);

But this is really basic stuff you could read on Android TextView

And rotating images was discussed here Android: Rotate image in imageview by an angle

Please read tutorials or refer to this before asking every little thing you could easily look up. Feel free to ask again when you get stuck. Good luck.

Community
  • 1
  • 1
G. Kalender
  • 491
  • 4
  • 13