Created
May 22, 2017 15:14
-
-
Save ssaurel/9abb8ea5a5bf2e0a84bcbecb9272236c to your computer and use it in GitHub Desktop.
Main Activity for the Roll Dice Game on the SSaurel's Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssaurel.dicer; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import java.util.Random; | |
public class MainActivity extends AppCompatActivity { | |
public static final Random RANDOM = new Random(); | |
private Button rollDices; | |
private ImageView imageView1, imageView2; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
rollDices = (Button) findViewById(R.id.rollDices); | |
imageView1 = (ImageView) findViewById(R.id.imageView1); | |
imageView2 = (ImageView) findViewById(R.id.imageView2); | |
rollDices.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
int value1 = randomDiceValue(); | |
int value2 = randomDiceValue(); | |
int res1 = getResources().getIdentifier("dice_" + value1, "drawable", "com.ssaurel.dicer"); | |
int res2 = getResources().getIdentifier("dice_" + value2, "drawable", "com.ssaurel.dicer"); | |
imageView1.setImageResource(res1); | |
imageView2.setImageResource(res2); | |
} | |
}); | |
} | |
public static int randomDiceValue() { | |
return RANDOM.nextInt(6) + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment