Search code examples
androidandroid-layoutandroid-datepicker

Hide "selected date" part of DatePicker


I have a custom dialog created where I have a DatePicker in the top portion of the View and a NumberPicker with AM/PM radio buttons in the lower part. Works fine, but the DatePicker by default has this extra area at the top that shows the "selected date" as seen in below screen shot. How can I remove it so all I have in the datepicker area is the calendar itself? Is this possible? The part I want removed is in yellow.

enter image description here

datetime_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layoutDateTimePickerContainer"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <DatePicker
        android:id="@+id/dialogDatePicker"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="horizontal">

        <NumberPicker
            android:id="@+id/dialogTimePicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />    

        <RadioGroup
            android:id="@+id/radgrpDateTimePicker"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="20dp"
            android:checkedButton="@+id/radDateTimePickerAM"
            android:gravity="center_vertical">

            <RadioButton
                android:id="@+id/radDateTimePickerAM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="AM"/>

            <RadioButton
                android:id="@+id/radDateTimePickerPM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="PM"/>

        </RadioGroup>

    </LinearLayout>

    <TableRow
        android:id="@+id/trDateTimePickerButtonsContainer"
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:padding="10dp">

        <Button
            android:id="@+id/btnDateTimePickerCancel"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:text="@string/general_cancel"
            android:textAllCaps="false"/>

        <Button
            android:id="@+id/btnDateTimePickerOkay"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/general_okay"
            android:textAllCaps="false"/>

    </TableRow>

</LinearLayout>

Solution

  • Figured it out. You have to get reference to the LinearLayout of the DatePicker itself and then .getChildAt(0) and hide it.

    Dialog dialog = new Dialog(context);
    dialog.setContentView(R.id.datetime_dialog);
    DatePicker datePicker = dialog.findViewById(R.id.dialogDatePicker);
    LinearLayout datePickerHeader = (LinearLayout) datePicker.getChildAt(0);
    datePickerHeader.setVisibility(View.GONE);