CHAPTER 9
USING
CHOICE BASED
CONTROLS
What Are Choice Controls ?
Choice controls are those which allow the user to
make a selection from the available list of options
Android supports 6 basic choice controls which are:
Checkbox
RadioButton
Switch
ToggleButton
ListView
Spinner
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.
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:id ="@+id/checkbox"
android:layout_width="
android:layout_width ="wrap_content
wrap_content""
android:layout_height="
android:layout_height ="wrap_content
wrap_content""
android:text="Check
android:text ="Check me" />
Event Handling In
CheckBox
It can be done in 2 ways:
A.
1. Implement the OnClickListener interface
2. Register the CheckBox object as source and our
MainActivity
Activity class as Listener by calling the
setOnClickListener(( ) method.
setOnClickListener
3. Override the method onClick
onClick(( )
Determining State Of
CheckBox
To determine whether CheckBox is on or off we can
use the method isChecked
isChecked(( ) of CheckBox object:
It’s prototype is:
public boolean isChecked
isChecked()
()
It returns true if the CheckBox is on otherwise it
returns false
Setting State Of CheckBox
To programmatically set the state of CheckBox we
can use it’s method setChecked
setChecked(( )
It’s prototype is
public void setChecked
setChecked((boolean
boolean))
If we pass true then CheckBox becomes selected
(ON) and if false is passed it becomes deselected
(OFF)
Event Handling In CheckBox
B.
1. Implement the OnCheckedChangeListener
interface
2. Register the CheckBox object as source and our
MainActivity
Activity class as Listener by calling the
setOnCheckedChangeListener(( ) method.
setOnCheckedChangeListener
3. Override the method onCheckedChanged
onCheckedChanged(( )
Event Handling In CheckBox
Prototype of onCheckedChanged
onCheckedChanged()
()
public void
onCheckedChanged(CompoundButton
buttonView, boolean isChecked)
Event Handling In CheckBox
First argument is a reference to the CheckBox
clicked . It is of class CompoundButton , since
CompoundButton is the base class for CheckBox as
well as other choice controls except ListView and
Spinner.
The second argument is a boolean value which is
true if the CheckBox is selected , otherwise it is false
EXERCISE
EXERCISE
BILLING APPLICATION
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.
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
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>
Event Handling In
RadioButton
It can be done in 2 ways:
A.
1. Implement the OnClickListener interface
2. Register the RadioButton object as source and our
MainActivity
Activity class as Listener by calling the
setOnClickListener(( ) method.
setOnClickListener
3. Override the method onClick
onClick(( )
Event Handling In
RadioButton
B.
1. Implement the OnCheckedChangeListener
interface
2. Register the RadioButton object as source and
MainActivity
Activity class as Listener by calling the
setOnCheckedChangeListener(( ) method.
setOnCheckedChangeListener
3. Override the method onCheckedChanged
onCheckedChanged(( )
RadioButton Methods
public boolean isChecked
isChecked()
()
public void setChecked
setChecked((boolean
boolean))
public Editable getText
getText()
()
RadioButton Methods
public void setText
setText((CharSequence
CharSequence))
public void
setOnClickListener((OnClickListener
setOnClickListener OnClickListener))
public void
setOnCheckedChangeListener((OnCheckedC
setOnCheckedChangeListener
hangeListener))
hangeListener
EXERCISE
Using ToggleButton
A ToggleButton is similar to a CheckBox in
behavior.
But it displays checked/unchecked states as a
button with a "light" indicator and by default
accompanied with the text "ON" or "OFF".
Creating ToggleButton
<ToggleButton
android:id="@+id/toggleButton1"
android:id ="@+id/toggleButton1"
android:layout_width="
android:layout_width ="wrap_content
wrap_content""
android:layout_height="
android:layout_height ="wrap_content
wrap_content""
android:layout_below="@+id/textView1"
android:layout_below ="@+id/textView1"
android:layout_marginTop="16dp"
android:layout_marginTop ="16dp"
/>
Changing Text On
ToggleButton
Although a ToggleButton has a text property but
it’s value does not get displayed.
Rather to change it’s text we need to set it’s textOn
and textOff properties.
textOn displays the text when ToggleButton is in
checked state
textOff displays the text when ToggleButton is in
unchecked state
The default text for these is “ON” and “OFF,”
respectively.
Creating ToggleButton
<ToggleButton
android:id="@+id/toggleButton1"
android:id ="@+id/toggleButton1"
android:layout_width="
android:layout_width ="wrap_content
wrap_content""
android:layout_height="
android:layout_height ="wrap_content
wrap_content""
android:textOff="Hide
android:textOff ="Hide Password"
android:textOn="Show
android:textOn ="Show Password"
android:layout_below="@+id/textView1"
android:layout_below ="@+id/textView1" />
Event Handling In
ToggleButton
It can be done in 2 ways:
A.
1. Implement the OnCheckedChangeListener
interface
2. Register the ToggleButton object as source and
our MainActivity class as Listener by calling the
setOnCheckedChangeListener(( ) method.
setOnCheckedChangeListener
3. Override the method onCheckedChanged
onCheckedChanged(( )
Event Handling In
ToggleButton
B.
1. Implement the OnClickListener interface
2. Register the ToggleButton object as source and
our MainActivity class as Listener by calling the
setOnClickListener(( ) method.
setOnClickListener
3. Override the method onClick
onClick(( )
ToggleButton Methods
public boolean isChecked
isChecked()
()
public void setChecked
setChecked((boolean
boolean))
public EdiTable getText
getText()
()
Using Switch Button
A Switch is a two-state toggle switch widget that
can select between two options.
The user may drag the "thumb" back and forth to
choose the selected option, or simply tap to toggle
as if it were a checkbox.
Creating Switch
<Switch
android:id="@+id/switch1"
android:id ="@+id/switch1"
android:layout_width="
android:layout_width ="wrap_content
wrap_content""
android:layout_height="
android:layout_height ="wrap_content
wrap_content""
android:layout_below="@+id/textView1"
android:layout_below ="@+id/textView1"
android:layout_marginTop="42dp"
android:layout_marginTop ="42dp"
android:text="Switch
android:text ="Switch Button Demo"
/>
Event Handling In
Switch
1. Implement the OnCheckedChangeListener
interface
2. Register the Switch object as source and our
MainActivity class as Listener by calling the
setOnCheckedChangeListener(( ) method.
setOnCheckedChangeListener
3. Override the method onCheckedChanged
onCheckedChanged(( )
End Of Lecture
Call us @ : 0755-4271659, 7879165533
Agenda for Next Lecture:
1. Using The ListView Control
2. ListView Attributes
3. Setting Data In ListView Using XML
4. Setting Data In ListView Dynamically
5. Event Handling In ListView