150 likes | 279 Views
Basic Functionality in Android. Functionality in Android. Events in Java mouse related mouse clicked button down or up mouse entered many others key pressed item selected (checkbox, table, list box) tabbed pane selection made others…. Tying a method to a Button. 3 approaches
E N D
Functionality in Android • Events in Java • mouse related • mouse clicked • button down or up • mouse entered • many others • key pressed • item selected (checkbox, table, list box) • tabbed pane selection made • others…
Tying a method to a Button • 3 approaches • By implementing the OnClickListener interface • By using an anonymous inner class • Most common • Same way it is done in typical Graphical programming • Within xml
.xml file used in following examples <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android=“http://schemas.android.com/apk/res/android” … <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Print Hello" android:id="@+id/pb1" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Print Goodbye" android:id="@+id/pb2" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et1" /> </LinearLayout>
Implementing OnClickListener • OnClickListenerInterface • android.view.View.OnClickListener • one abstract method: public abstract void onClick (View v); • widget must ‘listen’ for this event
Implementing OnClickListener • Implementing the interface and “Listening” for the event public class OnClickWithSwitch extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View button1 = findViewById(R.id.pb1); button1.setOnClickListener(this); View button2 = findViewById(R.id.pb2); button2.setOnClickListener(this); }
Implementing OnClickListener • Implementing onClick() public void onClick (View v) { switch (v.getId()) { case R.id.pb1: ((TextView)findViewById(R.id.et1)).setText("Hello"); break; case R.id.pb2: ((TextView)findViewById(R.id.et1)).setText("Goodbye"); break; default: ((TextView)findViewById(R.id.et1)).setText("Nothing"); } } }
Using an anonymous inner class • With anonymous inner class • OnClickListener not explicitly implemented • Each widget must still ‘listen’ for the event • when the listener is set, an anonymous inner class is used • inner class is a subclass of OnClickListener • onClick is implemented • Each widget implements functionality accordingly
Using anonymous inner class • Utilizing an anonymous inner class public class OnClickWithInner extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); (findViewById(R.id.pb1)).setOnClickListener(new OnClickListener () { public void onClick (View v) { ((TextView)findViewById(R.id.et1)).setText("Hello"); } }); (findViewById(R.id.pb2)).setOnClickListener(new OnClickListener () { public void onClick (View v) { ((TextView)findViewById(R.id.et1)).setText("Goodbye"); } }); } }
Using onClick XML attribute • With XML • onClick is an attribute in XML • Attribute value is set to the name of the method that is tied to the button • android:onClick=“methodName” • Method is written with the following signature: • public void methodName(View v)
Using onClick XML attribute • .xml file used in the following example: <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android=“http://schemas.android.com/apk/res/android” … <Button android:layout_width=“match_parent" android:layout_height="wrap_content" android:text="Print Hello" android:id="@+id/pb1“ android:onClick=“displayHello“ /> <Button android:layout_width=“match_parent" android:layout_height="wrap_content" android:text="Print Goodbye" android:id="@+id/pb2“ android:onClick="displayGoodbye" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et1" /> </LinearLayout>
Using onClick XML attribute • Implementing the methods named in XML public class OnClickWithXML extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void displayHello(View v) { ((TextView)findViewById(R.id.et1)).setText("Hello"); } public void displayGoodbye(View v) { ((TextView)findViewById(R.id.et1)).setText("Goodbye"); } }