android-times-square
android-times-square copied to clipboard
Using CalendarPickerView inside AlertDialog 28 api
Hi! If CalendarPickerView add inside AlertDialog, then In 28 api doesn't work scrollToPosition.
DateRangePickerDialog.java
public class DateRangePickerDialog extends AlertDialog {
public DateRangePickerDialog(@NonNull Context context) {
super(context);
View content = LayoutInflater.from(getContext()).inflate(R.layout.calendar_picker_layout, null);
CalendarPickerView calendarPickerView = content.findViewById(R.id.calendar_view);
Calendar nextYear = Calendar.getInstance();
nextYear.set(2020, 12, 12);
Calendar lastYear = Calendar.getInstance();
lastYear.set(2017, 6, 12);
Date today = new Date();
calendarPickerView.init(lastYear.getTime(), nextYear.getTime())
.withSelectedDate(today);
setView(content);
setButton(DialogInterface.BUTTON_POSITIVE, "OK", ((dialog, which) -> dialog.dismiss()));
setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", (dialog, which) -> dialog.dismiss());
setOnCancelListener(DialogInterface::dismiss);
}
}
calendar_picker_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<com.squareup.timessquare.CalendarPickerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/calendar_view"
android:layout_width="300dp"
android:layout_height="350dp"
android:layout_gravity="center_horizontal"
android:background="@android:color/white"
android:clipToPadding="false"/>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val alertDialog: AlertDialog = DateRangePickerDialog(this)
alertDialog.show()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
I don't see you calling scrollToPosition. What about it "doesn't work"?
I don't see you calling scrollToPosition
I mean setSelection from the ListView class.
What about it "doesn't work"?
If you execute the code, then the focus in the calendar will be on minDate, and not on the selected date.
I see similar problem too. scrollToDate doesn't work. Happens only on Api 28.
CalendarPickerView.FluentInitializer initializer;
if (timeZone != null) {
initializer = calendarPickerView.init(earliestSelectableDate, latestSelectableDate, timeZone);
} else {
initializer = calendarPickerView.init(earliestSelectableDate, latestSelectableDate);
}
initializer.inMode(selectionMode);
//assume date is class property which is available always.
if (date != null) {
calendarPickerView.selectDate(date);
}
@EgorChe Seems like setSelection() from list view has some bug and so not scrolling to selected item ( on devices with API 28). How ever smoothScrollToPosition() is working.
So as a work around try calling below and it should work.
calendarPickerView.selectDate(date, true);
But I agree with you @EgorChe this has to be fixed properly.