Change DatePickerDialog theme in android using DialogFragment

How to create DatePickerDialog with different styles DEVICE_DEFAULT_DARK, LIGHT, HOLO DARK, HOLO LIGHT, TRADITIONAL.

In this tutorial we are creating DatePickerDialog  with 5 different themes on button click. We have calling declaring DialogFragment object using class. So here is the complete step by step tutorial for Change DatePickerDialog theme in android using DialogFragment.

android-project-download-code-button

How to Change DatePickerDialog theme in android using DialogFragment.

Code for MainActivity.java file.

package com.datepickerdialogtheme_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 button1,button2,button3,button4,button5;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button1 = (Button)findViewById(R.id.button1);
 button2 = (Button)findViewById(R.id.button2);
 button3 = (Button)findViewById(R.id.button3);
 button4 = (Button)findViewById(R.id.button4);
 button5 = (Button)findViewById(R.id.button5);
 
 button1.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 DialogFragment dialogfragment = new DatePickerDialogTheme1();

 dialogfragment.show(getFragmentManager(), "Theme 1");
 
 }

 });
 
 button2.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 DialogFragment dialogfragment = new DatePickerDialogTheme2();

 dialogfragment.show(getFragmentManager(), "Theme 2");
 
 }
 });
 
 button3.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 
 DialogFragment dialogfragment = new DatePickerDialogTheme3();

 dialogfragment.show(getFragmentManager(), "Theme 3");
 
 
 }
 });
 
 button4.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 
 DialogFragment dialogfragment = new DatePickerDialogTheme4();

 dialogfragment.show(getFragmentManager(), "Theme 4");
 
 }
 });
 
 button5.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 DialogFragment dialogfragment = new DatePickerDialogTheme5();

 dialogfragment.show(getFragmentManager(), "Theme 5");
 
 }
 });
 
 }

 public static class DatePickerDialogTheme1 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);
 
 }
 }
 
 public static class DatePickerDialogTheme2 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_LIGHT,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);
 
 }
 }
 
 public static class DatePickerDialogTheme3 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_HOLO_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);
 
 }
 }
 
 public static class DatePickerDialogTheme4 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_HOLO_LIGHT,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);
 
 }
 }
 
 public static class DatePickerDialogTheme5 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_TRADITIONAL,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.datepickerdialogtheme_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="32dp"
 android:text="Selected Date Disply Here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:text="DatePickerDialog theme 1" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:text="DatePickerDialog theme 2" />

 <Button
 android:id="@+id/button3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button2"
 android:layout_centerHorizontal="true"
 android:text="DatePickerDialog theme 3" />

 <Button
 android:id="@+id/button4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button3"
 android:layout_centerHorizontal="true"
 android:text="DatePickerDialog theme 4" />

 <Button
 android:id="@+id/button5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button4"
 android:layout_centerHorizontal="true"
 android:text="DatePickerDialog theme 5" />

</RelativeLayout>

Screenshots:

Click here to download Change DatePickerDialog theme in android using DialogFragment project with source code.