Create a 2-in-1 Date-Time-Picker using SublimePicker

This is a 2-in-1 Date-Time-Picker in Android built using SublimePicker.

Add the dependency to your app level build.gradle file

implementation 'com.appeaser.sublimepickerlibrary:sublimepickerlibrary:2.1.2'

Create a class called SublimePickerDialogFragment.

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.admads.transitguide.R;
import com.appeaser.sublimepickerlibrary.SublimePicker;
import com.appeaser.sublimepickerlibrary.datepicker.SelectedDate;
import com.appeaser.sublimepickerlibrary.helpers.SublimeListenerAdapter;
import com.appeaser.sublimepickerlibrary.helpers.SublimeOptions;
import com.appeaser.sublimepickerlibrary.recurrencepicker.SublimeRecurrencePicker;

import java.text.DateFormat;
import java.util.Locale;
import java.util.TimeZone;

public class SublimePickerDialogFragment extends DialogFragment {
    DateFormat mDateFormatter, mTimeFormatter;
    SublimePicker mSublimePicker;
    Callback mCallback;

    SublimeListenerAdapter mListener = new SublimeListenerAdapter() {
        @Override
        public void onCancelled() {
            if (mCallback!= null) {
                mCallback.onCancelled();
            }
            dismiss();
        }

        @Override
        public void onDateTimeRecurrenceSet(SublimePicker sublimeMaterialPicker,
                                            SelectedDate selectedDate,
                                            int hourOfDay, int minute,
                                            SublimeRecurrencePicker.RecurrenceOption recurrenceOption,
                                            String recurrenceRule) {
            if (mCallback != null) {
                mCallback.onDateTimeRecurrenceSet(selectedDate,
                        hourOfDay, minute, recurrenceOption, recurrenceRule);
            }

            dismiss();
        }
    };

    public SublimePickerDialogFragment() {
        mDateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
        mTimeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
        mTimeFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));


    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mSublimePicker = (SublimePicker) getActivity().getLayoutInflater().inflate(R.layout.sublime_picker, container);
        Bundle arguments = getArguments();
        SublimeOptions options = null;
        if (arguments != null) {
            options = arguments.getParcelable("SUBLIME_OPTIONS");
        }

        mSublimePicker.initializePicker(options, mListener);
        return mSublimePicker;
    }

    public interface Callback {
        void onCancelled();

        void onDateTimeRecurrenceSet(SelectedDate selectedDate,
                                     int hourOfDay, int minute,
                                     SublimeRecurrencePicker.RecurrenceOption recurrenceOption,
                                     String recurrenceRule);
    }
}

In your activity/fragment, write this function to show the picker and handle the selection

public void sublimePicker() {
    SublimePickerDialogFragment.Callback mFragmentCallback = new SublimePickerDialogFragment.Callback() {
        @Override
        public void onCancelled() {
        }
        @Override
        public void onDateTimeRecurrenceSet(SelectedDate mSelectedDate,
                                            int hourOfDay, int minute,
                                            SublimeRecurrencePicker.RecurrenceOption recurrenceOption,
                                            String recurrenceRule) {

            if (mSelectedDate != null) {

                //sample handling. add your own handling here.
                int year = mSelectedDate.getEndDate().get(Calendar.YEAR);
                int month = mSelectedDate.getEndDate().get(Calendar.MONTH);
                int day = mSelectedDate.getEndDate().get(Calendar.DATE);
                GregorianCalendar date = new GregorianCalendar(year, month, day, hourOfDay, minute, 0);
                long epoch = date.getTimeInMillis();
                String datestr = Helper.epoch2DateString(epoch,"dd MMM, hh:mm a");
                txttime.setText(datestr);
            }
        }
    };
    SublimePickerDialogFragment pickerFrag = new SublimePickerDialogFragment();
    pickerFrag.setCallback(mFragmentCallback);

    SublimeOptions opts = new SublimeOptions().setCanPickDateRange(false).setDisplayOptions(SublimeOptions.ACTIVATE_DATE_PICKER | SublimeOptions.ACTIVATE_TIME_PICKER).setPickerToShow(SublimeOptions.Picker.TIME_PICKER);
    Bundle bundle = new Bundle();
    bundle.putParcelable("SUBLIME_OPTIONS", opts);
    pickerFrag.setArguments(bundle);
    pickerFrag.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    pickerFrag.show(getActivity().getSupportFragmentManager(), "SUBLIME_PICKER");
}

 


Also published on Medium.

By |2019-04-04T16:31:57+00:00March 14th, 2019|Categories: Android Tips|0 Comments

Leave A Comment