wdullaer / MaterialDateTimePicker

Pick a date or time on Android in style
Apache License 2.0
4.67k stars 950 forks source link

Clear button. #183

Open anepsha opened 8 years ago

anepsha commented 8 years ago

Hi,

Is it possible to add a third button (clear) to date picker dialog?

Thank you

wdullaer commented 8 years ago

What would you like to be cleared exactly? There is always a selection in the datepicker.

If you need to clear an alarm in your app, try adding it to your app UI with a seperate button, or a swipe to dismiss action

anepsha commented 8 years ago

Hi, thank you for your reply. I have a clickable text field, when user clicks on it date picker dialog shows up. Then user selects a date and date value sets to that textview. The problem is that I don't have a lot of space in that textview and clear button looks terrible there. Is there a way to add an extra button to datepicker dialog such as "Clear"?

wdullaer commented 8 years ago

Check PR #156 I currently don't really plan to add this functionality, the goal is to stick to the guidelines, but I can always be convinced by compelling arguments.

denversc commented 8 years ago

FWIW, here is a way that you can add a "Clear" button (or any other button for that matter) into DatePickerDialog using version 2.2.0 of MaterialDateTimePicker.

ClearableDatePickerDialog.java:

public class ClearableDatePickerDialog extends DatePickerDialog {

    private OnDateClearedListener mOnDateClearedListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
        View view = super.onCreateView(inflater, container, state);
        LinearLayout buttonContainer = (LinearLayout) view.findViewById(
            com.wdullaer.materialdatetimepicker.R.id.done_background);
        View clearButton = inflater.inflate(R.layout.date_picker_dialog_clear_button,
            buttonContainer, false);
        clearButton.setOnClickListener(new ClearClickListener());
        buttonContainer.addView(clearButton, 0);

        return view;
    }

    public void setOnDateClearedListener(OnDateClearedListener listener) {
        mOnDateClearedListener = listener;
    }

    public OnDateClearedListener getOnDateClearedListener() {
        return mOnDateClearedListener;
    }

    public interface OnDateClearedListener {

        /**
         * @param view The view associated with this listener.
         */
        void onDateCleared(ClearableDatePickerDialog view);

    }

    private class ClearClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            tryVibrate();

            OnDateClearedListener listener = getOnDateClearedListener();
            if (listener != null) {
                listener.onDateCleared(ClearableDatePickerDialog.this);
            }

            dismiss();
        }

    }

}

date_picker_dialog_clear_button.xml:

<?xml version="1.0" encoding="utf-8"?>

<!--
This button is based on
https://github.com/wdullaer/MaterialDateTimePicker/blob/master/library/src/main/res/layout/mdtp_done_button.xml
-->

<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/clear"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    style="@style/mdtp_ActionButton.Text"
    android:text="@string/clear"
    />

Screenshot of DatePickerDialog with "Clear" button: datepickerwithclearbutton

wdullaer commented 8 years ago

Great stuff. I'll turn that in a gist and add it to the FAQ of the project.

It shows how you can extend this without me having to support a thousand different rarely used options :-)

androidneha commented 8 years ago

Is there any possiblity to remove all button..... just by clicking on date...Date will be set to your textview. please send me the code 4 that . Thanx in Advance....!!!!

wdullaer commented 8 years ago

The pickers extend DialogFragment, so you can create your own Dialog and use them as the content for that. When not creating their own Dialog, the pickers do not render the OK and Cancel buttons, so this should allow you to create a buttonless dialog.

Meggi07 commented 6 years ago

in datepickerdialog show me this type error java.lang.RuntimeException: Font asset not found fonts/Roboto-Medium.ttf sometime please give me solution how to fix that?

wdullaer commented 6 years ago

Can you please create a new issue, rather than commenting in totally unrelated ones? I will also need more info on this: at least a stacktrace, the library version and the api version of the device on which the error occurred.

Meggi07 commented 6 years ago

How to create new issue?