ANDROID EVENT HANDLING L Grewe Widget Views that

  • Slides: 17
Download presentation
ANDROID – EVENT HANDLING L. Grewe

ANDROID – EVENT HANDLING L. Grewe

Widget : Views that have events For a list of the widgets provided by

Widget : Views that have events For a list of the widgets provided by Android, see the android. widget package. Some Examples Button Check. Box Date. Picker Edit. Text Image. View Search. View Spinner

Event Handling STEP 1: Decide what Widgets who’s events to process STEP 2: Define

Event Handling STEP 1: Decide what Widgets who’s events to process STEP 2: Define an event listener STEP 3: Register event listener with the View. On. Click. Listener (for handling "clicks" on a View), View. On. Touch. Listener (for handling touch screen events in a View), and View. On. Key. Listener (for handling device key presses within a View) http: //developer. android. com/guide/topics/ui/ui -events. html details more

Lets add a Button to a programbased interface Step 1: Button specified in XML

Lets add a Button to a programbased interface Step 1: Button specified in XML layout we want to process/handle Step 2: Implement Event Handler TWO OPTIONS – separate class to handle event(s), OR have the Activity containing the button do the event handling for a Button means implementing the View. On. Click. Listener interface Step 3: Register Event Handler

Event handling done by Activity itself Here code to handle is inside Activity itself

Event handling done by Activity itself Here code to handle is inside Activity itself public class Example. Activity extends Activity implements On. Click. Listener { protected void on. Create(Bundle saved. Values) { . . . Button button = (Button)find. View. By. Id(R. id. button_DO_IT); //STEP 1 button. set. On. Click. Listener(this); //STEP 3 - registration } // Implement the On. Click. Listener callback //STEP 2 –event handler public void on. Click(View v) { // do something when the button is clicked } . . . } NOTE: in this example, the Activity is the Event Handler as it is implementing the On. Click. Listenerinterface ---often you may do an anonymous inner class instead or a regular class if you want to reuse it

Event Handling - here have a SEPARATE anonymous inner class EVENT HANDLING CODE in

Event Handling - here have a SEPARATE anonymous inner class EVENT HANDLING CODE in separate object m. Corky. Listner // Create an anonymous implementation of On. Click. Listener STEP 2 private On. Click. Listener m. Do. IT_Listener = new On. Click. Listener() { public void on. Click(View v) { // do something when the button is clicked Creating instance of } anonymous inner class to }; implement the On. Click. Listener //Now inside your Activity class protected void on. Create(Bundle saved. Values) { . . . // STEP 1: Capture our button from layout Button button = (Button)find. View. By. Id(R. id. corky); // STEP 3 : Register the on. Click listener with the implementation above button. set. On. Click. Listener(m. Do. IT_Listener); . . . }

Lets do it step by step In Android. Studio

Lets do it step by step In Android. Studio

STEP 1: Create a Button Create button using XML GUI interface Set property so

STEP 1: Create a Button Create button using XML GUI interface Set property so label on button is “DO IT” Also, added text view with instructions telling user to hit button

STEP 2: Register the main Application Activity as Event. Handler for button Grab button

STEP 2: Register the main Application Activity as Event. Handler for button Grab button associated with its id, store in local variable b. Call seton. Click. Listener(this) public class Main. Activity extends Activity implements On. Click. Listener { int hits =0; /** Called when the activity is first created. */ @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Button b = (Button)find. View. By. Id(R. id. button_DO_IT); b. set. On. Click. Listener(this); //SETP 3: register handler }

STEP 2: Register the main Application Activity as Event. Handler for button Have the

STEP 2: Register the main Application Activity as Event. Handler for button Have the main class “Main. Activity” implement On. Click. Listener for button In method on. Click increment hits and use in display string. public class Main. Activity extends Activity implements. On. Click. Listener { int hits =0; /** Called when the activity is first created. */ @Override public void on. Create(Bundle saved. Instance. State) { //see previous slide……. . } // Implement the On. Click. Listener callback //STEP 3 –event handler for button being hit public void on. Click(View v) { // do something when the button is clicked this. hits++; Text. View text = (Text. View)find. View. By. Id(R. id. text_Message); String s = "You hit me " + this. hits; text. set. Text(s); } }

Run your code Lets run the previous code.

Run your code Lets run the previous code.

After running it—before hit button

After running it—before hit button

After hitting button a few times

After hitting button a few times

What can you do with Event Handling …. your imagination is the limit…. .

What can you do with Event Handling …. your imagination is the limit…. .

Remember ……. Widget : Views that have events For a list of the widgets

Remember ……. Widget : Views that have events For a list of the widgets provided by Android, see the android. widget package. Some Examples Button Check. Box Date. Picker Edit. Text Image. View Search. View Spinner

Don’t forget…. Intents and Intent Recievers …less coupled ways to have things happen. ….

Don’t forget…. Intents and Intent Recievers …less coupled ways to have things happen. …. but, that is another lecture

Explore our Website, Book and… Look at our website, book and Android developer websites

Explore our Website, Book and… Look at our website, book and Android developer websites for examples of event handling