create account

How to build a Dice Roller App for Android: Our First Game - Hive Programmers by skyehi

View this thread on: hive.blogpeakd.comecency.com
· @skyehi ·
$2.28
How to build a Dice Roller App for Android: Our First Game - Hive Programmers
Greetings to my favorite science community online, StemSocial.

It's @skyehi and I'm really excited to be back to continue my series on Android App development tutorials for beginners. 

Yesterday's tutorial on building our first News Reader App got quite a lot of attention and I'm grateful to both StemSocial and @empo.voter for all the support.

> Because I'm so happy about yesterday's post success, I've decided to make more like a bonus tutorial. We'll be building our first Android Game App. This would be a basic dice rolling game. 

There are a lot of dice rolling games where the user presses a "Roll dice" button and gets a number. Some game developers use it for developing Play-to-earn games or gambling sites. Site's like freebitco and other crypto faucet P2E games use the Rolling Dice technique to build their games and platforms.

> Ever wondered how they did it?. Well we'll be developing a dice roller game and you'll see exactly how that works.


![Polish_20231130_153407707.jpg](https://files.peakd.com/file/peakd-hive/skyehi/23tbPiVkwLBkHxkt3nZRofynPNDxRBLN1xbafFoXuAN87YLReVGXHiXzhuHkVjKyECHqF.jpg)[Original Image Source](https://pixabay.com/photos/website-code-html-coding-647013/) by lmonk72 from Pixabay


![YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png](https://files.peakd.com/file/peakd-hive/skyehi/23swrbKc3yjMYSUJMwmHkXUC71AufZXrkQ315XAST7nXzaKhpkvsAScnV3nBnpS55SJ2r.png)



### Prerequisites

For the sake of all the newcomers to my series, before you can build an Android app, you would need to make sure you have Android Studio and Java Development Kit, JDK installed on your computer. 

Here's the link to the main download page. You can download the software from [Android Developer website](https://developer.android.com/studio).

If you're having any issue downloading or installing both softwares on your computer, please let me know in the comments guys. 

### Creating a new Android Studio Project

At this point, I'll assume that my readers have successfully installed Android Studio IDE. 

The first thing we need to do is to create a new Android Studio Project. 

We'll do that by opening Android Studio and clicking on "Start a new Android Studio project" 

On the next page, you will be able to create a name for your App and choose the package name. I would also recommend that you choose Java as the programming language because that's what I'll be using for the beginner's tutorial series. 

As we keep making progress in our series, I'll start using Kotlin which would allow us to create simpler Apps with less bugs.

Please choose "Empty Activity" as the template to make developing the game simpler.


![2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png](https://files.peakd.com/file/peakd-hive/skyehi/EoAYDi9ZKfkBZi7rMWbojnS8KytoTpbSepvPmfeZmL2V2h8FDZ3xRpC58MZDkf1LeRQ.png)


### Designing the Layout

Now that we're all set with creating a new Android studio Project for our app, it's time to work on the frontend design. This is the part that the user will see and interact with. 

The layout design of our app will be done inside the `res/layout/activity_main.xml` file.

We will be creating a simple dice roller app so we will only need a Button the user would use to roll the dice and a TextView to display the result.

> Here's how your code should look like;

```xml
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/rollButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Roll Dice"
        android:layout_centerInParent="true"/>

    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result will be displayed here."
        android:layout_below="@id/rollButton"
        android:layout_marginTop="16dp"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>
```

### Creating the logic of our game

Our Dice app would have random numbers from "1" to "6". When the user presses the button, any of those numbers can be shown as a result.

In more advanced projects, we would create a reward system where the user can earn points based on the results of the dice rolling.

It's a really basic code and after finishing our project, I'm hopeful that you'll be proud of your first Android dice game.

The logic code of our app will be written inside `MainActivity.java` 

> Here's how your code should look like guys. 


```java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private Button rollButton;
    private TextView resultTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rollButton = findViewById(R.id.rollButton);
        resultTextView = findViewById(R.id.resultTextView);

        rollButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rollDice();
            }
        });
    }

    private void rollDice() {
        Random random = new Random();
        int diceResult = random.nextInt(6) + 1; // Generate a random number between 1 and 6
        resultTextView.setText("Dice Result: " + diceResult);
    }
}
```


![2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png](https://files.peakd.com/file/peakd-hive/skyehi/EoAYDi9ZKfkBZi7rMWbojnS8KytoTpbSepvPmfeZmL2V2h8FDZ3xRpC58MZDkf1LeRQ.png)


### Running the Dice Roller Game

Congratulations guys, we just finished building a great and simple Dice Roller App. It's now time to see the results and play the game. 

To run the app, you can either connect your Android device or use an emulator. Like I always say, I personally prefer running my Apps on my physical Android device so I'll see how the App would look on an actual phone.

Please do not forget to turn on USB debugging in order to be able to run the App on your device. 

When the app launches, click the "Roll Dice" button, and you should see the result displayed in the TextView.


![YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png](https://files.peakd.com/file/peakd-hive/skyehi/23swrbKc3yjMYSUJMwmHkXUC71AufZXrkQ315XAST7nXzaKhpkvsAScnV3nBnpS55SJ2r.png)


Thank you so much for taking the time to read today's tutorial blog. I hope you enjoyed today's tutorial guys. We are gradually make progress and we'll soon build more complex Apps that you'll be proud of.

As always guys, if you're having troubles writing the code, installing the IDE or JDK or even running the App, please let me know in the comments section and I'll be of help.

> Have a lovely day and catch you next time on StemSocial. Goodbye ✍️

<center>
You Can Follow Me @skyehi For More Like This And Others 
</center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 169 others
properties (23)
authorskyehi
permlinkhow-to-build-a-dice-roller-app-for-android-our-first-game-hive-programmers
categoryhive-196387
json_metadata{"app":"peakd/2023.11.3","format":"markdown","tags":["health","stemng","stemgeeks","neoxian","waivio","cent","proofofbrain","chessbrothers","alive","stemsocial"],"users":["skyehi","empo.voter","Override"],"image":["https://files.peakd.com/file/peakd-hive/skyehi/23tbPiVkwLBkHxkt3nZRofynPNDxRBLN1xbafFoXuAN87YLReVGXHiXzhuHkVjKyECHqF.jpg","https://files.peakd.com/file/peakd-hive/skyehi/23swrbKc3yjMYSUJMwmHkXUC71AufZXrkQ315XAST7nXzaKhpkvsAScnV3nBnpS55SJ2r.png","https://files.peakd.com/file/peakd-hive/skyehi/EoAYDi9ZKfkBZi7rMWbojnS8KytoTpbSepvPmfeZmL2V2h8FDZ3xRpC58MZDkf1LeRQ.png"]}
created2023-11-30 15:23:57
last_update2023-11-30 15:23:57
depth0
children1
last_payout2023-12-07 15:23:57
cashout_time1969-12-31 23:59:59
total_payout_value1.130 HBD
curator_payout_value1.149 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,759
author_reputation103,172,568,869,122
root_title"How to build a Dice Roller App for Android: Our First Game - Hive Programmers"
beneficiaries
0.
accountstemsocial
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,297,761
net_rshares4,969,073,835,672
author_curate_reward""
vote details (233)
@stemsocial ·
re-skyehi-how-to-build-a-dice-roller-app-for-android-our-first-game-hive-programmers-20231130t231953268z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-skyehi-how-to-build-a-dice-roller-app-for-android-our-first-game-hive-programmers-20231130t231953268z
categoryhive-196387
json_metadata{"app":"STEMsocial"}
created2023-11-30 23:19:54
last_update2023-11-30 23:19:54
depth1
children0
last_payout2023-12-07 23:19:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length545
author_reputation22,463,635,119,853
root_title"How to build a Dice Roller App for Android: Our First Game - Hive Programmers"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,307,138
net_rshares0