Reputation: 3
Hi I am passing month and year in datepicker dialog to show dates of that month is there any possible way to hide year and month from datepicker dialogThis is how it looks
This is what i need to show:
Upvotes: 0
Views: 1149
Reputation: 363647
You can use the MaterialDatePicker
provided by the Material Components Library.
Instead of hiding the month/year selector you can restrict the selection to a single month with a CalendarConstraints
.
long today = MaterialDatePicker.todayInUtcMilliseconds();
Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
utc.clear();
utc.setTimeInMillis(today);
utc.set(Calendar.DAY_OF_MONTH, 1);
long thisMonthFirst = utc.getTimeInMillis();
utc.set(Calendar.DATE, utc.getActualMaximum(Calendar.DATE));
long thisMonthLast = utc.getTimeInMillis();
MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
builder.setSelection(today);
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
constraintsBuilder.setOpenAt(today);
constraintsBuilder.setStart(thisMonthFirst);
constraintsBuilder.setEnd(thisMonthLast);
builder.setCalendarConstraints(constraintsBuilder.build());
MaterialDatePicker<Long> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());
In this way the selection is restricted to the current month and the user can't select another year/month.
You can change your logic (utc.setTimeInMillis(today)
) to use another month.
Upvotes: 1
Reputation: 7081
You want to use the CalendarView
widget. Note that the year and month will be shown but not in big font. You can put it in a DialogFragment with a few buttons to achieve the result you want:
public class CalendarViewDialog extends DialogFragment {
private static final String KEY_DATE = "date";
private long date = System.currentTimeMillis();
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Context context = requireContext();
Bundle args = getArguments();
if (args != null && args.containsKey(KEY_DATE)) {
date = args.getLong(KEY_DATE);
}
CalendarView calendarView = new CalendarView(context);
calendarView.setDate(date);
calendarView.setOnDateChangeListener((view, year, month, dayOfMonth) ->
date = new GregorianCalendar(year, month, dayOfMonth).getTimeInMillis());
return new AlertDialog.Builder(context)
.setView(calendarView)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
if (getParentFragment() instanceof Callback) {
((Callback) requireParentFragment()).onDateSelected(date);
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
public static CalendarViewDialog newInstance(long date) {
CalendarViewDialog dialog = new CalendarViewDialog();
Bundle args = new Bundle();
args.putLong(KEY_DATE, date);
dialog.setArguments(args);
return dialog;
}
public interface Callback {
void onDateSelected(long date);
}
}
Use it like this from another fragment:
public class MyFragment extends Fragment implements CalendarViewDialog.Callback {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
btn.setOnClickListener(v -> {
CalendarViewDialog.newInstance(System.currentTimeMillis())
.show(getChildFragmentManager(), "date-dialog");
});
}
@Override
public void onDateSelected(long date) {
// Do whatever you want with `date`.
}
}
Upvotes: 0