android-times-square icon indicating copy to clipboard operation
android-times-square copied to clipboard

Using CalendarPickerView inside AlertDialog 28 api

Open EgorChe opened this issue 7 years ago • 4 comments

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"/>

EgorChe avatar Nov 28 '18 11:11 EgorChe

I don't see you calling scrollToPosition. What about it "doesn't work"?

edenman avatar Nov 28 '18 14:11 edenman

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.

EgorChe avatar Nov 29 '18 05:11 EgorChe

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);
    }

ashish2442 avatar Jan 16 '19 03:01 ashish2442

@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.

ashish2442 avatar Jan 16 '19 06:01 ashish2442