Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
How to Build a Dice Game in Java Android?
Next article icon

How to Build a Dice Game in Java Android?

Last Updated : 03 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will be building a Dice Game Project using Java/Kotlin and XML in Android. The Dice Game is based on a two-player dice game. Both dices are rolled and the winner result is displayed in a textview. There will be a single activity in this application. The result and a roll button will be displayed at the bottom.

Note that we are going to implement this project using Java and Kotlin.

How-to-Build-a-Dice-Game-in-Java-Android_


Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2:Add Dice images to drawable folder

All the dice images are listed below. Save them in your drawable folder in resources. Go to the app > res > drawable and paste the following files:

  • Dice 1
  • Dice 2
  • Dice 3
  • Dice 4
  • Dice 5
  • Dice 6

Step 3: Working with the activity_main.xml file

The XML codes are used to build the structure of the activity as well as its styling part. It contains a TextView at the very top of the activity. Then it contains two ImageView, each displaying the image of the rolled dice. At the bottom of the activity, there is a Button to roll the dice and a TextView to display the result.

Below is the code for the activity_main.xml file.

activity_main.xml:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout      xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@color/white">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginTop="32dp"         android:text="DICE"         android:textColor="@color/black"         android:textSize="32sp"         android:textStyle="bold"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent" />      <TextView         android:id="@+id/textView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginBottom="16dp"         android:text="Player A"         android:textColor="@color/black"         android:textSize="24sp"         android:textStyle="bold"         app:layout_constraintBottom_toTopOf="@+id/firstDice"         app:layout_constraintEnd_toEndOf="@+id/firstDice"         app:layout_constraintStart_toStartOf="@+id/firstDice" />      <TextView         android:id="@+id/textView2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Player B"         android:textColor="@color/black"         android:textSize="24sp"         android:textStyle="bold"         app:layout_constraintBottom_toBottomOf="@+id/textView"         app:layout_constraintEnd_toEndOf="@+id/secondDice"         app:layout_constraintStart_toStartOf="@+id/secondDice"         app:layout_constraintTop_toTopOf="@+id/textView" />       <ImageView         android:id="@+id/firstDice"         android:layout_width="140dp"         android:layout_height="140dp"         android:padding="10dp"         android:src="@drawable/dice6"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toStartOf="@+id/secondDice"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent" />      <ImageView         android:id="@+id/secondDice"         android:layout_width="140dp"         android:layout_height="140dp"         android:padding="10dp"         android:src="@drawable/dice6"         app:layout_constraintBottom_toBottomOf="@+id/firstDice"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toEndOf="@id/firstDice"         app:layout_constraintTop_toTopOf="@+id/firstDice" />      <Button         android:id="@+id/rollButton"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_margin="48dp"         android:backgroundTint="@color/black"         android:text="ROLL"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent" />      <TextView         android:id="@+id/winnerTextView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Draw"         android:textColor="@color/black"         android:textSize="24sp"         android:textStyle="bold"         app:layout_constraintBottom_toTopOf="@+id/rollButton"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/firstDice" />  </androidx.constraintlayout.widget.ConstraintLayout> 


Step 4: Working with the MainActivity file

We will create an array inside the file that will contain all the images of the dice. Then we will call onClickListener() for the button and generate two random numbers using the Random function. Then we will check the two numbers and display the result respectively. Also, we will set the two images from the array. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.

MainActivity File:

MainActivity.java
package org.geeksforgeeks.demo;  import android.os.Bundle; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.Random;  public class MainActivity extends AppCompatActivity {      private Button button;     private ImageView firstDice;     private ImageView secondDice;     private TextView winnerTextView;     private Random random = new Random();      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // array to store dice images         int[] dice = {                 R.drawable.dice1, R.drawable.dice2, R.drawable.dice3,                 R.drawable.dice4, R.drawable.dice5, R.drawable.dice6         };          // get the references of views         button = findViewById(R.id.rollButton);         firstDice = findViewById(R.id.firstDice);         secondDice = findViewById(R.id.secondDice);         winnerTextView = findViewById(R.id.winnerTextView);          // call the on click function         button.setOnClickListener(v -> {             // generate two random numbers             // using Random function             int num1 = random.nextInt(6);             int num2 = random.nextInt(6);              // set the images from the array by the index             // position of the numbers generated             firstDice.setImageResource(dice[num1]);             secondDice.setImageResource(dice[num2]);              if (num1 > num2) {                 winnerTextView.setText("Player A Wins");             } else if (num1 < num2) {                 winnerTextView.setText("Player B Wins");             } else {                 winnerTextView.setText("Draw");             }         });     } } 
MainActivity.kt
package org.geeksforgeeks.demo  import android.os.Bundle import android.widget.Button import android.widget.ImageView import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.util.Random  class MainActivity : AppCompatActivity() {     private lateinit var button: Button     private lateinit var firstDice: ImageView     private lateinit var secondDice: ImageView     private lateinit var winnerTextView: TextView     private val random = Random()      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // array to store dice images         val dice = intArrayOf(             R.drawable.dice1, R.drawable.dice2, R.drawable.dice3,             R.drawable.dice4, R.drawable.dice5, R.drawable.dice6         )          // get the references of views         button = findViewById(R.id.rollButton)         firstDice = findViewById(R.id.firstDice)         secondDice = findViewById(R.id.secondDice)         winnerTextView = findViewById(R.id.winnerTextView)          // call the on click function         button.setOnClickListener {             // generate two random numbers             // using Random function             val num1 = random.nextInt(6)             val num2 = random.nextInt(6)              // set the images from the array by the index             // position of the numbers generated             firstDice.setImageResource(dice[num1])             secondDice.setImageResource(dice[num2])              winnerTextView.text = if (num1 > num2) {                 "Player A Wins"             } else if (num1 < num2) {                 "Player B Wins"             } else {                 "Draw"             }         }     } } 

Output:



Next Article
How to Build a Dice Game in Java Android?

N

namanjha10
Improve
Article Tags :
  • Java
  • Android
  • Kotlin Android
  • Android Projects
  • Java-Android
Practice Tags :
  • Java

Similar Reads

    How to Build a Tic Tac Toe Game in Android?
    In this article, we will be building a Tic Tac Toe Game Project using Java and XML in Android. The Tic Tac Toe Game is based on a two-player game. Each player chooses between X and O. The Player plays one move at a time simultaneously. In a move, a player can choose any position from a 3x3 grid. The
    8 min read
    How to Build a Simple Reflex Game in Android?
    A reflex game is a simple fun game that measures your responding speed. It is quite simple to make and understand. We will be designing a reflex game that will calculate your responding speed. The rules are simple just press the stop button when you see the change in background color, and the time y
    4 min read
    How to Build a Simple Swiping Game in Android?
    In this article, we are going to create a simple swiping game using RecyclerView in Android Studio. RecyclerView is the ViewGroup that contains the views corresponding to your data. It's a view itself, so you add RecyclerView into your layout the way you would add any other UI element. We are going
    6 min read
    How to Create a Quiz App In Android?
    Android is an operating system which is basically made for Mobile phones. It is based on the Linux Kernel and other open-source software and is developed by Google. Android is very popular nowadays among students and students are now choosing Android for their projects. It's very much important for
    7 min read
    How to Generate a PDF file in Android App?
    There are many apps in which data from the app is provided to users in the downloadable PDF file format. So in this case we have to create a PDF file from the data present inside our app and represent that data properly inside our app. So by using this technique, we can easily create a new PDF accor
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences