How to Build a Simple Magic 8 Ball App in Android?
Last Updated :
20 Jan, 2021
In this article, we will be building a Magic 8 Ball App Project using Java and XML in Android. The application is based on a decision making application. In this app, a user can ask the ball what decision they should make. The ball will randomly answer Yes, No, Not Sure, and other such answers. There will be a single activity in this application. A sample GIF is given below to get an idea about what we are going to do in this article.

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. Note that select Java as the programming language.
Step 2: Before going to the coding section first you have to do some pre-task
Ball Images: All the ball images are listed below. Save them in your drawable folder in resources. Go to the app > res > drawable and paste the following files:
Change the style to NoActionBar in the themes.xml file:
<style name=”AppTheme” parent=”Theme.AppCompat.NoActionBar”>
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 to display the title. Then it contains an ImageView of the ball in the center of the activity. At last, we have a Button at the bottom of the activity for asking the application to make decisions. This is a single activity application. Below is the code for the activity_main.xml file.
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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0F9D58"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="3">
<!--textview to display the title of the activity-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Make everyday decisions easier!"
android:textColor="#000000"
android:textSize="25dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2">
<!--imageview to display the balls-->
<ImageView
android:id="@+id/image_eightBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ball1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2">
<!--button to ask the app for decision-->
<Button
android:id="@+id/askButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#3F51B5"
android:text="Ask"
android:textColor="#FFFFFF" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Step 4: Working with the MainActivity.java file
In the java file we will create an onClickListener() function for the ask button also we will create an array to store the id of all the images. Inside the onClickListener() we will generate a random number from 1-4 using the Random() function. After that, we will set the image from the array of random number's position in the image view. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// link all the variables with its id
final ImageView imageView = (ImageView) findViewById(R.id.image_eightBall);
Button button = (Button) findViewById(R.id.askButton);
// create an array to store all the images
final int a[] = {R.drawable.ball2, R.drawable.ball3, R.drawable.ball4, R.drawable.ball5};
// ask button's onClick function
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// generate a number using Random() function
Random random = new Random();
int x = random.nextInt(4);
// set the image to the view
imageView.setImageResource(a[x]);
}
});
}
}
Output:
Similar Reads
How to Build a Simple Alarm Setter App in Android? In this article, we are going to see how to build a much interesting app named Alarm Setter. Alarm plays a vital role in our day-to-day life. Nowadays alarm has become our wake-up assistant. Every mobile phone is associated with an alarm app. We will create this app using android studio. Android Stu
5 min read
How to Build a Simple Augmented Reality Android App? Augmented Reality has crossed a long way from Sci-fi Stories to Scientific reality. With this speed of technical advancement, it's probably not very far when we can also manipulate digital data in this real physical world as Tony Stark did in his lab. When we superimpose information like sound, text
11 min read
How to Build a Sensor App in Android? Android mobile phones have sensors, so we can perform various functions like Controlling screen brightness, Monitoring acceleration along a single axis, Motion detection, etc. In this article, we will be building an application that will determine the intensity of light in the room with the help of
5 min read
How to Build a Rick and Morty App in Android? Rick and Morty is an American animated science fiction sitcom created by Justin Roiland and Dan Harmon. In this article, we will build an application that will display the name and image of all rick and Morty characters using this API. In order to build this application we will be using the Retrofit
5 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 Pomodoro App in Android? The Pomodoro Technique is a time management method developed by Francesco Cirillo in the late 1980s. The technique uses a timer to break down work into intervals, traditionally 25 minutes in length, separated by short breaks of 5 minutes. These intervals are known as "pomodoros". The method is based
4 min read
How to Build a Weather App in Android? In this project, we will be building a weather application. This application will show the temperature of a location. To fetch weather information we will need an API. An API(Application Programming Interface) is a function that allows applications to interact and share data using various components
6 min read
How to Build a SOS Mobile Application in Android Studio? The SOS applications are basically advanced emergency apps that can rescue you and/or your loved ones if you and/or they find themselves in a life-threatening emergency situation and need immediate assistance. When you need some personal assistance, you can actually turn on your phone and can call o
15+ min read
How to Build a Simple Expense Calculator App in Android? Pre-requisites: Android App Development Fundamentals for BeginnersGuide to Install and Set up Android StudioHow to Create/Start a New Project in Android Studio?Running your first Android appRecyclerView in Android with ExampleShared Preferences in Android with Example A simple expense calculator let
10 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