Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ELET4133: Embedded Systems Topic 9 Widget Event Handling Originals of Slides and Source Code for Examples: http://www.coreservlets.com/android-tutorial/ Agenda • Develop an App to change the colors of the main screen (a TextView) • Will use three approaches – Use a separate Listener class – Use a named inner class – Use an anonymous inner class (AIC) • All approaches work well with multiple controls – Use the same event handler – But pass parameters (like the color) into it 9/27/2013 http://www.coreservlets.com 2 The App • The App will appear as shown in the emulator Buttons Vertical Linear Layout Buttons3 Radio Button Group Text Area 9/27/2013 http://www.coreservlets.com 3 The App • The App will – Change the color of the TextView area when a Button or a RadioButton is pressed. – Different colors depending on which pressed. • Approach – Use an external class that implements View.OnClickListener – Import android.view.View.OnClickListener • use “implements OnClickListener” • A “Listener” is a class that waits for an event to occur – When the event occurs it causes this class to execute 9/27/2013 http://www.coreservlets.com 4 The Advantages/Disadvantages • The advantages and disadvantages to this approach – 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. – Disadvantages • If you want to call the code in main Activity, you need a reference – Even then, that code in the main Activity must be public 9/27/2013 http://www.coreservlets.com 5 Start with the Layout: activity_main.xml file 9/27/2013 6 res/layout/activity_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/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:textColor="#ff0000" android:text="@string/red_prompt"/> <Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="match_parent" android:textColor="#0000ff" android:text="@string/blue_prompt"/> <Button android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="match_parent" android:textColor="#ffff00" android:text="@string/yellow_prompt"/> 9/27/2013 http://www.coreservlets.com The first part of activity_main.xml: Starts the LinearLayout 7 res/layout/activity_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/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:textColor="#ff0000" android:text="@string/red_prompt"/> <Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="match_parent" android:textColor="#0000ff" android:text="@string/blue_prompt"/> <Button android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="match_parent" android:textColor="#ffff00" android:text="@string/yellow_prompt"/> 9/27/2013 http://www.coreservlets.com The first part of activity_main.xml: Starts the LinearLayout Defines the 3 buttons 8 res/layout/activity_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/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:textColor="#ff0000" android:text="@string/red_prompt"/> <Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="match_parent" android:textColor="#0000ff" android:text="@string/blue_prompt"/> <Button android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="match_parent" android:textColor="#ffff00" android:text="@string/yellow_prompt"/> 9/27/2013 http://www.coreservlets.com The first part of activity_main.xml: Starts the LinearLayout Defines the 3 buttons Radio Button and the Radio Group come next 9 res/layout/activity_main.xml <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"/> <RadioButton android:id="@+id/radio_button2" android:layout_height="wrap_content" 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> <TextView android:id="@+id/color_region" android:layout_height="match_parent" android:layout_width="match_parent"/> </LinearLayout> 9/27/2013 http://www.coreservlets.com The RadioGroup: must use the “group” object so that only one button can be selected at a time 10 res/layout/activity_main.xml <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"/> <RadioButton android:id="@+id/radio_button2" android:layout_height="wrap_content" 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> <TextView android:id="@+id/color_region" android:layout_height="match_parent" android:layout_width="match_parent"/> </LinearLayout> 9/27/2013 http://www.coreservlets.com The RadioGroup: must use the “group” object so that only one button can be selected at a time Then, each of the 3 radio buttons are declared 11 res/layout/activity_main.xml <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"/> <RadioButton android:id="@+id/radio_button2" android:layout_height="wrap_content" 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> <TextView android:id="@+id/color_region" android:layout_height="match_parent" android:layout_width="match_parent"/> </LinearLayout> 9/27/2013 http://www.coreservlets.com The RadioGroup: must use the “group” object so that only one button can be selected at a time Then, each of the 3 radio buttons are declared Finally, the text view is declared and then the layout is complete 12 res/layout/activity_main.xml • The App’s appearance • No functionality yet 9/27/2013 http://www.coreservlets.com 13 res/layout/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> The title of the App It can easily be changed just by editing this file and then saving it The text on each of the 3 buttons 9/27/2013 http://www.coreservlets.com 14 gen/packageName/R.java • The main java file (Events1Example.java) will use this file to find the IDs for each of the objects • This file is automatically generated by Eclipse 9/27/2013 http://www.coreservlets.com 15 The Main Activity Class: Events1Example.java Has a separate Event Handler Class: ColorSetter.java 9/27/2013 16 gen/packageName/Events1Example.java The first part of Events1Example.java : It extends Activity (makes this a subclass of Activity so @Override that it inherits all of the public void onCreate(Bundle savedInstanceState) { Activity Class’s functionality) super.onCreate(savedInstanceState); public class Events1Example extends Activity { private View mColorRegion; 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 9/27/2013 http://www.coreservlets.com 17 gen/packageName/Events1Example.java The first part of Events1Example.java : It extends Activity (makes this a subclass of Activity so @Override that it inherits all of the public void onCreate(Bundle savedInstanceState) { Activity Class’s functionality) super.onCreate(savedInstanceState); public class Events1Example extends Activity { private View mColorRegion; 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 9/27/2013 It contains the onCreate () method required in the main java class http://www.coreservlets.com 18 gen/packageName/Events1Example.java The first part of Events1Example.java : public class Events1Example extends Activity { private View mColorRegion; It declares the content view and color region (note that it looks for the IDs in R.java) @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 9/27/2013 http://www.coreservlets.com 19 gen/packageName/Events1Example.java The first part of Events1Example.java : public class Events1Example extends Activity { private View mColorRegion; It declares the content view and color region (note that it looks for the IDs in R.java) @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 It finds the IDs for the 3 buttons and The 3 radio buttons 9/27/2013 It finds their IDs by referencing the R.java file http://www.coreservlets.com 20 gen/packageName/Events1Example.java The second part of Events1Example.java : b1.setOnClickListener(new b2.setOnClickListener(new b3.setOnClickListener(new r1.setOnClickListener(new r2.setOnClickListener(new r3.setOnClickListener(new ColorSetter(Color.RED, this)); ColorSetter(Color.BLUE, this)); ColorSetter(Color.YELLOW, this)); ColorSetter(Color.RED, this)); ColorSetter(Color.BLUE, this)); ColorSetter(Color.YELLOW, this)); } public void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } Assigns a class as the event handler for each of the Buttons and RadioButtons. } 9/27/2013 http://www.coreservlets.com 21 gen/packageName/Events1Example.java The second part of Events1Example.java : b1.setOnClickListener(new b2.setOnClickListener(new b3.setOnClickListener(new r1.setOnClickListener(new r2.setOnClickListener(new r3.setOnClickListener(new ColorSetter(Color.RED, this)); ColorSetter(Color.BLUE, this)); ColorSetter(Color.YELLOW, this)); ColorSetter(Color.RED, this)); ColorSetter(Color.BLUE, this)); ColorSetter(Color.YELLOW, this)); } public void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } } You can pass arguments to the event handler (the colors) so that the same event handler class can have different behaviors for different controls (buttons and radioButtons) 9/27/2013 http://www.coreservlets.com 22 gen/packageName/Events1Example.java The second part of Events1Example.java : b1.setOnClickListener(new b2.setOnClickListener(new b3.setOnClickListener(new r1.setOnClickListener(new r2.setOnClickListener(new r3.setOnClickListener(new ColorSetter(Color.RED, this)); ColorSetter(Color.BLUE, this)); ColorSetter(Color.YELLOW, this)); ColorSetter(Color.RED, this)); ColorSetter(Color.BLUE, this)); ColorSetter(Color.YELLOW, this)); } public void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } You have to pass a reference to the main Activity (“this” above) so that the event handler can call back to code in that Activity. } 9/27/2013 http://www.coreservlets.com 23 gen/packageName/ColorSetter.java The Event Handler Class: public class ColorSetter implements OnClickListener { private int regionColor; private Events1Example mainActivity; public ColorSetter(int regionColor, Events1Example mainActivity) { this.regionColor = regionColor; this.mainActivity = mainActivity; } Event handler must store a reference to the main Activity so that it can call back to it. @Override public void onClick(View v) { mainActivity.setRegionColor(regionColor); } } 9/27/2013 http://www.coreservlets.com 24 gen/packageName/ColorSetter.java The Event Handler Class: public class ColorSetter implements OnClickListener { private int regionColor; private Events1Example mainActivity; public ColorSetter(int regionColor, Events1Example mainActivity) { this.regionColor = regionColor; this.mainActivity = mainActivity; } Event handler must store a reference to the main Activity so that it can call back to it. @Override public void onClick(View v) { mainActivity.setRegionColor(regionColor); } } 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. 9/27/2013 http://www.coreservlets.com 25 Results on the Emulator 9/27/2013 http://www.coreservlets.com 26 The Main Activity Class Using a Named Inner Class for Event Handling 9/27/2013 27 The App • Goal – Change color of a TextView when Button or RadioButton is pressed. Different colors depending on which pressed. • Same App 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 9/27/2013 http://www.coreservlets.com 28 XML Files: Same as Previous Example • res/layout/main.xml – Defines vertical LinearLayout that contains 3 Buttons, a horizontal RadioGroup (with 3 RadioButtons), and a TextView. – The Buttons, RadioButtons, and TextView have IDs so that they can be referred to in the Java code • res/values/strings.xml – Defines the app name and the labels of the Buttons and RadioButtons 9/27/2013 http://www.coreservlets.com 29 Main Activity Class public class Events2Example extends Activity { private View mColorRegion; Except for the class name, this top part of the Activity is exactly the same as the previous example. @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); 9/27/2013 http://www.coreservlets.com 30 Main Activity Class b1.setOnClickListener(new b2.setOnClickListener(new b3.setOnClickListener(new r1.setOnClickListener(new r2.setOnClickListener(new r3.setOnClickListener(new ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); } private void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } 9/27/2013 http://www.coreservlets.com Assigns an inner class as the event handler for each of the Buttons and RadioButtons. 31 Main Activity Class b1.setOnClickListener(new b2.setOnClickListener(new b3.setOnClickListener(new r1.setOnClickListener(new r2.setOnClickListener(new r3.setOnClickListener(new ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); } private void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } Assigns an inner class as the event handler for each of the Buttons and RadioButtons. As with the previous example, you can pass arguments to the event handler (the colors) so that the same event handler class can have different behaviors for different controls 9/27/2013 http://www.coreservlets.com 32 Main Activity Class b1.setOnClickListener(new b2.setOnClickListener(new b3.setOnClickListener(new r1.setOnClickListener(new r2.setOnClickListener(new r3.setOnClickListener(new ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); } private void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } As with the previous example, you can pass arguments to the event handler (the colors) so that the same event handler class can have different behaviors for different controls 9/27/2013 http://www.coreservlets.com Assigns an inner class as the event handler for each of the Buttons and RadioButtons. Since the event handler is in the same class, you do not have to supply a reference to the main Activity class. 33 Main Activity Class b1.setOnClickListener(new b2.setOnClickListener(new b3.setOnClickListener(new r1.setOnClickListener(new r2.setOnClickListener(new r3.setOnClickListener(new ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); } private void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } Note no closing brace. This class is not finished yet (continued on next slide) 9/27/2013 http://www.coreservlets.com Since this method will only be called by the method in inner event handler class, it is allowed to be private. 34 Main Activity Class private class ColorSetter implements OnClickListener { private int regionColor; public ColorSetter(int regionColor) { this.regionColor = regionColor; } @Override public void onClick(View v) { setRegionColor(regionColor); } } } Event handler can directly call methods in the main activity, even if the method is private. Closes the class 9/27/2013 http://www.coreservlets.com 35 Results on the Emulator Results are the same as the previous example 9/27/2013 http://www.coreservlets.com 36 The Main Activity Class Using an Anonymous Inner Class (AIC) for Event Handling 9/27/2013 37 Main Activity Class public class Events2Example extends Activity { private View mColorRegion; Except for the class name, this top part of the Activity is exactly the same as the previous example. @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); 9/27/2013 http://www.coreservlets.com 38 Original Main Activity Class b1.setOnClickListener(new b2.setOnClickListener(new b3.setOnClickListener(new r1.setOnClickListener(new r2.setOnClickListener(new r3.setOnClickListener(new ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); } private void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } 9/27/2013 http://www.coreservlets.com In the original Main Activity Class, we called an event handler inner class (an onClickListener) for each button and passed data into it 39 Original Main Activity Class b1.setOnClickListener(new b2.setOnClickListener(new b3.setOnClickListener(new r1.setOnClickListener(new r2.setOnClickListener(new r3.setOnClickListener(new ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); ColorSetter(Color.RED)); ColorSetter(Color.BLUE)); ColorSetter(Color.YELLOW)); } private void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } In the original Main Activity Class, we called an event handler inner class (an onClickListener) for each button and passed data into it Then, the event handler called this method to change the color of the view area 9/27/2013 http://www.coreservlets.com 40 NEW Main Activity Class Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override The part in RED is the new public void onClick(View v) { setRegionColor(Color.RED); part: the anonymous inner } class }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.BLUE); } }); b3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.YELLOW); } }); 9/27/2013 http://www.coreservlets.com 41 Comparying Separate Classes and AICs private class ColorSetter implements OnClickListener { private int regionColor; This is an example of an @Override public void onClick(View v) { Event Handling Class declaration setRegionColor(regionColor); } } b1.set.OnClickListener(EventHandlingClass); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.RED); } }); 9/27/2013 http://www.coreservlets.com 42 Comparying Separate Classes and AICs private class ColorSetter implements OnClickListener { private int regionColor; This is an example of an @Override public void onClick(View v) { Event Handling Class declaration setRegionColor(regionColor); } This an example of assigning the } b1.set.OnClickListener(EventHandlingClass); Event Handling Class to a button b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.RED); } }); 9/27/2013 http://www.coreservlets.com 43 Comparying Separate Classes and AICs private class ColorSetter implements OnClickListener { private int regionColor; @Override public void onClick(View v) { setRegionColor(regionColor); Between the parentheses is the } Event Handling Class followed by } b1.set.OnClickListener(EventHandlingClass); a semicolon specifying the end of the statement b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.RED); } }); 9/27/2013 http://www.coreservlets.com 44 Comparying Separate Classes and AICs private class ColorSetter implements OnClickListener { private int regionColor; @Override public void onClick(View v) { setRegionColor(regionColor); Then, the implementation of the } class is between the braces } b1.set.OnClickListener(EventHandlingClass); b1.setOnClickListener (new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.RED); } }); 9/27/2013 http://www.coreservlets.com 45 Comparying Separate Classes and AICs private class ColorSetter implements OnClickListener { private int regionColor; @Override public void onClick(View v) { setRegionColor(regionColor); The implementation of the } onClick () method is between } these braces b1.set.OnClickListener(EventHandlingClass); b1.setOnClickListener (new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.RED); } }); 9/27/2013 http://www.coreservlets.com 46 Comparying Separate Classes and AICs private class ColorSetter implements OnClickListener { private int regionColor; @Override public void onClick(View v) { setRegionColor(regionColor); The statements of the onClick } method are the same (if you } b1.set.OnClickListener(EventHandlingClass); () replaced the variable regionColor) with a constant color definition b1.setOnClickListener (new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.RED); } }); 9/27/2013 http://www.coreservlets.com 47 Back to the New Main Activity Class Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override So, the part in RED is the public void onClick(View v) { setRegionColor(Color.RED); anonymous inner class } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.BLUE); } }); b3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.YELLOW); } }); 9/27/2013 http://www.coreservlets.com 48 NEW Main Activity Class Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override The part in RED is the public void onClick(View v) { setRegionColor(Color.RED); anonymous inner class } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.BLUE); Each AIC is identical except } for the color passed to the }); b3.setOnClickListener(new OnClickListener() {method setRegionColor() @Override public void onClick(View v) { setRegionColor(Color.YELLOW); } }); 9/27/2013 http://www.coreservlets.com 49 NEW Main Activity Class Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override The anonymous inner class public void onClick(View v) { setRegionColor(Color.RED); is declared here. } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.BLUE); } }); b3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.YELLOW); } }); 9/27/2013 http://www.coreservlets.com 50 NEW Main Activity Class Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override The anonymous inner class public void onClick(View v) { setRegionColor(Color.RED); is declared here. } }); We’ve called a new b2.setOnClickListener(new OnClickListener() { onClickListener, but didn’t @Override public void onClick(View v) { name it. setRegionColor(Color.BLUE); } }); b3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.YELLOW); } }); 9/27/2013 http://www.coreservlets.com 51 NEW Main Activity Class Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override The anonymous inner class public void onClick(View v) { setRegionColor(Color.RED); is declared here. } }); We’ve called a new b2.setOnClickListener(new OnClickListener() { onClickListener, but didn’t @Override public void onClick(View v) { name it. setRegionColor(Color.BLUE); } But, we did declare how to }); b3.setOnClickListener(new OnClickListener() { handle the event for that @Override Button public void onClick(View v) { setRegionColor(Color.YELLOW); } }); 9/27/2013 http://www.coreservlets.com 52 NEW Main Activity Class Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override The anonymous inner class public void onClick(View v) { setRegionColor(Color.RED); is declared here. } }); We’ve called a new b2.setOnClickListener(new OnClickListener() { onClickListener, but didn’t @Override public void onClick(View v) { name it. setRegionColor(Color.BLUE); } But, we did declare how to }); b3.setOnClickListener(new OnClickListener() { handle the event for that @Override Button public void onClick(View v) { setRegionColor(Color.YELLOW); And it is right here: } }); Obviously associated with the button 9/27/2013 http://www.coreservlets.com 53 NEW Main Activity Class Uses an Anonymous Inner Class for each button r1.setOnClickListener(new OnClickListener() { @Override The radio buttons use the public void onClick(View v) { setRegionColor(Color.RED); same AIC (except for color) } }); r2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.BLUE); Each calls the method } setRegionColor() }); r3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.YELLOW); } }); } 9/27/2013 http://www.coreservlets.com 54 This is the setRegionColor() method /** Sets the color of the color region. */ private void setRegionColor(int color) { mColorRegion.setBackgroundColor(color); } } This worked exactly the same as the two previous approaches • In this case, the AICs were redundant since each button was supposed to do the same thing. • Each AIC called the setRegionColor() method • Why not just put this inside each of the AICs? 9/27/2013 http://www.coreservlets.com 55 Advantages and Disadvantages • There are some disadvantages: – Each AIC was exactly the same except for the color • So there was a lot of redundancy • It may need more memory than is available – Possible but unlikely – Parameters can not be passed to the AICs – The android:onClick attribute cannot be used in XML file (not that that is so important) • Advantages: – The event handler is at the same place as the button (in the code) – Easy to see that the two are associated – You don’t have to search through code for the handler 9/27/2013 http://www.coreservlets.com 56 Results on the Emulator Results are the same as the previous examples 9/27/2013 http://www.coreservlets.com 57 One more change: delete the setRegionColor() method 9/27/2013 58 Remove the setRegionColor() method Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override Each AIC calls the public void onClick(View v) { setRegionColor(Color.RED); setRegionColor() method } change the color of the }); text view b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.BLUE); } }); b3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setRegionColor(Color.YELLOW); } }); 9/27/2013 http://www.coreservlets.com to 59 Remove the setRegionColor() method Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mColorRegion.setBackgroundColor(Color.RED); } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mColorRegion.setBackgroundColor(Color.BLUE); } }); b3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mColorRegion.setBackgroundColor(Color.YELLOW); } }); 9/27/2013 http://www.coreservlets.com Each AIC calls the setRegionColor() method to change the color of the text view But, why not change the color here instead of calling a separate method 60 Remove the setRegionColor() method Uses an Anonymous Inner Class for each button b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mColorRegion.setBackgroundColor(Color.RED); } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mColorRegion.setBackgroundColor(Color.BLUE); } }); b3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mColorRegion.setBackgroundColor(Color.YELLOW); } }); 9/27/2013 http://www.coreservlets.com Each AIC calls the setRegionColor() method to change the color of the text view But, why not change the color here instead of calling a separate method So each of the AICs was changed and the setRegionColor() method was deleted 61 Results on the Emulator And, of course, the results are the same as the previous examples 9/27/2013 http://www.coreservlets.com 62 Summary • Developed an App to change the colors of the main screen (a TextView) • Used three approaches – The first used a separate Listener class – The second used a named inner class – The third used an anonymous inner class • All approaches worked well with multiple controls – Using the same event handler – But passing parameters (like the color) into it 9/27/2013 http://www.coreservlets.com 63 Summary (continued) • I prefer the Anonymous Inner Class method, when it can be used – It is obviously the event handler for the button where it is declared – It is easy to find if code needs to be changed 9/27/2013 http://www.coreservlets.com 64