0% found this document useful (0 votes)
6 views

4.11 DatePicker

The document provides an overview of the DatePicker and TimePicker controls in Android, detailing their attributes, methods, and examples of usage. The DatePicker allows users to select dates in either calendar or spinner mode, while the TimePicker enables time selection in AM/PM or 24-hour format. Both controls include various attributes for customization and methods for retrieving user-selected values.

Uploaded by

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

4.11 DatePicker

The document provides an overview of the DatePicker and TimePicker controls in Android, detailing their attributes, methods, and examples of usage. The DatePicker allows users to select dates in either calendar or spinner mode, while the TimePicker enables time selection in AM/PM or 24-hour format. Both controls include various attributes for customization and methods for retrieving user-selected values.

Uploaded by

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

DatePicker-

In android, DatePicker is a control that will allow users to select the date by a day, month and year in our
application user interface.
Generally, in android DatePicker available in two modes, one is to show the complete calendar and another one is
to show the dates in spinner view.
Example-
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
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. Default mode is calendar
Example-
<DatePicker
android:id="@+id/simpleDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner" /> <!-- spinner mode of a date picker -->

To get only spinner mode date selection, then we need to


set android:calendarViewShown="false" attribute in DatePicker control like as shown below.

<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:calendarViewShown="false"/>
3. background: background attribute is used to set the background of a date picker. We can set a color or a drawable image in
the background

4. padding: padding attribute is used to set the padding from left, right, top or bottom for a date picker.
paddingRight: set the padding from the right side of the date picker.
paddingLeft: set the padding from the left side of the date picker.
paddingTop: set the padding from the top side of the date picker.
paddingBottom: set the padding from the bottom side of the date picker.
Padding: set the padding from the all side’s of the date picker.
Methods of DatePicker
1. setSpinnersShown(boolean shown):
This method is used to set whether the spinner of the date picker in shown or not. In this method you have to set a
Boolean value either true or false. True indicates spinner is shown, false value indicates spinner is not shown. Default value
for this function is true
Example-
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.simpleDatePicker); // initiate a date picker
simpleDatePicker.setSpinnersShown(false); // set false value for the spinner shown function
2. getDayOfMonth():
This method is used to get the selected day of the month from a date picker. This method returns an integer value.
Example-
/*Add in Oncreate() funtion after setContentView()*/
DatePicker simpleDatePicker = (DatePicker) findViewById(R.id.simpleDatePicker); // initiate a date picker
int day = simpleDatePicker.getDayOfMonth(); // get the selected day of the month

3. getMonth():
This method is used to get the selected month from a date picker. This method returns an integer value.
Example-
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.simpleDatePicker); // initiate a date picker
int month = simpleDatePicker.getMonth(); // get the selected month
4. getYear():
This method is used to get the selected year from a date picker. This method returns an integer value.
Example-
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.simpleDatePicker); // initiate a date picker
int year = simpleDatePicker.getYear(); // get the selected year

5. getFirstDayOfWeek():
This method is used to get the first day of the week. This method returns an integer value.
Example-
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.simpleDatePicker); // initiate a date picker
int firstDay=simpleDatePicker.getFirstDayOfWeek(); // get the first day of the week
Example-
Example-
TimePicker-
In Android, 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. If we need to show this view as a Dialog then we have
to use a TimePickerDialog class.
Example-
<TimePicker
android:id="@+id/simpleTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
Attributes of TimePicker:
1. id: id is an attribute used to uniquely identify a time picker.
2. timePickerMode: time picker mode is an attribute of time picker used to set the mode either spinner or clock. Default
mode is clock but this mode is no longer used after api level 21, so from api level 21 you have to set the mode to spinner.
Example-
<TimePicker
android:id="@+id/simpleTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner" /> <!-- time picker mode of a time picker -->
3. background: background attribute is used to set the background of a time picker. We can set a color or a drawable image
in the background. We can also set the background color programmatically means in java class.
Example-
android:background="#F88F00" />
In java- simpleTimePicker.setBackgroundColor(Color.YELLOW);

4. padding: padding attribute is used to set the padding from left, right, top or bottom for a time picker.
paddingRight: set the padding from the right side of the time picker.
paddingLeft: set the padding from the left side of the time picker.
paddingTop: set the padding from the top side of the time picker.
paddingBottom: set the padding from the bottom side of the time picker.
Padding: set the padding from the all side’s of the time picker.
Example- android:padding="20dp"/>
Methods of TimePicker:
1. setCurrentHour(Integer currentHour):
This method is used to set the current hours in a time picker.
Example-
TimePicker simpleTimePicker=(TimePicker)findViewById(R.id.simpleTimePicker); // initiate a time picker
// set the value for current hours
simpleTimePicker.setCurrentHour(5);

2. setCurrentMinute(Integer currentMinute):
This method is used to set the current minutes in a time picker.
Example-
TimePicker simpleTimePicker=(TimePicker)findViewById(R.id.simpleTimePicker); // initiate a time picker
// set the value for current hours
simpleTimePicker.setCurrentMinute(35);
3. getCurrentHour():
This method is used to get the current hours from a time picker.
Example-
TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.simpleTimePicker); // initiate a time
pickerint hours =simpleTimePicker.getCurrentHour(); // before api level 23
int hours =simpleTimePicker.getHour();

4. getCurrentMinute():
This method is used to get the current minutes from a time picker.
Example-
TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.simpleTimePicker); // initiate a time picker
int minutes = simpleTimePicker.getCurrentMinute(); // before api level 23
int minutes = simpleTimePicker.getMinute(); // after api level 23
5. setIs24HourView(Boolean is24HourView):
This method is used to set the mode of the Time picker either 24 hour mode or AM/PM mode. In this method we
set a Boolean value either true or false. True value indicate 24 hour mode and false value indicate AM/PM mode.
Example-
TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.simpleTimePicker); // initiate a time picker
simpleTimePicker.setIs24HourView(true); // set 24 hours mode for the time picker
6. is24HourView():
This method is used to check the current mode of the time picker. This method returns true if its 24 hour mode or
false if AM/PM mode is set.
Example-
TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.simpleTimePicker); // initiate a time picker
Boolean mode=simpleTimePicker.is24HourView(); // check the current mode of the time picker
7. setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener):
This method is used to set the callback that indicates the time has been adjusted by the user.
onTimeChanged(TimePicker view, int hourOfDay, int minute) is an override function of this listener in which we have three
parameters first is for TimePicker, second for getting hour of the day and last is for getting the minutes after changing the
time of the time picker.
Example-
TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.simpleTimePicker); // initiate a time picker
simpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { } });
Example-

You might also like