CHAPTER 6B(Standard Widgets)
-
Upload
cshashank1992 -
Category
Documents
-
view
214 -
download
0
Transcript of CHAPTER 6B(Standard Widgets)
![Page 1: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/1.jpg)
Android development
Standard Android Controls(CheckBox and RadioButton)
![Page 2: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/2.jpg)
Using CheckBox• The check box button is often used in lists of
items where the user can select multiple items. • The Android check box contains a text attribute
that appears to the side of the check box. • Because the Checkbox class is derived from the
TextView and Button classes, much of the attributes and methods behave in a similar fashion.
![Page 3: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/3.jpg)
Using CheckBox• Here’s an XML layout resource definition for a
simple CheckBox control with some default text displayed:
<CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check me" />
![Page 4: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/4.jpg)
Event Handling In CheckBox• It can be done in 2 ways:A.1. Implement the OnCheckedChangeListener
interface2. Register the CheckBox object as source and
Activity class as Listener by calling the setOnCheckedChangeListener( ) method.
3. Override the method onCheckedChanged( )
![Page 5: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/5.jpg)
Event Handling In CheckBoxB.1. Implement the OnClickListener interface2. Register the CheckBox object as source and
Activity class as Listener by calling the setOnClickListener( ) method.
3. Override the method onClick( )
![Page 6: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/6.jpg)
Determining State Of CheckBox
• To determine whether CheckBox is on or off we can use the method isChecked( ) of CheckBox object:
• It’s prototype is:public boolean isChecked()
• It returns true if the CheckBox is on otherwise it returns false
![Page 7: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/7.jpg)
Setting State Of CheckBox• To programmatically set the state of CheckBox
we can use it’s method setChecked( )• It’s prototype is
public void setChecked(boolean) • If we pass “true” then CheckBox becomes
selected (ON) and if “false” is passed it becomes deselected (OFF)
![Page 8: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/8.jpg)
EXERCISE
![Page 9: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/9.jpg)
![Page 10: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/10.jpg)
Using RadioButton• RadioButton controls are two-state widgets that
can be in either a checked or unchecked state. • The difference between check boxes and radio
buttons is that you can select more than one check box in a set, whereas radio buttons are mutually exclusive—only one radio button can be selected in a group.
• Selecting a radio button automatically deselects other radio buttons in the group.
![Page 11: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/11.jpg)
Creating RadioButton• To make RadioButtons mutually exclusive,
they are grouped together into the RadioGroup element so that no more than one can be selected at a time.
• To create a group of radio buttons, we first create a RadioGroup and then populate the group with few RadioButton controls
![Page 12: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/12.jpg)
Creating RadioButton<RadioGroup android:id="@+id/group_hotel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1"> <RadioButton android:id="@+id/radio_fivestar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Five Star " /> <RadioButton android:id="@+id/radio_threestar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Three Star" /></RadioGroup>
![Page 13: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/13.jpg)
Event Handling In RadioButton
• It can be done in 2 ways:A.1. Implement the OnCheckedChangedListener
interface2. Register the RadioButton object as source
and Activity class as Listener by calling the setOnCheckedChangeListener( ) method.
3. Override the method onCheckedChanged( )
![Page 14: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/14.jpg)
Event Handling In RadioButton
B.1. Implement the OnClickListener interface2. Register the RadioButton object as source
and Activity class as Listener by calling the setOnClickListener( ) method.
3. Override the method onClick( )
![Page 15: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/15.jpg)
RadioButton Methods• public boolean isChecked()
• public void setChecked(boolean)
• public EdiTable getText()
![Page 16: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/16.jpg)
RadioButton Methods• public void setText(CharSequence)
• public void setOnClickListener(OnClickListener)
• public void setOnCheckedChangeListener(OnCheckedChangeListener)
![Page 17: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/17.jpg)
EXERCISE
![Page 18: CHAPTER 6B(Standard Widgets)](https://reader035.fdocuments.in/reader035/viewer/2022070600/577ccd391a28ab9e788bd2ed/html5/thumbnails/18.jpg)