1 / 15

Basic Functionality in Android

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

falala
Download Presentation

Basic Functionality in Android

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Functionality in Android

  2. 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…

  3. 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

  4. .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>

  5. Approach 1Implementing the OnClickListener interface

  6. Implementing OnClickListener • OnClickListenerInterface • android.view.View.OnClickListener • one abstract method: public abstract void onClick (View v); • widget must ‘listen’ for this event

  7. 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); }

  8. 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"); } } }

  9. Method 2Using an anonymous inner class

  10. 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

  11. 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"); } }); } }

  12. Method 3Implementing onClick via XML

  13. 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)

  14. 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>

  15. 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"); } }

More Related