Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Main Activity for the Roll Dice Game on the SSaurel's Channel
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
You can’t perform that action at this time.