0% found this document useful (0 votes)
48 views

Develop An Application in Android To Make Rolling Dice Game

The document describes how to develop a rolling dice game application in Android. It includes Java code to define the main activity class with methods to generate random dice values and set the images displayed for each dice roll. It also includes XML layout code which defines the user interface with two image views to display the dice and a button to trigger new rolls. When the button is clicked, random values are generated for each die and the corresponding drawable resource is set for each image view.

Uploaded by

Sachin Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Develop An Application in Android To Make Rolling Dice Game

The document describes how to develop a rolling dice game application in Android. It includes Java code to define the main activity class with methods to generate random dice values and set the images displayed for each dice roll. It also includes XML layout code which defines the user interface with two image views to display the dice and a button to trigger new rolls. When the button is clicked, random values are generated for each die and the corresponding drawable resource is set for each image view.

Uploaded by

Sachin Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1. Develop an application in android to make Rolling Dice game.

JAVA code:
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;
}
}

XML code:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ssaurel.dicer.MainActivity">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:orientation="horizontal"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="20dp"
android:src="@drawable/dice_2"/>

<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/dice_4"/>

</LinearLayout>
<Button
android:id="@+id/rollDices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll Dices"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"

android:layout_centerHorizontal="true"/>

</RelativeLayout>

You might also like