Friday, February 24, 2012

small step toward learning Android

package com.hello.touch.event;
/**Android Programming I
Assignment I
1. Starts up with a RED screen.
2. Touching the screen turn the screen GREEN.
3. Untouching the screen turns the screen BLUE.
**/

//An activity is a single, focused thing that the user can do
import android.app.Activity;
//The Color class defines methods for creating and converting color ints
//The Color class defines methods for creating and converting color ints. Colors are represented as packed ints, made up of 4 bytes
import android.graphics.Color;

import android.os.Build;
//Information about the current build, extracted from system properties.
import android.os.Bundle;
//Object used to report movement (mouse, pen, finger, trackball) events.
//Motion events may hold either absolute or relative movements and other data, depending on the type of device.
import android.view.MotionEvent;
//A View occupies a rectangular area on the screen and is responsible for drawing and event handling.
//View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.).
import android.view.View;

public class TouchActivity extends Activity {


 View vw;

 //Annotation type used to mark methods that override a method declaration in a superclass.
 //Compilers produce an error if a method annotated with @Override does not actually override a method in a superclass. 
    @Override
   
   //Called when the activity is starting. This is where most initialization should go
    public void onCreate(Bundle savedInstanceState) {
     //vw = new View(this);
        super.onCreate(savedInstanceState);
       //Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.
       setContentView(R.layout.main);
       
        //Retrieve ID from R.Java as defined in main.xml doing findViewById(0x7f050000) should also work
        //Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle)
          vw = findViewById(R.id.vw1);
         
        //Sets the background color for this view.
         // vw.setBackgroundColor(Color.RED);
     
       
      
       
    }
   
    //Called when a touch screen event was not handled by any of the views under it.
    //This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it.
   
    public boolean onTouchEvent(MotionEvent ev){
     // Set background to Green on ACTION_DOWN
     if (ev.getAction() == 0 ){
      vw.setBackgroundColor(Color.GREEN);
     
     
     }
     // Set background to White on ACTION_MOVE
     if (ev.getAction() == MotionEvent.ACTION_MOVE ){
      vw.setBackgroundColor(Color.WHITE);
     
     }
     //Set background to Blue on ACTION_UP
     //Yes, -16776961 is really blue
     if (ev.getAction() == 1 ){
      vw.setBackgroundColor(-16776961);
     
     }
  return true;
   
   
    }
   
  
   
}