Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For my project I am creating a dice game (Yahtzee) for school. For my buttons I added them in XML </p> <pre><code>&lt;ImageButton android:id = "@+id/dice1" android:src = "@drawable/red_1_dice" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "60px" android:layout_marginLeft = "30px" /&gt; &lt;ImageButton android:id = "@+id/dice2" android:src = "@drawable/red_2_dice" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginLeft = "250px" android:layout_marginTop = "-130px"/&gt; &lt;ImageButton android:id = "@+id/dice3" android:src = "@drawable/red_3_dice" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginLeft = "135px" android:layout_marginTop = "20px" /&gt; </code></pre> <p>then in my main i did this.</p> <pre><code>public class Z_veselinovic_yahtzeeActivity extends Activity { /** Called when the activity is first created. */ ImageButton button1, button2, button3, button4, button5; Button start, reroll, hold; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Buttons(); } public void Buttons() { button1 = (ImageButton)findViewById(R.id.dice1); button2 = (ImageButton)findViewById(R.id.dice2); button3 = (ImageButton)findViewById(R.id.dice3); button4 = (ImageButton)findViewById(R.id.dice4); button5 = (ImageButton)findViewById(R.id.dice5); start = (Button)findViewById(R.id.Start); reroll = (Button)findViewById(R.id.Reroll); hold = (Button)findViewById(R.id.Hold); reroll.setVisibility(View.GONE); hold.setVisibility(View.GONE); start.setOnClickListener(new OnClickListener() { public void onClick(View whatever) { Toast.makeText(getBaseContext(), start.getText() + " Game", Toast.LENGTH_LONG).show(); Random rand1 = new Random(); Random rand2 = new Random(); Random rand3 = new Random(); Random rand4 = new Random(); Random rand5 = new Random(); int dice_num_1 = rand1.nextInt(6) + 1; int dice_num_2 = rand2.nextInt(6) + 1; int dice_num_3 = rand3.nextInt(6) + 1; int dice_num_4 = rand4.nextInt(6) + 1; int dice_num_5 = rand5.nextInt(6) + 1; if(dice_num_1 == 1) { button1.setImageResource(R.drawable.red_1_dice); } else if(dice_num_1 == 2) { button1.setImageResource(R.drawable.red_2_dice); } else if(dice_num_1 == 3) { button1.setImageResource(R.drawable.red_3_dice); } else if(dice_num_1 == 4) { button1.setImageResource(R.drawable.red_4_dice); } else if(dice_num_1 == 5) { button1.setImageResource(R.drawable.red_5_dice); } else if(dice_num_1 == 6) { button1.setImageResource(R.drawable.red_6_dice); } if(dice_num_2 == 1) { button2.setImageResource(R.drawable.red_1_dice); } else if(dice_num_2 == 2) { button2.setImageResource(R.drawable.red_2_dice); } else if(dice_num_2 == 3) { button2.setImageResource(R.drawable.red_3_dice); } else if(dice_num_2 == 4) { button2.setImageResource(R.drawable.red_4_dice); } else if(dice_num_2 == 5) { button2.setImageResource(R.drawable.red_5_dice); } else if(dice_num_2 == 6) { button2.setImageResource(R.drawable.red_6_dice); } </code></pre> <p>I hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload