Skip to content

Instantly share code, notes, and snippets.

@parthdave93
Created May 11, 2017 04:59
Show Gist options
  • Save parthdave93/03b1254379dcbf5628f7af7a04035395 to your computer and use it in GitHub Desktop.
Save parthdave93/03b1254379dcbf5628f7af7a04035395 to your computer and use it in GitHub Desktop.
Date Picker Using Databinding
@BindingAdapter(value = {"bind:datePick", "bind:maxDate", "bind:minDate"}, requireAll = false)
public static void bindTextViewDateClicks(final TextView textView, final ObservableField<Date> date, final ObservableField<Date> maxDate, final ObservableField<Date> minDate) {
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectDate(textView.getContext(), date, maxDate, minDate);
}
});
}
@BindingAdapter(value = {"bind:textDate", "bind:nullText"}, requireAll = true)
public static void bindTextViewDate(final TextView textView, final Date date, String nullText) {
if (date != null) {
textView.setText("" + dateFormat.format(date));
} else if (nullText != null) {
textView.setText(nullText);
}
}
public static void selectDate(Context context, final ObservableField<Date> date, final ObservableField<Date> maxDate, final ObservableField<Date> minDate) {
final Calendar calBefore = Calendar.getInstance();
if (date != null && date.get() != null)
calBefore.setTime(date.get());
DatePickerDialog dialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker dpView, int year, int monthOfYear, int dayOfMonth) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
date.set(cal.getTime());
date.notifyChange();
}
}, calBefore.get(Calendar.YEAR), calBefore.get(Calendar.MONTH), calBefore.get(Calendar.DAY_OF_MONTH));
if (minDate != null && minDate.get()!=null)
dialog.getDatePicker().setMinDate(minDate.get().getTime());
if (maxDate != null && maxDate.get()!=null)
dialog.getDatePicker().setMaxDate(maxDate.get().getTime());
dialog.show();
}
<com.wozzon.ui.customview.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColor="@color/text_grey"
bind:datePick="@{model.startTime}"
bind:maxDate="@{model.expireTime}"
bind:nullText="@{@string/startTime}"
bind:textDate="@{model.startTime}"/>
<com.wozzon.ui.customview.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColor="@color/text_grey"
bind:datePick="@{model.expireTime}"
bind:minDate="@{model.startTime}"
bind:nullText="@{@string/expireTime}"
bind:textDate="@{model.expireTime}"/>
@TahaChetehouna
Copy link

Good

@parthdave93
Copy link
Author

Thanks (y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment