Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear button. #183

Open
anepsha opened this issue Mar 15, 2016 · 10 comments
Open

Clear button. #183

anepsha opened this issue Mar 15, 2016 · 10 comments

Comments

@anepsha
Copy link

anepsha commented Mar 15, 2016

Hi,

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

Thank you

@wdullaer
Copy link
Owner

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
Copy link
Author

anepsha commented Mar 16, 2016

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
Copy link
Owner

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
Copy link

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
Copy link
Owner

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
Copy link

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
Copy link
Owner

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
Copy link

Meggi07 commented Nov 2, 2017

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
Copy link
Owner

wdullaer commented Nov 2, 2017

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
Copy link

Meggi07 commented Nov 3, 2017

How to create new issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants