androidandroid-timepickerdialog-preference

TimePickerDialog with Neutral button


I have a TimePickerDialog preference with a "TimePreference" class which extends DialogPreference:

public class TimePreference extends DialogPreference{
...
private TimePicker picker=null;

...
public TimePreference(Context ctxt, AttributeSet attrs){
    super(ctxt, attrs);
    setPositiveButtonText(android.R.string.ok);
    setNegativeButtonText(android.R.string.cancel);
}

@Override
protected View onCreateDialogView(){
    picker=new TimePicker(getContext());
    return(picker);
}
...
}

I would like to add a third button, which will set the preference to the current time and close the dialog, like this:

enter image description here

The question is, is there some way of doing this using TimePickers or should I make a custom AlertDialog?


Solution

  • Thanks for your answers! I will show you how I finally managed it, just in case someone wants to do the same.

    First, I have a layout file good_timedialog.xml which looks like I wanted: TimePicker with Positive, neutral and negative buttons

    Then I have a TimePreference class:

    public class TimePreference extends DialogPreference{
        private final static String lTag="CustomTimeDialog";
        private TimePicker picker;
        protected TimeObj lastTime;
    
    public TimePreference(Context ctxt,AttributeSet atrs) {
        super(ctxt,atrs);
        setDialogLayoutResource(R.layout.good_timedialog); // this loads the layout
        setPositiveButtonText("");
        setNegativeButtonText(""); // hide default buttons
        setDialogTitle(ctxt.getString(R.string.selectTimeTimePrefTitle));
    }
    
    @Override
    protected void onBindDialogView(View v){
        super.onBindDialogView(v);
    
        v.findViewById(R.id.butCancel).setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Log.d(lTag,"cancel clicked");
                getDialog().dismiss();
            }
        });
        v.findViewById(R.id.butNow).setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Log.d(lTag,"now clicked");
                saveToSP(true);
                getDialog().dismiss();
            }
        });
        v.findViewById(R.id.butOK).setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Log.d(lTag,"ok clicked");
                saveToSP(false);
                getDialog().dismiss();
            }
        });
    
        picker = (TimePicker) v.findViewById(R.id.timePicker);
        if (picker!=null) {
            picker.setCurrentHour(lastTime.hr);
            picker.setCurrentMinute(lastTime.min);
        } else { // if it is null
            Log.w(lTag,"var picker is null!");
        }
    
    }
    
    void saveToSP(boolean now){
        if (now) lastTime = new TimeObj(); // make it the current time
        else {
            lastTime.hr = picker.getCurrentHour();
            lastTime.min = picker.getCurrentMinute();
        }
        setSummary(lastTime.giveString()); // update Summary like hh:mm 16:28
    
        int time = lastTime.giveMinutes(); // 10:30am will be 630 minutes
    
        // save to sharedprefs
        if(callChangeListener(time)){ 
            persistInt(time);
            if(getEditor().commit()) Log.d(lTag,"Successfully saved changes");
            else Log.w(lTag,"Did NOT save changes");
    
        }
    }
    //... more ...
    }
    

    And at the perferences.xml file, I can use <com.example.TimePreference android:key="test"/>