Create a Tetris Game in Android Studio

Create a Tetris Game in Android Studio:

 

Download Android Studio from the
link given blow according to your operating system Windows, Mac, Linux, Chrome
OS etc.

Link: https://developer.android.com/studio

More downloads are available here.

Link: https://developer.android.com/studio/archive

 

CODE:

 

AndroidManifest.xml


<manifest
xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.sava.tetrisgame”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.game.tetrisgame.IntialActivity”
android:configChanges=”orientation|keyboardHidden|screenSize”
android:label=”@string/app_name”
android:theme=”@style/FullscreenTheme” >

<category
android:name=”android.intent.category.LAUNCHER” />

colors.xml

<color
name=”dark_red”>#66000033

strings.xml


<string
name=”app_name”>TetrisGame
Empty Content
Empty Button

styles.xml 



customtheme.xml

/>

activity_intial.xml

<FrameLayout
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”
android:background=”#0099cc”
tools:context=”.IntialActivity” >

  

<TextView
android:id=”@+id/fullscreen_content”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:keepScreenOn=”true”
android:text=”@string/empty_content”
android:textColor=”#33b5e5″
android:textSize=”50sp”
android:textStyle=”bold” />

<FrameLayout
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:fitsSystemWindows=”true” >

<LinearLayout
android:id=”@+id/fullscreen_content_controls”
style=”?buttonBarStyle”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_gravity=”bottom|center_horizontal”
android:background=”@color/dark_red”
android:orientation=”horizontal”
tools:ignore=”UselessParent” >

<Button
android:id=”@+id/empty_button”
style=”?buttonBarButtonStyle”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:text=”@string/empty_button” />

SystemHiderUserInterface.java

package
com.game.tetrisgame.usageclasses;

import android.app.Activity;
import android.os.Build;
import android.view.View;

public abstract class
SystemHiderUserInterface {

   public static final int FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES = 0x1;

  
   public static final int FLAG_FULLSCREEN = 0x2;

  
   public static final int FLAG_HIDE_NAVIGATION = FLAG_FULLSCREEN |
0x4;


   protected Activity mActivity;


   protected View mAnchorView;


   protected int mFlags;


   protected OnVisibilityChangeListener mOnVisibilityChangeListener =
sDummyListener;


   public static SystemHiderUserInterface getInstance(Activity
activity, View anchorView,
           int flags) {
       if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.HONEYCOMB) {
           return new
SystemHoneycombUserInteface(activity, anchorView, flags);
       } else {
           return new
SystemBaseUserInterface(activity, anchorView, flags);
       }
   }

   protected
SystemHiderUserInterface(Activity activity, View anchorView, int flags) {
       mActivity = activity;
       mAnchorView = anchorView;
       mFlags = flags;
   }

  
   public abstract void setup();


   public abstract boolean isVisible();


   public abstract void hide();


   public abstract void show();

  
   public void toggle() {
       if (isVisible()) {
           hide();
       } else {
           show();
       }
   }

  
   public void setOnVisibilityChangeListener(
           OnVisibilityChangeListener
listener) {
       if (listener == null) {
           listener = sDummyListener;
       }

      
mOnVisibilityChangeListener = listener;
   }


   private static OnVisibilityChangeListener sDummyListener = new
OnVisibilityChangeListener() {
       @Override
       public void onVisibilityChange(boolean visible)
{
       }
   };


   public interface OnVisibilityChangeListener {

       public void onVisibilityChange(boolean
visible);
   }
}


SystemBaseUserInterface.java

package com.game.tetrisgame.usageclasses;

import android.app.Activity;
import android.view.View;
import android.view.WindowManager;


public class SystemBaseUserInterface extends SystemHiderUserInterface {

   private boolean
mVisible = true;

  
   protected SystemBaseUserInterface(Activity activity, View
anchorView, int flags) {
       super(activity, anchorView, flags);
   }

   @Override
   public void setup() {
       if ((mFlags &
FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES) == 0) {
           mActivity.getWindow().setFlags(

    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN

            |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,

    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN

            |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
       }
   }

   @Override
   public boolean isVisible() {
       return mVisible;
   }

   @Override
   public void hide() {
       if ((mFlags & FLAG_FULLSCREEN) != 0) {
           mActivity.getWindow().setFlags(

    WindowManager.LayoutParams.FLAG_FULLSCREEN,

    WindowManager.LayoutParams.FLAG_FULLSCREEN);
       }

mOnVisibilityChangeListener.onVisibilityChange(false);
       mVisible = false;
   }

   @Override
   public void show() {
       if ((mFlags & FLAG_FULLSCREEN) != 0) {

mActivity.getWindow().setFlags(0,

    WindowManager.LayoutParams.FLAG_FULLSCREEN);
       }

mOnVisibilityChangeListener.onVisibilityChange(true);
       mVisible = true;
   }
}

SystemHoneycombUserInterface.java

package
com.game.tetrisgame.usageclasses;

import
android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;


public class SystemHoneycombUserInterface extends SystemBaseUserInterface {

   private int
mShowFlags;


   private int mHideFlags;


   private int mTestFlags;

  
   private boolean mVisible = true;

  
   protected SystemHoneycombUserInterface(Activity activity, View
anchorView,
           int flags) {
       super(activity, anchorView, flags);

      
mShowFlags = View.SYSTEM_UI_FLAG_VISIBLE;
       mHideFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
       mTestFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;

       if ((mFlags
& FLAG_FULLSCREEN) != 0) {

           mShowFlags |=
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
           mHideFlags |=
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

    | View.SYSTEM_UI_FLAG_FULLSCREEN;
       }

       if
((mFlags & FLAG_HIDE_NAVIGATION) != 0) {

           mShowFlags |=
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
           mHideFlags |=
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
           mTestFlags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
       }
   }


