Develop an Android Dice Application using Android Studio a Complete Tutorial
A very simple Android application suited for a beginner who wanted to start mobile development, so the concept of the game is simple, roll the dice, display image view for the number shown on the dice and a voice telling the number.
Let’s get started
App details
App Name:
Dice
Package Name:
com.sutanaryan.dice
Activity:
MainActivity
manifests/
|--AndroidManifests.xml
res/
So for the animation, to make our Application simple and easiest as much as possible we only use predefined XML values animation, though it won’t perfectly match the coded animation it does the job done.
Please add these directories and files that didn’t exist yet on your project.
|--anim/
┌----bounce.xml // This animation is used to bounce the dice number
└----move.xml // This animation is used to move the dice on all corners
|--drawable/
┌----craps.gif // This image is used as our primary App background image
|----d1.png // These lists of d{#}.png files are used to display random dice number.
|----d2.png
|----d3.png
|----d4.png
|----d5.png
|----d6.png
|----n1.png // These lists of d{#}.png files are used to display winner number.
|----n2.png
|----n3.png
|----n4.png
|----n5.png
└----n6.png
|--layout/
|----activity_main.xml // This file contains our App main activity
|--raw/
┌----shake_dice.mp3 // This audio is used once the dice is shake or rolling
|----one.mp3 // These list sof audio files are used to tell the winner number.
|----two.mp3
|----three.mp3
|----four.mp3
|----five.mp3
└----six.mp3
|--values/
┌----colors.xml
|----strings.xml
└----styles.xml
AndroidManifest.xml
You can left this file untouch.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sutanaryan.dice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
layout/activity_main.xml
Our main activity file, this layout will be used on the App onload as defined in the manifest
file above.
So here we just simply assigned click event directly to the ImageView via onClick
parameters.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.sutanaryan.dice.MainActivity"
android:background="@drawable/craps">
<!--this will be used to display dice number-->
<ImageView
android:id="@+id/dice_number"
android:layout_width="109dp"
android:layout_height="119dp"
android:src="@drawable/n1"
app:layout_constraintRight_toRightOf="parent" />
<!--dice image placeholder-->
<ImageView
android:id="@+id/dice_picture"
android:layout_width="114dp"
android:layout_height="120dp"
android:src="@drawable/d1"
android:onClick="HandleClick"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="1.0"
app:layout_constraintHorizontal_bias="0.0" />
</android.support.constraint.ConstraintLayout>
anim/bounce.xml
This animation will be used to bounce dice number.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/bounce_interpolator">
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
anim/bounce.xml
This animation will be used to move the dice on all corners of the screen.
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<rotate
android:fromDegrees="0"
android:toDegrees="2400"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2400"
android:repeatMode="restart"
android:repeatCount="0"
android:interpolator="@android:anim/cycle_interpolator"/>
<!--bottom to top-->
<translate
android:fromYDelta="0%p"
android:toYDelta="-80%p"
android:duration="600" />
<!--left to right-->
<translate
android:fromXDelta="0%p"
android:toXDelta="74%p"
android:duration="600"
android:startOffset="600" />
<!--top to bottom-->
<translate
android:fromYDelta="0%p"
android:toYDelta="80%p"
android:duration="600"
android:startOffset="1200"/>
<!--right to left-->
<translate
android:fromXDelta="0%p"
android:toXDelta="-75%p"
android:duration="600"
android:startOffset="1800" />
</set>
MainAcivity.java
Complete code for our MainActivity class
package com.sutanaryan.dice;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import java.util.Random;
public class MainActivity extends Activity {
ImageView dice_picture, dice_number; // reference to dice picture
Random rWinner = new Random(); // generate random numbers
SoundPool dice_sounds = new SoundPool(1, AudioManager.STREAM_MUSIC,0);
// Used to control sound stream return by SoundPool
int random_number;
int sound_shake, sound_dice, number_dice;
int sound1, sound2, sound3, sound4, sound5, sound6;
boolean rolling = false; // This check if the dice is rolling?
private Animation moveAnimation;
private Animation bounceAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load dice sound
sound_shake = dice_sounds.load(this,R.raw.shake_dice,1);
sound1 = dice_sounds.load(this,R.raw.one,1);
sound2 = dice_sounds.load(this,R.raw.two,1);
sound3 = dice_sounds.load(this,R.raw.three,1);
sound4 = dice_sounds.load(this,R.raw.four,1);
sound5 = dice_sounds.load(this,R.raw.five,1);
sound6 = dice_sounds.load(this,R.raw.six,1);
// get reference to image widget
dice_picture = (ImageView) findViewById(R.id.dice_picture);
dice_number = (ImageView) findViewById(R.id.dice_number);
// load dice move animation
moveAnimation = AnimationUtils.loadAnimation(this, R.anim.move);
moveAnimation.setRepeatCount(3); // animation repeats 3 times
// We set Animation Listenser so we can check when the animation Start, End, and Repeat
// Once the Animation is ended we then display ImageView winner place holder and an audio
moveAnimation.setAnimationListener( animationListener );
// Load bounce animation
bounceAnimation = AnimationUtils.loadAnimation(this, R.anim.bounce);
}
// User pressed dice, lets start
public void HandleClick(View arg0) {
if(! rolling) {
rolling = true;
// DISABLE animation
dice_picture.startAnimation( moveAnimation );
dice_picture.setImageResource(R.drawable.d1);
// Start rolling sound
dice_sounds.play(sound_shake, 1.0f, 1.0f, 0, 0, 1.0f);
random_number = rWinner.nextInt(6) + 1;
switch (random_number) {
case 1:
dice_picture.setImageResource(R.drawable.d1);
number_dice = R.drawable.n1;
sound_dice = sound1;
break;
case 2:
dice_picture.setImageResource(R.drawable.d2);
number_dice = R.drawable.n2;
sound_dice = sound2;
break;
case 3:
dice_picture.setImageResource(R.drawable.d3);
number_dice = R.drawable.n3;
sound_dice = sound3;
break;
case 4:
dice_picture.setImageResource(R.drawable.d4);
number_dice = R.drawable.n4;
sound_dice = sound4;
break;
case 5:
dice_picture.setImageResource(R.drawable.d5);
number_dice = R.drawable.n5;
sound_dice = sound5;
break;
case 6:
dice_picture.setImageResource(R.drawable.d6);
number_dice = R.drawable.n6;
sound_dice = sound6;
break;
default:
}
rolling=false; //user can press again
}
}
private AnimationListener animationListener = new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
dice_number.setVisibility(View.GONE);
// Clear animation on start animation so the number back to original state
dice_number.clearAnimation();
}
@Override
public void onAnimationEnd(Animation animation) {
dice_picture.clearAnimation();
// Play dice sounds
dice_sounds.play(sound_dice,1.0f,1.0f,0,0,1.0f);
// Display dice number and start animation
dice_number.setImageResource(number_dice);
dice_number.startAnimation( bounceAnimation );
}
@Override
public void onAnimationRepeat(Animation animation) {
// Do nothing
}
};
}
values/colors.xml
You can left this file ontouch.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
values/strings.xml
You can left this file ontouch.
<resources>
<string name="app_name">Dice</string>
</resources>
values/strings.xml
You can left this file ontouch.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
Resources
For the audio
and images
you may download any from your preferred sources, Google Images or something.
That’s it, happy coding ^_^