Android screen crack prank

In this post, we are going to develop an application, its kinda game which can be used to prank your friends. :). Yes, you want to fool your friends with a simple android application, just go through the post to develop one for you :). I would like to name this application – ‘Screen crack prank’.

Application’s theme:

Let your friends play the game on your Android phone and see how the screen cracks and the screen goes black and also vibrates. Your friends will believe they broke your phone.

You must have already seen such applications in Google play but creating one such application is really fun!!

Let us start creating the application:

Quick Links

Project Structure

  • Create new android project [File >> New >> Android Project] with project name TapperApp
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.prgguru.android’
  • Click finish

Code Listings

Layout creation:

We will create a simple layout which has one textview to display instructions and one button who is the hero of ‘Screen Crack Prank’ app. :)

Main.xml:

Replace the XML present in /res/layout with the below one:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:id="@+id/background">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Challenge for you!"
        android:textStyle="bold"
        android:textColor="#0587d9"
        android:textSize="20sp"
        android:layout_margin="20dip"
        android:gravity="center" 
        android:id="@+id/title"/>

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:layout_margin="20dip"
        android:text="INSTRUCTIONS:nnTry to tap the button 100 times within 30 seconds.nnBest of Luck!"
        android:id="@+id/blurb"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dip"
        android:padding="25dip"
        android:text="100"
        android:layout_gravity="center"
        android:id="@+id/button"/>

</LinearLayout>

The application layout should look like:

 

We have to add few resources before we proceed with writing logic for the application.

Load cracked_screen.png (crack image) under /res/drawable-hdpi folder and glass.ogg (glass breaking sound file) under /res/raw folder. If raw folder is not created, create one under /res folder.

Application logic:

TapperAppActivity.java:

[pgsubscribe]

Logic behind the application is given below:

Open TapperAppAcitivty class under com.prgguru.android package and add the below class. Each line of code is commented to let you understand the code logic.

package com.prgguru.android;

import java.util.Random;
import com.example.tapperapp.R;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TapperAppActivity extends Activity implements OnClickListener{
	MediaPlayer mPlayer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Set application layout
        setContentView(R.layout.main);
        //Assign media player object with sound file
        mPlayer = MediaPlayer.create(TapperAppActivity.this, R.raw.glass);
        //Find the button			
        ((Button) findViewById(R.id.button)).setOnClickListener(this);
    }

    public void onClick(View v) {
    	//If our applicaitons' button is clicked
		if (v.getId()==R.id.button) {
			//Get the button text which is 100
			int i = Integer.parseInt(((Button) v).getText().toString());
			//Decrement button text value and set it as button text
			((Button) v).setText(Integer.toString(--i));
			//Create Random object
			Random gen = new Random();
			//If random value btwn (0 - 9)+1 equals to 10 or i is lesser than 50
			if ((gen.nextInt(10) + 1 == 10) || (i<50)) {
				//Call crack method
				crack();
				//Make listener to button as null
				((Button) v).setOnClickListener(null);
			}
		}
	}

	private void crack() {
		//Call audio effects method
		audibleFX();
		//Call visual effects method
		visualFX();
		//Call vibrate effects method
		touchFX();
	}

	private void audibleFX() {
		//Play sound file
		mPlayer.start();
	}

	private void visualFX() {
		//Set background with broken glass image
		findViewById(R.id.background).setBackgroundResource(R.drawable.cracked_screen);
		//Set title textview with color 0x6400ff00
		((TextView)findViewById(R.id.title)).setTextColor(0x6400ff00);
		//Set blurb textview with color 0x64ffffff
		((TextView)findViewById(R.id.blurb)).setTextColor(0x64ffffff);
		//Set button  with color 0x64cdc9c9
		((Button)findViewById(R.id.button)).setBackgroundColor(0x64cdc9c9);
	}

	private void touchFX() {
		//Create vibrator object
		Vibrator mv = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
		//Vibrate for 500 MS
		mv.vibrate(new long[]{ 0, 500, 0 }, -1);
	}

	protected void onDestroy() {
		super.onDestroy();
		// Release mediaplayer
		if (mPlayer != null) {
			mPlayer.release();
			mPlayer = null;
		}
	}
}
Add vibrate permission in Androidmanifest.xml:
<uses-permission android:name="android.permission.VIBRATE" />

To know more about how to use vibration service in Android, see Android vibrate example

[pglinkadssmall1]

Demo

Congratulations, We are done.
Let us test the application:
Right click on the project >> Run as >> Android application >> Choose emulator or device
Tap on the button and see what happens.



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.

[pglinkadssmall]

I hope you enjoyed the post!! :)

Suggested posts for further reading


[pgfeedback]
[pgwriteforus]

Related Articles

Author: Android Guru

Share This Post On
468 ad
  • g.gopikrishna

    how to remove ram memory in android device using java code or phonegap code .please provide slolution for me

  • Wiltshire

    My brother recommended I might like this blog. He was entirely right. This publish truly made my day. You can not imagine just how a lot time I had spent for this information! Thank you!

  • MrCleanX

    Great post! Great Blog! Just what I need to get rolling in Droid programming. Thanks, and keep it up!

  • Samuel

    Hi I’ve followed your instruction and have recreated the app using Eclipse, only changing minor bits such as the “android:text=” part into using a @string method (This was flagged by Eclipse) and the file name of the main.xml

    After finishing that I tried to run it on an emulator as well as my own Android device. While at first it runs, it force quitted after playing the glass sound, without displaying the cracked_glass png as well as any vibration.

    I tried using your source code provided at the end of this guide and it works.

    Any idea what I’ve done wrong?

  • aravind

    nice post

Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares