Creating a DatePicker in Android
Android gives controls for the user to pick a date as ready-to-use dialogs.
Each date picker gives controls for selecting each part of the date(day, month, year).
Using this picker helps ensure that your users can pick a date is valid, formatted correctly, and adjusted to the user's locale.
We can get DatePickerDialog through DialogFragment and DatePickerDialog.OnDateSetListener.
Create Android Project with name as "DatePickerDemo" with package name "com.com.datepickerdemo". Create Activity with name as "DatePicker" and it will fetch date. This is the code for DatePicker.
Paste below code to "activity_date_picker.xml"
Paste below code to "AndroidManifest.xml"
You can download source code here.
Each date picker gives controls for selecting each part of the date(day, month, year).
Using this picker helps ensure that your users can pick a date is valid, formatted correctly, and adjusted to the user's locale.
We can get DatePickerDialog through DialogFragment and DatePickerDialog.OnDateSetListener.
Create Android Project with name as "DatePickerDemo" with package name "com.com.datepickerdemo". Create Activity with name as "DatePicker" and it will fetch date. This is the code for DatePicker.
package com.com.datepickerdemo;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DatePicker extends FragmentActivity {
Button show;
static TextView date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
show = (Button) findViewById(R.id.ids);
date = (TextView) findViewById(R.id.date);
}
public void showDate(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(this.getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int month = dayOfMonth + 1;
date.setText("Selected date is " + month + "/" + monthOfYear + "/"
+ year);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.date_picker, menu);
return true;
}
}
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DatePicker extends FragmentActivity {
Button show;
static TextView date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
show = (Button) findViewById(R.id.ids);
date = (TextView) findViewById(R.id.date);
}
public void showDate(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(this.getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int month = dayOfMonth + 1;
date.setText("Selected date is " + month + "/" + monthOfYear + "/"
+ year);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.date_picker, menu);
return true;
}
}
Paste below code to "activity_date_picker.xml"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DatePicker" >
<Button
android:id="@+id/ids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:onClick="showDate"
android:text="show date" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ids"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="Please select date" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DatePicker" >
<Button
android:id="@+id/ids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:onClick="showDate"
android:text="show date" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ids"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="Please select date" />
</RelativeLayout>
Paste below code to "AndroidManifest.xml"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.com.datepickerdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.com.datepickerdemo.DatePicker"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.com.datepickerdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.com.datepickerdemo.DatePicker"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can download source code here.
Comments
Post a Comment