Skip to content Skip to sidebar Skip to footer

Setondatechangelistener For Calendarview Does Not Work

I am trying to have a fragment for CalendarView in my adroid app. If I change the date then the toast should get printed but it does not do that. I want the toast to be printed if

Solution 1:

You already inflated the view in onCreateView(). But you are are doing it twice. Change your onCreateView() by

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Viewv= inflater.inflate(R.layout.fragment_calendar, container, false);

    cal = (CalendarView) v.findViewById(R.id.calendarView);
    date = cal.getDate();

    cal.setOnDateChangeListener(newCalendarView.OnDateChangeListener() {
        @OverridepublicvoidonSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
            if(cal.getDate() != date) {
                date = cal.getDate();
                Toast.makeText(view.getContext(), "Year=" + year + " Month=" + month + " Day=" + dayOfMonth, Toast.LENGTH_LONG).show();
                //cal.setBackgroundColor(Color.RED);
            }
        }
    });

    // Inflate the layout for this fragmentreturn v;
}

Hope it helps !!

Post a Comment for "Setondatechangelistener For Calendarview Does Not Work"