Checkboxradio
Checkboxradio
• Check box allows user to select one or more options from a set
• It is a type of 2 state button either checked or unchecked
• Check box class in Android is android.widget.CheckBox
• Methods
• public Boolean isChecked() →returns true if it is checked
• public void setChecked(Boolean status) →changes state of
checkbox
The CheckBox Control
• <CheckBox
android:id="@+id/chickenCB"
android:text="Chicken"
android:checked="true"
android:layout_width=“wrap_content“
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/fishCB“
android:text="Fish"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
• //initiate a check box
CheckBox simpleCheckBox = (CheckBox) findViewById
(R.id.simpleCheckBox);
//check current state of a check box (true or false)
Boolean checkBoxState = simpleCheckBox.isChecked();
Event listener
• CheckBox checkBoxState= (CheckBox) findViewById
(simpleCheckBox);
checkBoxState.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something
}
}
Radio button
• A radio button gives the users several choices and forces them to
select a single item
• To enforce this single-selection model, radio buttons generally
belong to a group, and each group is forced to have only one item
selected at a time.
• To create a group of radio buttons in Android, first create a
RadioGroup, and then populate the group with radio buttons
• Implement a radio group using android.widget.RadioGroup and
a radio button using android.widget.RadioButton.
<RadioGroup
android:id="@+id/rBtnGrp" android:layout_width="wrap_content"
android:layout_height="wrap_content“
android:orientation="vertical" >
<RadioButton
android:id="@+id/chRBtn" android:text="Chicken" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
To retrieve value using java code
• RadioButton food=(RadioButton )findViewById(R.id.chRBtn);
• Boolean state=chRBtn.isChecked();
• Click events can be handled using onClick()
ImageView
• This is used to display an image, where the image can come
from a file, a content provider, or a resource such as a drawable.
• we can even specify a color, and the ImageView will display
that color.
• Implement using android.widget.ImageView;
• <ImageView android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />
• ImageView image=(ImageView)findViewById(R.id.imageView1);
image.setImageResource(R.drawable.capture);
• Click events can be handled using onClick()
Date and Time Controls
• Android provides controls for the user to pick a time or pick a
date as ready-to-use dialogs
Android Date Picker
• Android Date Picker allows to select the date consisting of
day, month and year in your custom user interface.
• For this functionality android provides DatePicker.
Attributes of DatePicker
1. id: id is an attribute used to uniquely identify a date picker.
2. datePickerMode: This attribute is used to set the Date
Picker in mode either spinner or calendar.
3. background: background attribute is used to set the
background of a date picker.
4. padding: padding attribute is used to set the padding from
left, right, top or bottom for a date picker.
Methods of DatePicker
1.setSpinnersShown(boolean shown):used to set whether
the spinner of the date picker in shown or not.
2. GetDayOfMonth()
3. getMonth()
4. getYear()
• <DatePicker android:id="@+id/simpleDatePicker“
android:layout_width="wrap_content“
android:layout_height="wrap_content"
android:datePickerMode="spinner"/>