SHARE
TWEET

HappyPlace MainActivity

Allen_L Oct 30th, 2013 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.Menu;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.ViewSwitcher;
  7.  
  8. public class MainActivity extends Activity implements OnClickListener {
  9.  
  10.         private static ViewSwitcher switcher;// needed for control of ViewSwitcher
  11.         private static View sad;// needed for onClick
  12.         private static View isSad;// needed for sadPictureReturn()
  13.  
  14.         @Override
  15.         protected void onCreate(Bundle savedInstanceState) {
  16.                 super.onCreate(savedInstanceState);
  17.                 setContentView(R.layout.activity_main);
  18.  
  19.                 // setup switcher view
  20.                 switcher = (ViewSwitcher) findViewById(R.id.ViewSwitcher1);
  21.                 switcher.setInAnimation(this, android.R.anim.fade_in);
  22.                 switcher.setOutAnimation(this, android.R.anim.fade_out);
  23.                 sad = (View) findViewById(R.id.Viewsad);
  24.                 sad.setOnClickListener(this);
  25.         }
  26.  
  27.         @Override
  28.         public boolean onCreateOptionsMenu(Menu menu) {
  29.                 // Inflate the menu; this adds items to the action bar if it is present.
  30.                 getMenuInflater().inflate(R.menu.main, menu);
  31.                 return true;
  32.         }
  33.  
  34.  
  35.         @Override
  36.         public void onClick(View arg0) {
  37.                 // called when sad view is clicked
  38.                 // switch to happy face and play sound
  39.                 if (arg0.getId() == R.id.Viewsad) {
  40.                         switcher.showNext();
  41.                         MyMediaPlayer.mediaPlaySound(getBaseContext());
  42.  
  43.                         // else switch to sad face
  44.                 } else {
  45.                         switcher.showPrevious();
  46.                 }
  47.  
  48.         }
  49.  
  50.  
  51.         public static void sadPictureReturn() {
  52.                 // When sound is finished return to sad view
  53.                 // check if next view is sad. if it is then switch
  54.                 // if not then do nothing because it is already on sad view
  55.                 isSad = switcher.getNextView();
  56.                 if (isSad == sad) {
  57.                         switcher.showPrevious();
  58.                 }
  59.         }
  60. }
RAW Paste Data
Want to get better at Java?
Learn to code Java in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top