Julian Julian - 1 year ago 68
Android Question

Click on a button to chance textfield 50% chance

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.

Answer Source

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

yourButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

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

      // do this
    } else {
      // do that


To change the text you use:


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.