Tuesday, August 25, 2015

How to use theme in DatePickerDialog in Android

MainActivity.java

package com.cfsuman.androidtutorials;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.DatePicker;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.fragment.app.DialogFragment;
import java.util.Calendar;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        Button button = findViewById(R.id.button);


        // Show the date picker dialog
        button.setOnClickListener(v -> {
            // Initialize a new date picker dialog fragment
            DialogFragment dialogFragment = new DatePickerFragment();

            // Show the date picker dialog fragment
            dialogFragment.show(
                    getSupportFragmentManager(),
                    "Date Picker"
            );
        });
    }



    public static class DatePickerFragment extends DialogFragment
            implements DatePickerDialog.OnDateSetListener{

        @NonNull
        @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);


            // Create a ContextThemeWrapper instance
            ContextThemeWrapper contextThemeWrapper
                    = new ContextThemeWrapper(
                    getActivity(),
                    android.R.style.Theme_Holo_Light_Dialog
                    //android.R.style.Theme_Holo_Dialog
            );


            // Return the DatePickerDialog
            return new DatePickerDialog(
                    getActivity(),
                    contextThemeWrapper.getThemeResId(),
                    this,year,month,day
            );
        }


        public void onDateSet(
                DatePicker view, int year, int month, int day){
            // Do something with the chosen date
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show DatePickerDialog"
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>