Magic Of Magic-8

Magic 8 is basically a funschool toy. Its a ball which contains a dice having 16 faces on a shake of the ball it gives answer to yes or no type of question.

The Magic 8 Ball is a hollow plastic sphere resembling an oversized, black and white 8-ball. Inside is a cylindrical reservoir containing a white, plastic, icosahedral die floating in alcohol with dissolved dark blue dye. Each of the 20 faces of the die has an affirmative, negative, or non-committal statement printed on it in raised letters. There is a transparent window on the bottom of the Magic 8 Ball through which these messages can be read.

To use the ball, it must be held with the window initially facing down. After “asking the ball” a yes-no question, the user then turns the ball so that the window faces him, setting in motion the liquid and die inside. When the die floats to the top and one of its faces is pressed against the window, the raised letters displace the blue liquid to reveal the message as white letters on a blue background. Although many users shake the ball before turning it upright, the instructions warn against doing so because it can lead to bubbles. Unfortunately, over time the fluid tends to leak into the die such that eventually, it will no longer float to the top to display an answer.

– Source Wikipedia(http://en.wikipedia.org/wiki/Magic_8-Ball)

I tried to make a virtual version of it . So, the first version of the App that I came up with was quite crude. For android developers- it comprised of a single activity having two text fields and a button. On click of the button it calls a function which generates a number corresponding to which it sends a value or answer to the textfield. The function is as follows
public void job()

{

Random crazy = new Random();//Random class to generate random number

int k = crazy.nextInt(20);// sets the range to integers between 0 and 20

int i = 15;

switch (k) {

case 0:

tvB.setText(“It is certain”);// text to send to the text field

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));// Beauty of code lies here ’cause ever output will have different colour

break;

case 1:

tvB.setText(“Outlook not so good”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 2:

tvB.setText(“It is decidedly so”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 3:

tvB.setText(“Without a doubt”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 4:

tvB.setText(“Yes definitely”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 5:

tvB.setText(“You may rely on it”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 6:

tvB.setText(“As I see it, yes”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 7:

tvB.setText(“Most likely”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 8:

tvB.setText(“Outlook good”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 9:

tvB.setText(“Yes”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 10:

tvB.setText(“Signs point to yes”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 11:

tvB.setText(“Very doubtful “);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 12:

tvB.setText(“Reply hazy try again”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 13:

tvB.setText(“Ask again later”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 14:

tvB.setText(” Better not tell you now”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 15:

tvB.setText(“Cannot predict now”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 16:

tvB.setText(“Concentrate and ask again”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 17:

tvB.setText(“Don’t count on it”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 18:

tvB.setText(“My reply is no”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 19:

tvB.setText(“My sources say no”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

}

}

The above function is the heart of this app, it is like the core of the armour. So on every click of the button this function is called. Now, I should tell you how i went about doing this app. First comes the graphical layout , the code is below and the trick I used is I used a picture for the background. Well, I am telling this because in the first version I used a simple background color which used to look ugly. So, a piece of advice for people like me instead of giving background color give a background image.

this is the layout

this is the layout

The xml code is:

case 2:

tvB.setText(“It is decidedly so”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 3:

tvB.setText(“Without a doubt”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 4:
tvB.setText(“Yes definitely”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 5:

tvB.setText(“You may rely on it”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 6:

tvB.setText(“As I see it, yes”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 7:

tvB.setText(“Most likely”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));
break;

case 8:

tvB.setText(“Outlook good”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 9:

tvB.setText(“Yes”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 10:

tvB.setText(“Signs point to yes”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 11:

tvB.setText(“Very doubtful “);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 12:

tvB.setText(“Reply hazy try again”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 13:
tvB.setText(“Ask again later”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 14:

tvB.setText(” Better not tell you now”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 15:
tvB.setText(“Cannot predict now”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 16:

tvB.setText(“Concentrate and ask again”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 17:

tvB.setText(“Don’t count on it”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 18:

tvB.setText(“My reply is no”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

case 19:

tvB.setText(“My sources say no”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));

break;

}

}

The xml code is:

One of the most important thing is java file , the back-end support of the app, the action packed feature literally. Java file is automatically generated in eclipse , and linked to the xml file , I will talk about that in later blogs on how to link java file to xml file , for now the java code is :

package com.example.magic_8;import java.util.Random;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.graphics.Color;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.util.Log;

import android.view.Gravity;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {

public TextView tvA, tvB;

SensorManager sm;

private static final int SHAKE_THRESHOLD = 800;

long lastUpdate = 0;

public float x, y, z;

public float last_x = 0;

public float last_y = 0;

public float last_z = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//this line makes the activity whole screen activity hiding the notification bar

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

intials();

//registering the sensor

if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {

Sensor r = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);

sm.registerListener(this, r, SensorManager.SENSOR_DELAY_GAME);

}

}

public void intials() { //function to initialize all the varibles

setContentView(R.layout.activity_main);

sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

tvA = (TextView) findViewById(R.id.tv1);

tvB = (TextView) findViewById(R.id.tv2);

}

public void job() {

Random crazy = new Random();

int k = crazy.nextInt(20);

int i = 15;

switch (k) {

case 0:

tvB.setText(“It is certain”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 1:

tvB.setText(“Outlook not so good”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 2:

tvB.setText(“It is decidedly so”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 3:

tvB.setText(“Without a doubt”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 4:

tvB.setText(“Yes definitely”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 5:

tvB.setText(“You may rely on it”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 6:

tvB.setText(“As I see it, yes”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 7:

tvB.setText(“Most likely”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 8:

tvB.setText(“Outlook good”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 9:

tvB.setText(“Yes”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 10:

tvB.setText(“Signs point to yes”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 11:

tvB.setText(“Very doubtful “);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 12:

tvB.setText(“Reply hazy try again”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 13:

tvB.setText(“Ask again later”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 14:

tvB.setText(” Better not tell you now”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 15:

tvB.setText(“Cannot predict now”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 16:

tvB.setText(“Concentrate and ask again”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 17:

tvB.setText(“Don’t count on it”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 18:

tvB.setText(“My reply is no”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

case 19:

tvB.setText(“My sources say no”);

tvB.setTextColor(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),

crazy.nextInt(265)));

break;

}

}

@Override

public void onAccuracyChanged(Sensor arg0, int arg1) {

// TODO Auto-generated method stub

}

@Override

public void onSensorChanged(SensorEvent k) {

// TODO Auto-generated method stub

// arrangement made to difine a shake using accelerometer

long curTime = System.currentTimeMillis();

// only allow one update every 100ms.

if ((curTime – lastUpdate) > 100) {

long diffTime = (curTime – lastUpdate);

lastUpdate = curTime;

x = k.values[0];

y = k.values[1];

z = k.values[2];

float speed = Math.abs(x + y + z – last_x – last_y – last_z)

/ diffTime * 10000;

if (speed > SHAKE_THRESHOLD) {

job();

}

last_x = x;

last_y = y;

last_z = z;

}

}

@Override

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

sm.unregisterListener(this);

}

}

One of the most important concept is linking two activity which I will be covering in the next blog till then experiment with the code , tell me any bugs or flaws you come across any suggestion, criticism and appreciation will make me very happy see you in the next blog peace out.

With this I am also pushing my first project on Github , do pull ,commit and play with the code the link to the project is here 🙂

Did you find this article valuable?

Support Farhaan Bukhsh by becoming a sponsor. Any amount is appreciated!