chapter 6b(standard widgets)

18
Android development Standard Android Controls (CheckBox and RadioButton)

Upload: cshashank1992

Post on 05-May-2017

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHAPTER 6B(Standard Widgets)

Android development

Standard Android Controls(CheckBox and RadioButton)

Page 2: CHAPTER 6B(Standard Widgets)

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)

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)

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)

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)

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)

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)

EXERCISE

Page 9: CHAPTER 6B(Standard Widgets)
Page 10: CHAPTER 6B(Standard Widgets)

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)

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)

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)

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)

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)

RadioButton Methods• public boolean isChecked()

• public void setChecked(boolean)

• public EdiTable getText()

Page 16: CHAPTER 6B(Standard Widgets)

RadioButton Methods• public void setText(CharSequence)

• public void setOnClickListener(OnClickListener)

• public void setOnCheckedChangeListener(OnCheckedChangeListener)

Page 17: CHAPTER 6B(Standard Widgets)

EXERCISE

Page 18: CHAPTER 6B(Standard Widgets)