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

avatar

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.jpgOriginal Image Source by lmonk72 from Pixabay

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.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.

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

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 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.

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

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

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 ✍️


You Can Follow Me @skyehi For More Like This And Others



0
0
0.000
1 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord 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. 
 

0
0
0.000