CS453 | mobile programming
  • outline
  • projects
  • syllabus
  • links

Android Event Handling for GUI

Two kinds of events dealing with Interface: 1) View level (GUI components) AND 2) Activity Level

 

View Level GUI Event Handling

 

Typically the View element involved with have a method that will let you register event handling code for it

Example:

main.xml

two button example<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
 <TextView
 	          android:layout_width="fill_parent"
 	          android:layout_height="wrap_content"
 	          android:text="Your Name" />
<EditText
 	          android:id="@+id/txt1"
 	          android:layout_width="214dp"
 	          android:layout_height="wrap_content"/>
<Button
 	          android:id="@+id/btn1"
 	          android:layout_width="106dp"
 	          android:text="OK"
 	          android:layout_height="wrap_content"/>
<Button
 	          android:id="@+id/btn2"
 	          android:layout_width="106dp"
 	          android:text="Cancel"
 	          android:layout_height="wrap_content"/>
</LinearLayout>

TwoButtonEventHandlingActivity.java -- HERE have one listener for both buttons, so does the same thing if either hit...see below for alternative

package grewe.twobutton;
//import android.R;
 	          import android.app.Activity;
 	          import android.os.Bundle;
 	          import android.widget.Button;
 	          import android.widget.Toast;
 	          import android.view.View.OnClickListener;
 	          import android.view.View;
 	          import android.util.Log;
public class TwoButtonEventHandlingActivity extends Activity {
 	          /** Called when the activity is first created. */
 	          @Override
 	          public void onCreate(Bundle savedInstanceState) {
 	              super.onCreate(savedInstanceState);
 	              setContentView(R.layout.main);
   
     	          //grab buttons
 	              Button btn1 = (Button) findViewById(R.id.btn1);
     	          Button btn2 = (Button) findViewById(R.id.btn2);
                                                                 
 	              //register the anonymous class instance btnListener as handler
     	          btn1.setOnClickListener(btnListener);
     	          btn2.setOnClickListener(btnListener);
 	          }
   
 	          //create anonymous class to handle events of button clicked
 	          private OnClickListener btnListener = new OnClickListener()
 	          {                                                         
 	              public void onClick(View v)
 	                  {
 	                      Toast.makeText(getBaseContext(), ((Button)v).getText() +" was clicked", Toast.LENGTH_LONG).show();
                        
 	                  }
 	          };
    
         }

ALTERNATIVE --- HAVE A DIFFERENT LISTENER FOR EACH BUTTON

main.xml --- SAME!!

 

AFTER HITTING CANCEL

2 button example with own handlers






















package grewe.twobutton;
 	          import android.app.Activity;
 	          import android.os.Bundle;
 	          import android.widget.Button;
 	          import android.widget.Toast;
 	          import android.view.View.OnClickListener;
 	          import android.view.View;
 	          import android.util.Log;
public class TwoButtonEventHandlingActivity extends Activity {
 	          /** Called when the activity is first created. */
 	          Button btn1,btn2;
 	          @Override
 	          public void onCreate(Bundle savedInstanceState) {
 	             super.onCreate(savedInstanceState);
 	             setContentView(R.layout.main);
      
 	             //grab buttons
 	             btn1 = (Button) findViewById(R.id.btn1);
 	             btn2 = (Button) findViewById(R.id.btn2);
   
 	             //register the anonymous class instance btnListener as handler
 	             btn1.setOnClickListener(btnListener1);
                 btn2.setOnClickListener(btnListener2);
 	          }
   
 	          //create anonymous class to handle events of button 1 clicked
 	          private OnClickListener btnListener1 = new OnClickListener()
 	          {
 	              public void onClick(View v)
 	              {
 	                  Toast.makeText(getBaseContext(), ((Button)v).getText() +" was clicked", Toast.LENGTH_LONG).show();
       
 	              }
 	          };
 	          
//create anonymous class to handle events of button 2 clicked private OnClickListener btnListener2 = new OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), ((Button)v).getText() +" inside btn2", Toast.LENGTH_LONG).show(); } }; }

 

Activity Level Event Handling

The following methods can be applied to specific View components but, can also, be to an Activity (or some in Fragment) when the corresponding event is not focused on a specific View component. You can OVERRIDE these methods in your Activity (some in Fragments) to recieve the events and handle them. See android api and book for examples or online.

 

onKeyDown(int, KeyEvent) Called when a new key event occurs.
onKeyUp(int, KeyEvent) Called when a key up event occurs.
onTrackballEvent(MotionEvent) Called when a trackball motion event occurs.
onTouchEvent(MotionEvent)
onMenuItemSelected(*)-only Activity
onMenuOpened(*) - only Activity

 

 

cs453:mobile programming

  • home
  • outline
  • projects
  • syllabus
  • links