Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Thursday 21 June 2018

Android app : Spin the bottle android game

To download the source code                            click here



Just copy and paste it as written nothing to do.

try it it is a best and easy game which you can show as a mini project also used to play spin the bottle game




Activity_main.xml





<?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=".MainActivity">

    <ImageView
        android:id="@+id/bottle"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:srcCompat="@drawable/bottle"
        android:scaleType="center"
        tools:layout_editor_absoluteX="155dp"
        tools:layout_editor_absoluteY="240dp" />

    <Button
        android:id="@+id/Clickit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go"
        tools:layout_editor_absoluteX="141dp"
        tools:layout_editor_absoluteY="522dp" />

</android.support.constraint.ConstraintLay>out



Activity mian.java





package com.example.sarfraz_alam.spinit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
ImageView img;
Button butt;
Random r;
int angle;
boolean restart= false;
int temp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img =findViewById(R.id.bottle);
        butt=findViewById(R.id.Clickit);
        r = new Random();
        angle=r.nextInt(3600)+360;
        temp = angle % 360;
        butt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(restart){
                    RotateAnimation rotate=new RotateAnimation
                            (temp,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
                    rotate.setFillAfter(true);
                    rotate.setDuration(1000);
                    rotate.setInterpolator(new AccelerateDecelerateInterpolator());
                    img.startAnimation(rotate);
                    restart=false;
                    butt.setText("GO");
                }
                 else
                {
                    RotateAnimation rotate=new RotateAnimation
                            (0,r.nextInt(3600)+360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
                    rotate.setFillAfter(true);
                    rotate.setDuration(3600);
                    rotate.setInterpolator(new AccelerateDecelerateInterpolator());
                    img.startAnimation(rotate);
                    restart=true;
                    butt.setText("Reset");
                }

            }
        });
    }
}

1 comment:

  1. It is good but you should do proper alignment

    ReplyDelete