Android Open Source - Crystal-Ball- Main Activity






From Project

Back to project page Crystal-Ball-.

License

The source code is released under:

Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECI...

If you think the Android project Crystal-Ball- listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sabersoft.crystalball;
import com.sabersoft.crystalball.ShakeDetector;
//from   w w w .jav  a2s  .  c o m
import com.sabersoft.crystalball.ShakeDetector.OnShakeListener;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


//GETTING INTO IMMERSIVE MODE 
//LINKING THE CRYSTALBALL + SHAKEDETECTOR CLASS WITH A NEW OBJECT 
//SOLVING THE VARIABLE SCOPE PROBLEM 1 WITH MEMBER VARIABLES (DECLARING AT BEG)  
//ASSIGNING ID'S TO DIFFERENT OBJECTS
//CREATING A SHAKEDETECTOR 
//CREATING AN ONCLICK LISTENER 
//IF STATEMENT WITHIN ONCLICK LISTENER
//REFERENCE VARIABLES WITHIN METHODS
//IF STATEMENTS AND ELSE IF STATEMENTS
//CREATE A METHOD THAT CAN BE ACTED UPON 
//ANIMATING IMAGES 
//SETTING A FADE ANIMATION
//SETTING A TRANSLATE ANIMATION
//PLAY SOUND
//MAKE AN OBJECT INVISIBLE
//SOLVE THE VARIABLE SCOPE PROBLEM 2
//SETTING ON RESUME METHOD FOR ONSHAKELISTENER
//SETTING ON PAUSE METHOD FOR ONSHAKELISTENER 
//CREATING A TOAST MESSAGE 
  


public class MainActivity extends Activity {
  
  
// GETTING INTO IMMERSIVE MODE 
  
  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
          super.onWindowFocusChanged(hasFocus);
      if (hasFocus) {
        getWindow().getDecorView().setSystemUiVisibility(
                  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
  } }
     
// LINKING THE CRYSTALBALL + SHAKEDETECTOR CLASS WITH A NEW OBJECT 
  public CrystalBall mCrystalBall = new CrystalBall(); 
  private ShakeDetector mShakeDetector;
  
// SOLVING THE VARIABLE SCOPE PROBLEM 1 WITH MEMBER VARIABLES (DECLARING AT BEG)  
  public TextView mAnswerLabel;
  public Button mAnswerButton; 
  public ImageView mCrystalBallImage;
  private SensorManager mSensorManager;
  private Sensor mAccelerometer;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
// ASSIGNING ID'S TO DIFFERENT OBJECTS
    
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
    mAnswerLabel = (TextView) findViewById(R.id.textView1);
    mAnswerButton = (Button) findViewById(R.id.button1);
    final Boolean rcode = false; 
    
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    
// CREATING A SHAKEDETECTOR 
    mShakeDetector = new ShakeDetector(new OnShakeListener(){

      @Override
      public void onShake() {
        String answer = mCrystalBall.getAnAnswer(); 
        mAnswerLabel.setText(answer);
        //relativeLayout.setBackgroundColor(Color.GREEN);
        animateCrystalBall(); 
        animateAnswer();
        playSound();
      }
      
    });


// CREATING AN ONCLICK LISTENER 
    
    mAnswerButton.setOnClickListener(new View.OnClickListener() {
      
      public void onClick(View v) {
        // TODO Auto-generated method stub
        
        String answer = mCrystalBall.getAnAnswer(); 
        mAnswerLabel.setText(answer);
        //relativeLayout.setBackgroundColor(Color.GREEN);
        animateCrystalBall(); 
        animateAnswer();
        playSound();
        
// IF STATEMENT WITHIN ONCLICK LISTENER
// REFERENCE VARIABLES WITHIN METHODS
        
        if (v == mAnswerButton)
        {
          //Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();
          //relativeLayout.setBackgroundColor(Color.GREEN);
          //setInvisible(answerLabel); 
        
        }
      
      
      }; });
    
    
    

        
// IF STATEMENTS AND ELSE IF STATEMENTS
        
          
        //if (randomNumber == 0)
          //answerLabel.setText("000000");
        //else if (randomNumber == 1)
          //answerLabel.setText("11111");
           
        
      }
  
    

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
  
// CREATE A METHOD THAT CAN BE ACTED UPON 
// ANIMATING IMAGES 
  
  private void animateCrystalBall()
  {
    mCrystalBallImage = (ImageView) findViewById(R.id.imageView2);
    mCrystalBallImage.setImageResource(R.drawable.ball_animation);
    AnimationDrawable ballAnimation = (AnimationDrawable) mCrystalBallImage.getDrawable();
    
    if (ballAnimation.isRunning())
    {
      ballAnimation.stop();
    }
    
    ballAnimation.start();
  }

// SETTING A FADE ANIMATION

  private void animateAnswer()
  {
    AlphaAnimation fadeInAnimation = new AlphaAnimation(0,1);
    fadeInAnimation.setDuration(1500);
    fadeInAnimation.setFillAfter(true);
    mAnswerLabel.setAnimation(fadeInAnimation);
  }
  
// SETTING A TRANSLATE ANIMATION 
  
  
  public void animateImage()
  {
  //Animation translateAnim = new TranslateAnimation(0, 320, 0, 0);
  //translateAnim.setDuration(1000); 
  //translateAnim.setRepeatCount(Animation.INFINITE); 
  //translateAnim.setRepeatMode(Animation.REVERSE); 
  //newImageView.startAnimation(translationAnim);
  } 
  
// PLAY SOUND 
  
  public void playSound()
  { 
    MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball);
    player.start(); 
    player.setOnCompletionListener(new OnCompletionListener() {
      
      @Override
      public void onCompletion(MediaPlayer mp) {
        mp.release();         
      }
    });
    
  }
  
// MAKE AN OBJECT INVISIBLE
// SOLVE THE VARIABLE SCOPE PROBLEM 2
  
  public void setInvisible(TextView mAnswerLabel)
  {
    mAnswerLabel.setVisibility(View.INVISIBLE);
  } 
  
// SETTING ON RESUME METHOD FOR ONSHAKELISTENER
  @Override
  public void onResume(){
    super.onResume();
    mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
  }

// SETTING ON PAUSE METHOD FOR ONSHAKELISTENER 
  @Override
  public void onPause(){
    super.onPause();
    mSensorManager.unregisterListener(mShakeDetector); } 
    
// CREATING A TOAST MESSAGE 
  public void createToast(){
    Toast swagToast = Toast.makeText(this, "swag swag", Toast.LENGTH_LONG );
    swagToast.setGravity(Gravity.TOP, 0, 0);
    swagToast.show();
    
    
  }
  }




Java Source Code List

com.sabersoft.crystalball.CrystalBall.java
com.sabersoft.crystalball.MainActivity.java
com.sabersoft.crystalball.ShakeDetector.java