recruit-mp / LightCalendarView

A lightweight monthly calendar view for Android, fully written in Kotlin. Designed to meet the minimum demands for typical calendars.
Apache License 2.0
445 stars 67 forks source link

SetSelectedDate Does not work #8

Closed AriMeidan closed 7 years ago

AriMeidan commented 7 years ago

this.getMonthViewForPosition(this.getPositionForDate(date)); returns null although this.getPositionForDate(date) is valid.

recruit-mahayash commented 7 years ago

Hi @AriMeidan,

Thank you for the report.

Are you calling LightCalendarView#getMonthViewForPosition() in an activity's onCreate() or a fragment's onCreateView() method? If so, move the code to somewhere like onResume() or onViewCreated() where the view creation and child view inflations have completed.

You can also use View#addOnLayoutChangeListener to run your code right after the inflation completes as follows:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final LightCalendarView calendarView = (LightCalendarView) findViewById(R.id.calendar_view);
        calendarView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                int positionForDate = calendarView.getPositionForDate(new Date());
                MonthView monthView = calendarView.getMonthViewForPosition(positionForDate);
                Log.d("MainActivity", "positionForDate = " + positionForDate + ", monthView = " + monthView);

                calendarView.removeOnLayoutChangeListener(this);
            }
        });
    }

Hope this helps.

recruit-mahayash commented 7 years ago

We are closing this issue as there are no further comments coming. Feel free to reopen this issue.

Thanks!