Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using CalendarPickerView inside AlertDialog 28 api #474

Open
EgorChe opened this issue Nov 28, 2018 · 4 comments
Open

Using CalendarPickerView inside AlertDialog 28 api #474

EgorChe opened this issue Nov 28, 2018 · 4 comments

Comments

@EgorChe
Copy link

EgorChe commented Nov 28, 2018

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"/>
@edenman
Copy link
Collaborator

edenman commented Nov 28, 2018

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

@EgorChe
Copy link
Author

EgorChe commented Nov 29, 2018

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.

@ashish2442
Copy link

ashish2442 commented Jan 16, 2019

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
Copy link

ashish2442 commented Jan 16, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants