Create DatePickerDialog in android example with DialogFragment

How to design custom datepickerdialog in android application to get date from user.

Date picker dialog is used to get date form user into android application. DatePickerDialog is a custom pop up date selection box . So here is the complete step by step tutorial for Create DatePickerDialog in android example with DialogFragment.

android-project-download-code-button

How to Create DatePickerDialog in android example with DialogFragment.

Code for MainActivity.java file.

 package com.datepickerdialog_android_examples.com;

import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;


public class MainActivity extends Activity {

 Button datepickerdialogbutton;
 TextView selecteddate;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 datepickerdialogbutton = (Button)findViewById(R.id.button1);
 selecteddate = (TextView)findViewById(R.id.textView1);
 
 datepickerdialogbutton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 DialogFragment dialogfragment = new DatePickerDialogClass();

 dialogfragment.show(getFragmentManager(), "Date Picker Dialog");
 
 }
 });
 }

 public static class DatePickerDialogClass extends DialogFragment implements DatePickerDialog.OnDateSetListener{

 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState){
 final Calendar calendar = Calendar.getInstance();
 int year = calendar.get(Calendar.YEAR);
 int month = calendar.get(Calendar.MONTH);
 int day = calendar.get(Calendar.DAY_OF_MONTH);
 
 DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
 AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,year,month,day);
 
 return datepickerdialog;
 }

 public void onDateSet(DatePicker view, int year, int month, int day){
 
 TextView textview = (TextView)getActivity().findViewById(R.id.textView1);
 
 textview.setText(day + ":" + (month+1) + ":" + year);
 
 }
 }
 
}

Code for activity_main.xml layout file.

<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="com.datepickerdialog_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click Here to open DatePickerDialog" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="25dp"
 android:text="Selected Date"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots:

select-date

Create DatePickerDialog in android example with DialogFragment

show-date

Click here to download Create DatePickerDialog in android example with DialogFragment project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *