Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add new button in date picker widget in android?

I want to add new button,which is clear button, to date picker widget and time picker widget in my android application. In default, these widgets have two button,set and cancel. How can I add one more button to these two button

Is it possible? If yes, can you give some example?

Thanks

like image 610
SavasCinar Avatar asked Jan 03 '12 16:01

SavasCinar


2 Answers

Simply create this class!

import android.app.DatePickerDialog;
import android.content.Context;

public class DatePickerWithNeutral extends DatePickerDialog {

    public DatePickerWithNeutral(Context context, OnDateSetListener callBack,
                            int year, int monthOfYear, int dayOfMonth) {
        super(context, 0, callBack, year, monthOfYear, dayOfMonth);

        setButton(BUTTON_POSITIVE, ("Ok"), this);
        setButton(BUTTON_NEUTRAL, ("Something"), this); // ADD THIS
        setButton(BUTTON_NEGATIVE, ("Cancel"), this);
    }
}

Then use this to add functionality to it!

date.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(
        new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(), "Neutral Button Clicked!", 
            Toast.LENGTH_LONG).show();
    }
});

Looks like this

enter image description here

Enjoy :)

like image 193
Michael Avatar answered Sep 18 '22 11:09

Michael


Just add a neutral button.

DatePickerDialog dialog = new DatePickerDialog(context, 0, callback, year, month, day);
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Name", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //Your code
    }
});
like image 25
undefined Avatar answered Sep 18 '22 11:09

undefined