Android Widget Event Handling PDF
Android Widget Event Handling PDF
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Spring, Hibernate/JPA, EJB3, GWT, Hadoop, SOAP-based and RESTful Web Services
Contact [email protected] for details Developed and taught by well-known author and developer. At public venues or onsite at your location.
Using the main Activity Copying and renaming Eclipse Android projects
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Idea
Goal
Change color of a TextView when Button or RadioButton is pressed. Different colors depending on which pressed.
Approach
Use an external class that implements View.OnClickListener
Import android.view.View.OnClickListener, then say implements OnClickListener
Advantages
You can pass arguments to change behavior Separate classes generally promote loose coupling
So, if event handler can be applied to different controls, it can be change independently from rest of app.
But, in most real situations, behavior is tightly coupled to app anyhow.
Disadvantages
If you want to call code in main Activity, you need reference Even then, that code in main Activity must be public
7
Summary of Layout
Button Button Button Horizontal RadioGroup
(Containing 3 RadioButtons)
Vertical LinearLayout
TextView
(No text, but controls will change the background color of this region.)
An upcoming tutorial section gives details on using layouts. However, you can do a pretty lot now by knowing just two simple things: 1) 2) You can make some pretty complex layouts by nesting horizontal and vertical layouts inside each other. You can experiment interactively with the visual layout editor in Eclipse. Edit main.xml and click on Graphical Layout.
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" Overall layout is a vertical stack of graphical items. android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="match_parent" This part defines the 3 buttons shown on the previous slide. android:text="@string/red_prompt"/> Each button is given an id so that it can be found in Java via <Button findViewById, then assigned an event handler via android:id="@+id/button2" setOnClickListener. android:layout_height="wrap_content" The text (Button label) is taken from strings.xml instead of entered directly here, because the same label will also be android:layout_width="match_parent" used for RadioButtons. android:text="@string/blue_prompt"/> <Button android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/yellow_prompt"/>
res/layout/main.xml (Continued)
<RadioGroup android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal"> <RadioButton android:id="@+id/radio_button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/red_prompt"/> A horizontal RadioGroup gives the same layout <RadioButton as a horizontal LinearLayout, except that it contains only RadioButtons. A RadioGroup also android:id="@+id/radio_button2" means that only one of the RadioButtons inside android:layout_height="wrap_content" can be selected at any given time. android:layout_width="wrap_content" android:text="@string/blue_prompt"/> <RadioButton android:id="@+id/radio_button3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/yellow_prompt"/> </RadioGroup>
10
res/layout/main.xml (Continued)
<TextView android:id="@+id/color_region" android:layout_height="match_parent" android:layout_width="match_parent"/> </LinearLayout>
This defines the blank region at the bottom that will change colors when the Buttons or RadioButtons are clicked. I used a TextView because I might later want to put some text inside.
11
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Event Handling Example</string> <string name="red_prompt">Red</string> <string name="blue_prompt">Blue</string> <string name="yellow_prompt">Yellow</string> </resources>
main.xml refers to these names with @string/red_prompt, @string/blue_prompt, and @string/yellow_prompt. Each string is used as label for one Button and one RadioButton.
12
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mColorRegion = findViewById(R.id.color_region); Button b1 = (Button)findViewById(R.id.button1); Button b2 = (Button)findViewById(R.id.button2); Button b3 = (Button)findViewById(R.id.button3); RadioButton r1 = (RadioButton)findViewById(R.id.radio_button1); RadioButton r2 = (RadioButton)findViewById(R.id.radio_button2); RadioButton r3 = (RadioButton)findViewById(R.id.radio_button3);
13
ColorSetter(Color.RED, this)); ColorSetter(Color.BLUE, this)); ColorSetter(Color.YELLOW, this)); ColorSetter(Color.RED, this)); ColorSetter(Color.BLUE, this)); ColorSetter(Color.YELLOW, this));
14
Event handler must store a reference to the main Activity so that it can call back to it. Another option in this particular case would be to pass the TextView to the event handler, but passing the main Activity is a more general solution.
Results on Emulator
16
17
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Idea
Goal
Change color of a TextView when Button or RadioButton is pressed. Different colors depending on which pressed.
Same as previous example
Approach
Use an inner class that implements View.OnClickListener
Advantages
You can pass arguments to change behavior Event handler methods can access private data of Activity. No reference is needed to call to Activity.
Disadvantages
Since Listener class is in same file as Activity, it is more tightly coupled, and cannot be changed independently
19
res/values/strings.xml
Defines the app name and the labels of the Buttons and RadioButtons
20
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mColorRegion = findViewById(R.id.color_region); Button b1 = (Button)findViewById(R.id.button1); Button b2 = (Button)findViewById(R.id.button2); Button b3 = (Button)findViewById(R.id.button3); RadioButton r1 = (RadioButton)findViewById(R.id.radio_button1); RadioButton r2 = (RadioButton)findViewById(R.id.radio_button2); RadioButton r3 = (RadioButton)findViewById(R.id.radio_button3);
21
23
Results on Emulator
Same as previous example.
24
25
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Idea
Goal
Randomly change color of TextView when Button is pressed.
Approach
Use an anonymous inner class that implements the Listener
Advantages
Assuming that each class is applied to a single control only, same advantages as named inner classes, but shorter.
This approach is widely used in Swing, SWT, AWT, and GWT.
Disadvantages
If you applied the handler to more than one control, you would have to cut and paste the code for the handler.
This approach should be applied for a single control only
If the code for the handler is long, it makes the code harder to read by putting it inline.
27
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/color_button" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/button_prompt"/> <TextView android:id="@+id/color_region" android:layout_height="match_parent" android:layout_width="match_parent"/> </LinearLayout>
28
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Event Handling Example</string> <string name="button_prompt">Random Color</string> </resources>
29
30
31
32
Results on Emulator
34
35
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Idea
Goal
Randomly change color of TextView when Button is pressed.
Same as previous example
Approach
Have the main Activity implement the Listener interface. Put the handler method in the main Activity. Call setOnClickListener(this).
Advantages
Assuming that the app has only a single control of that Listener type, this is the shortest and simplest of the approaches.
Disadvantages
Scales poorly to multiple controls unless they have completely identical behavior.
If you assigned this as the handler for more than one control of the same Listener type, the onClick (or whatever) method would have to have cumbersome if statements to see which control was clicked This approach should be applied when your app has only a single control of that Listener type
res/values/strings.xml
Defines the app name and the label of the Button
38
39
40
Results on Emulator
Same as previous example.
41
42
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Idea
Goal
Randomly change color of TextView when Button is pressed.
Same as previous example
Approach
Put the handler method in the main Activity. Do not implement a Listener interface or call setOnClickListener. Have the layout file (main.xml) specify the handler method via the android:onClick attribute.
Advantages
Assuming that the app has only a single control of that Listener type, mostly the same advantages (short/simple code) as the previous approach where the Activity implemented the interface. More consistent with the do layout in XML strategy You can supply different method names for different controls, so not nearly as limited as interface approach.
Disadvantages
You cannot pass arguments to Listener. Less clear to the Java developer which method is the handler for which control Since no @Override, no warning until run time if method is spelled wrong or has wrong argument signature
44
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/color_button" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/button_prompt" android:onClick="randomizeColor"/> <TextView This is the name of the event android:id="@+id/color_region" handler method in the main class. android:layout_height="match_parent" This method must have a void return type and take a View as an android:layout_width="match_parent"/> argument. However, the method </LinearLayout> name is arbitrary, and the main
45
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Event Handling Example</string> <string name="button_prompt">Random Color</string> </resources>
46
47
48
Results on Emulator
Same as previous example.
49
50
Developed and taught by well-known author and developer. At public venues or onsite at your location.
52
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Options
Use a separate event handler class
Pros: can pass args to handler to customize behavior, easier to change independently of main app Cons: if handler will call code in main Activity, must pass this and must make methods public
Options
Use an anonymous inner class
Pros: same as named inner class, but more concise Cons: confusing to newbies or if handler code is long
Put handler method in Activity, no interface, specify method with android:onClick in main.xml
Pros: one method per control, but can specify different methods for each control. More XML-oriented. Less Java code. Cons: more confusing to Java developer (arguably)
55
Questions?
JSF 2, PrimeFaces, Java 7, Ajax, jQuery, Hadoop, RESTful Web Services, Android, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training.
Developed and taught by well-known author and developer. At public venues or onsite at your location.