Tuesday, August 25, 2015

How to create a custom Title for DatePickerDialog in Android

MainActivity.java

package com.cfsuman.androidtutorials;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams;
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 dFragment = new DatePickerFragment();

            // Show the date picker dialog fragment
            dFragment.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);


            DatePickerDialog datePickerDialog = new DatePickerDialog(
                    getActivity(),0,this,year,month,day);


            // Create a TextView programmatically.
            TextView textView = new TextView(getActivity());


            // Create a TextView programmatically
            LayoutParams lp = new LayoutParams(
                    LayoutParams.WRAP_CONTENT, // Width of TextView
                    LayoutParams.WRAP_CONTENT  // Height of TextView
            );
            textView.setLayoutParams(lp);
            textView.setPadding(0, 48, 0, 48);
            textView.setGravity(Gravity.CENTER);
            textView.setTypeface(Typeface.SANS_SERIF,Typeface.ITALIC);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,24);
            textView.setText("This is a custom title.");
            textView.setTextColor(Color.WHITE);
            textView.setBackgroundColor(Color.parseColor("#5DA493"));


            // Set the newly created TextView as
            // a custom tile of DatePickerDialog
            datePickerDialog.setCustomTitle(textView);


            // Or you can simply set a tile for DatePickerDialog
            // Uncomment this line to activate it
            //datePickerDialog.setTitle("This is a simple title.");


            // Return the DatePickerDialog
            return  datePickerDialog;
        }

        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"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>