   @Override
   public void setup() {
       mAnchorView

.setOnSystemUiVisibilityChangeListener(mSystemUiVisibilityChangeListener);
   }

  
   @Override
   public void hide() {
       mAnchorView.setSystemUiVisibility(mHideFlags);
   }

  
   @Override
   public void show() {
       mAnchorView.setSystemUiVisibility(mShowFlags);
   }

   @Override
   public boolean isVisible() {
       return mVisible;
   }

   private
View.OnSystemUiVisibilityChangeListener mSystemUiVisibilityChangeListener = new
View.OnSystemUiVisibilityChangeListener() {
       @Override
       public void onSystemUiVisibilityChange(int vis)
{

           if ((vis & mTestFlags)
!= 0) {
               if
(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {

    mActivity.getActionBar().hide();

    mActivity.getWindow().setFlags(

WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);
               }

      

mOnVisibilityChangeListener.onVisibilityChange(false);
               mVisible
= false;

      
    } else {
               mAnchorView.setSystemUiVisibility(mShowFlags);
               if
(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {

    mActivity.getActionBar().show();

    mActivity.getWindow().setFlags(0,

WindowManager.LayoutParams.FLAG_FULLSCREEN);
               }

      

mOnVisibilityChangeListener.onVisibilityChange(true);
               mVisible
= true;
           }
       }
   };
}


IntialActivity.java

package com.game.tetrisgame;

import
com.game.tetrisgame.usageclasses.SystemHiderUserInterface;

import
android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;


public class IntialActivity extends Activity {

   private static final
boolean AUTO_HIDE = true;

  
   private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

  
   private static final boolean TOGGLE_ON_CLICK = true;


   private static final int HIDER_FLAGS =
SystemUiHider.FLAG_HIDE_NAVIGATION;


   private SystemHiderUserInterface mSystemUiHider;


   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

      
setContentView(R.layout.activity_menu);

      
final View controlsView = findViewById(R.id.fullscreen_content_controls);
       final View contentView =
findViewById(R.id.fullscreen_content);

      
       mSystemUiHider = SystemHiderUserInterface.getInstance(this,
contentView,

HIDER_FLAGS);
       mSystemUiHider.setup();
       mSystemUiHider

.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {

    int mControlsHeight;

    int mShortAnimTime;

      

(Build.VERSION_CODES.HONEYCOMB_MR2)

    public void onVisibilityChange(boolean visible) {

        if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.HONEYCOMB_MR2) {

            if (mControlsHeight ==
0) {

mControlsHeight = controlsView.getHeight();

            }

            if (mShortAnimTime ==
0) {

mShortAnimTime = getResources().getInteger(

        android.R.integer.config_shortAnimTime);

            }

            controlsView

    .animate()

    .translationY(visible ? 0 : mControlsHeight)

    .setDuration(mShortAnimTime);

        } else {

controlsView.setVisibility(visible ? View.VISIBLE

    : View.GONE);

        }

      
                if
(visible && AUTO_HIDE) {

            // Schedule a hide().
               delayedHide(AUTO_HIDE_DELAY_MILLIS);

        }

    }
               });

      
       contentView.setOnClickListener(new
View.OnClickListener() {
           @Override
           public void onClick(View
view) {
               if (TOGGLE_ON_CLICK)
{

    mSystemUiHider.toggle();
               } else {

    mSystemUiHider.show();
               }
           }
       });

      
scheduled hide()

       findViewById(R.id.empty_button).setOnTouchListener(

mDelayHideTouchListener);
   }


   protected void onPostCreate(Bundle savedInstanceState) {
       super.onPostCreate(savedInstanceState);

      
       delayedHide(100);
   }


   View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener()
{
       @Override
       public boolean onTouch(View view, MotionEvent
motionEvent) {
           if (AUTO_HIDE) {

delayedHide(AUTO_HIDE_DELAY_MILLIS);
           }
           return false;
       }
   };

   Handler mHideHandler =
new Handler();
   Runnable mHideRunnable = new Runnable() {
       @Override
       public void run() {
           mSystemUiHider.hide();
       }
   };


   private void delayedHide(int delayMillis) {
       mHideHandler.removeCallbacks(mHideRunnable);
       mHideHandler.postDelayed(mHideRunnable,
delayMillis);
   }
}


You Must Keep The Files As Shown Order Below:

AndroidManifest.xml

libs/android-support-v4.jar

proguard-project.txt

project.properties

res/drawable-hdpi/ic_launcher.png

res/drawable-mdpi/ic_launcher.png

res/drawable-xhdpi/ic_launcher.png

res/layout/activity_intial.xml

res/values-v11/styles.xml

res/values-v14/styles.xml

res/values/customtheme.xml

res/values/colors.xml

res/values/strings.xml

res/values/styles.xml

src/com/game/tetrisgame/IntialActivity.java

src/com/game/tetrisgame/usageclasses/SystemHiderUserInterface.java

src/com/game/tetrisgame/usageclasses/SystemBaseUserInterface.java

src/com/game/tetrisgame/usageclasses/SystemHoneycombUserInterface.java

 

This is how you can create a Tetris game in Android Studio. Please use the above code with complete explained process and develop the game. If you still need more help and assistance, please comment down below so that we can help you. Good Luck!!