Open In App

How to Build Spin the Bottle Game Application in Android?

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

In this article, we will be building a Spin the Bottle Game Project using Java/Kotlin and XML in Android. The application is based on a multiplayer game. One player spins the bottle and the direction in which the bottle spins will determine the player who gets selected for a task or any other stuff. There will be a single activity in this application.

How-to-Build-Spin-the-Bottle-Game-Application-in-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: Import bottle image

Go to app > res > drawable and add this bottle image.

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 show the title. Then it contains an ImageView of the bottle in the center of the activity. Below is the code for the activity_main.xml file.

activity_main.xml:

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"
    tools:context=".MainActivity">

    <!--to show the game title-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Spin the Bottle"
        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" />

    <!--image of the bottle -->
    <ImageView
        android:id="@+id/bottle"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:src="@drawable/bottle"
        android:backgroundTintMode="multiply"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


Step 4: Working with the MainActivity file

We will set an onClickListener on the bottle to rotate the bottle when the bottle in tapped. Then, we will create a spinBottle() function to write the code for spinning the bottle. Inside this function, we will generate a random number from 5-10 that will be the number of the times the bottle spins. For this, we will be using the Random function. We will use ObjectAnimator to spin the image. We can set a duration of the animation. We will also use DecelerateInterpolator() to slow down the bottle naturally.

MainActivity File:

MainActivity.java
package org.geeksforgeeks.demo;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    // declare bottle as imageview
    private ImageView bottle;
    // variable to track if the bottle is spinning
    private boolean isSpinning = false;
    // variable to track the last rotation
    private float lastRotation = 0f;

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

        // find the bottle imageview by its id
        bottle = findViewById(R.id.bottle);

        // set a click listener on the bottle imageview
        bottle.setOnClickListener(v -> {
            if (!isSpinning) {
                // start spinning the bottle
                spinBottle();
            }
        });
    }

    private void spinBottle() {
        // minimum full spins
        int minSpins = 5;
        // maximum full spins
        int maxSpins = 10;

        // random number of spins between minSpins and maxSpins
        int spins = new Random().nextInt((maxSpins - minSpins) + 1) + minSpins;
        // final stop angle
        int angle = new Random().nextInt(360);
        // total rotation
        float totalRotation = 360f * spins + angle;

        // animate the bottle using ObjectAnimator
        ObjectAnimator animator = ObjectAnimator.ofFloat(bottle, "rotation", lastRotation, lastRotation + totalRotation);
        // set duration of the animation
        animator.setDuration(3000);
        // slows down naturally
        animator.setInterpolator(new DecelerateInterpolator());

        // set a listener on the animation
        animator.addListener(new Animator.AnimatorListener() {
            // when the animation starts
            @Override
            public void onAnimationStart(Animator animation) {
                isSpinning = true;
            }

            // when the animation ends
            @Override
            public void onAnimationEnd(Animator animation) {
                isSpinning = false;
                lastRotation = (lastRotation + totalRotation) % 360;
            }

            // other methods are will not be used
            @Override
            public void onAnimationCancel(Animator animation) {}

            @Override
            public void onAnimationRepeat(Animator animation) {}
        });

        // start the animation
        animator.start();
    }
}
MainActivity.kt
package org.geeksforgeeks.demo

import android.animation.Animator
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlin.random.Random

class MainActivity : AppCompatActivity() {
    // declare bottle as imageview
    private lateinit var bottle: ImageView
    // variable to track if the bottle is spinning
    private var isSpinning = false
    // variable to track the last rotation
    private var lastRotation = 0f

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // find the bottle imageview by its id
        bottle = findViewById(R.id.bottle)

        // set a click listener on the bottle imageview
        bottle.setOnClickListener {
            if (!isSpinning) {
                // start spinning the bottle
                spinBottle()
            }
        }
    }

    private fun spinBottle() {
        // minimum full spins
        val minSpins = 5
        // maximum full spins
        val maxSpins = 10

        // random number of spins between minSpins and maxSpins
        val spins = Random.nextInt(minSpins, maxSpins + 1)
        // final stop angle
        val angle = Random.nextInt(0, 360)  
        // total rotation
        val totalRotation = 360f * spins + angle

        // animate the bottle using ObjectAnimator
        val animator = ObjectAnimator.ofFloat(bottle, "rotation", lastRotation, lastRotation + totalRotation)
        // set duration of the animation
        animator.duration = 3000
        // slows down naturally
        animator.interpolator = DecelerateInterpolator()
        
        // set a listener on the animation
        animator.addListener(object : Animator.AnimatorListener {
            // when the animation starts
            override fun onAnimationStart(animation: Animator) {
                isSpinning = true
            }

            // when the animation ends
            override fun onAnimationEnd(animation: Animator) {
                isSpinning = false
                lastRotation = (lastRotation + totalRotation) % 360
            }

            // other methods are will not be used
            override fun onAnimationCancel(animation: Animator) {}
            override fun onAnimationRepeat(animation: Animator) {}
        })
        
        // start the animation
        animator.start()
    }
}

Output:



Next Article

Similar Reads