Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created May 22, 2017 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/9abb8ea5a5bf2e0a84bcbecb9272236c to your computer and use it in GitHub Desktop.
Save ssaurel/9abb8ea5a5bf2e0a84bcbecb9272236c to your computer and use it in GitHub Desktop.
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