0% found this document useful (0 votes)
4 views14 pages

Checkboxradio

The document provides an overview of various UI controls in Android, including CheckBox, RadioButton, ImageView, DatePicker, and TimePicker. Each control is described with its implementation details, methods, and attributes necessary for usage in Android applications. It includes code snippets for creating and managing these controls programmatically.

Uploaded by

aji m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Checkboxradio

The document provides an overview of various UI controls in Android, including CheckBox, RadioButton, ImageView, DatePicker, and TimePicker. Each control is described with its implementation details, methods, and attributes necessary for usage in Android applications. It includes code snippets for creating and managing these controls programmatically.

Uploaded by

aji m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

The CheckBox Control

• 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"/>

<RadioButton android:id="@+id/fishRBtn" android:text="Fish" android:checked="true"


android:layout_width="wrap_content" android:layout_height="wrap_content"/>

<RadioButton android:id="@+id/stkRBtn" android:text="Steak"


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"/>

• simpleDatePicker = (DatePicker) findViewById(R.id.simpleDatePicker);


• String day = "Day = " + simpleDatePicker.getDayOfMonth();
String month = "Month = " + (simpleDatePicker.getMonth() + 1);
String year = "Year = " + simpleDatePicker.getYear();
TimePicker
• TimePicker is a widget used for selecting the time of the day in
either AM/PM mode or 24 hours mode.
• The displayed time consist of hours, minutes and clock format.
• Implement using android.widget.TimePicker
• Atttribute:timePickerMode: spinner or clock
• Methods of TimePicker:
• setHour(Integer hour): used to set the current hours in a time picker.
• setMinute(Integer minute):used to set the current minutes in a time
picker.
• setIs24HourView(Boolean is24HourView):
• is24HourView():
• setOnTimeChangedListener(TimePicker.OnTimeChangedListener):
to set the callback that indicates the time has been adjusted by the user
<TimePicker android:id="@+id/simpleTimePicker"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_marginTop="50dp"
android:background="#090" android:padding="20dp"
android:timePickerMode="spinner" />
-----------------------------------------------------------------------------------
simpleTimePicker = (TimePicker) findViewById(R.id.simpleTimePicker);
simpleTimePicker.setIs24HourView(false); // used to display AM/PM mode
// perform set on time changed listener event
simpleTimePicker.setOnTimeChangedListener(new
TimePicker.OnTimeChangedListener() { @Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{ // code
}});

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy