DatePicker View Tutorial
|
|
The DatePicker view, enables users to select a particular date on the activity. You can use “android.widget.DatePicker” class to render a date picker component to select day, month and year in a pre-defined user interface.
You can create DatePicker View using <DatePicker> xml element like this -
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
DatePicker view has three main methods-
- getMonth()
- getDayOfMonth()
- getYear()
These methods are used to get month , day & year respectively. Please note that the getMonth() method returns 0 (zero) for January, 1 for February & so on. Hence, you need to add a one to the result of this method to get the month number.
Example of DatePicker
Project Name : DatePickerDemo
Package : com.sunil.dpdemo
Build Target : Android 2.0
main.xml
(File : res/layout/main.xml)
DatePickerDemoActivity.java
(File : DatePickerDemoActivity.java)
Here is the output of this example-
That's it.You can also download the source code of this tutorial-
Project Name : DatePickerDemo
Package : com.sunil.dpdemo
Build Target : Android 2.0
main.xml
(File : res/layout/main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Date" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Date" />
</LinearLayout>
DatePickerDemoActivity.java
(File : DatePickerDemoActivity.java)
package com.sunil.dpdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
public class DatePickerDemoActivity extends Activity {
DatePicker datepicker;
int year , month , day;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datepicker = (DatePicker) findViewById(R.id.datePicker);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Date : "
+ (datepicker.getMonth() + 1) +"/"+datepicker.getDayOfMonth()
+"/"+datepicker.getYear(), Toast.LENGTH_LONG).show();
}
});
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
public class DatePickerDemoActivity extends Activity {
DatePicker datepicker;
int year , month , day;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datepicker = (DatePicker) findViewById(R.id.datePicker);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Date : "
+ (datepicker.getMonth() + 1) +"/"+datepicker.getDayOfMonth()
+"/"+datepicker.getYear(), Toast.LENGTH_LONG).show();
}
});
}
}
Here is the output of this example-
That's it.You can also download the source code of this tutorial-











nice tutorial..
Good Tutorial
Post a Comment