Search code examples
androidandroid-calendarcalendarview

How to show only a specific month on Android CalendarView?


I want to show a specific month on CardView without NEXT and PREVIOUS arrow to navigate the calendar. If I want to show February 2010 user must see only February 2010. They can't go next or previous month.

I followed this Stack Overflow answer to show a specific month but it's not working for me. It shows all time the current date and month for my case. I'm not sure is it possible or not to disable NEXT-PREVIOUS navigation.

My XML:

<RelativeLayout 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="megaminds.dailyeditorialword.Fragments.CalendarViewFragment">

<CalendarView
    android:id="@+id/calendarViewT"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:firstDayOfWeek="7">

</CalendarView>

onCreateView of my fragment class:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_calendar_view, container, false);

    int year = 2010, month = 2, day = 1;

    CalendarView calendarView = (CalendarView) view.findViewById(R.id.calendarViewT);
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day); //want to show full month of February 2010

    calendarView.setMinDate(calendar.getTimeInMillis());

    return view;
}

How can I show only February month without next-previous navigation?


Solution

  • Calendar febFirst=Calendar.getInstance();
    
    febFirst.set(2017, 1, 1, 0, 0, 0);
    
    calendar.setMinDate(febFirst.getTimeInMillis());
    
    Calendar febLast=Calendar.getInstance();
    
    febLast.set(2017, 2, 0, 0, 0, 0);
    
    calendar.setMaxDate(febLast.getTimeInMillis());
    calendar.setDate(febFirst.getTimeInMillis());
    

    where calendar is the CalendarView.

    CalendarView, locked to February 2017, on Nexus 5 running Android 6