Android DatePicker
Android DatePicker
1
Android DatePicker is an interface component that allows
the user to select a date and ensure that the user input data
are valid.
DatePicker has two modes with different interface.
android:datePickerMode="calendar" (Default)
android:datePickerMode="spinner"
2
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="calendar" />
3
• Portrait Screen
4
• Landscape Screen
5
android:datePickerMode="spinner"
In spinner mode, DatePicker consists of two parts,
•The left side is a block of three Spinners that permits
the user to adjust the month, day and year.
•The other is the simpler CalendarView than it is
in calendar mode.
6
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:datePickerMode="spinner" />
7
DatePicker [spinner mode] default.
8
• To hide the right-hand side Calendar View component.
android:calendarViewShown="false"
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner" />
9
• android:calendarViewShown="false"
10
android:spinnersShown="false" to hide the block (containing three Spinners)
on your left.
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:datePickerMode="spinner"
android:spinnersShown="false" />
11
• android:spinnersShown="false"
12
Android DatePicker Example
•one DatePicker control,
•one TextView control and
•one Button control in RelativeLayout to show the selected date
on Button click in the android application.
13
Create a new android application using android studio and
give names as DatePickerExample.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
14
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/datePicker1"
android:layout_marginLeft="100dp"
android:text="Get Date" />
15
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="100dp"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:textSize="18dp"/>
</RelativeLayout>
16
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatePicker picker;
Button btn;
TextView t1;
17
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.textView1);
picker=(DatePicker)findViewById(R.id.datePicker1);
btn=(Button)findViewById(R.id.button1);
18
btn.setOnClickListener(new View.OnClickListener()
{
@Override
});
}
}
19