X
    Categories: Beginner

Android CalendarView Tutorial in Android Studio

Android CalendarView :

Android CalendarView is a widget used for selecting  dates from the calendar, this View is much more comfortable than the previous date picker in my point of view you can use accordingly.

Go through the tutorial on android date picker here

Calendar has got a good interface which is easier to understand because the basic calendar is available to view in android calendar view so that user can easily get selected date from android calendar view day view.

Also week number is displayed so that user can know the number of the week in the entire year,  android calendar view horizontal scroll will help you to scroll down to get next month appeared in the view and so on….

Calendar View also got many other features like calendar View month color, calendar View multiple dates, calendar view mark day, calendar view language, calendar view mark dates and many more so it much exciting for the previous users of date picker.

You can customize the calendar depending upon your requirements like you can show a week wise dates or complete month based on your requirements.

So now let’s get started with this tutorial and know more about implementation.

 

 

activity_main.xml :

Adding a Android CalendarView to the layout.You may add other elements and customize the way a calendar can look based on your requirement.

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

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="fill_parent"
        android:layout_height="600dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#f2f3f4" />


</RelativeLayout>

MainActivity.java :

 

Now initialize Android CalendarView and then setonclicklistener to make sure you get the user input.

calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month,
                                    int dayOfMonth) {
        // TODO Auto-generated method stub
      
    }
});

 

Android CalendarView Full code :

Providing the full code for Android CalendarView implementation.

Now click on the date you will get a toast displaying day, month, year as selected.

import android.app.Activity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        calendar = (CalendarView)findViewById(R.id.calendarView);

        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                                            int dayOfMonth) {
                // TODO Auto-generated method stub

                Toast.makeText(MainActivity.this,"You have Selected : " + dayOfMonth +" / " + (month+1) + " / " + year,Toast.LENGTH_LONG).show();


            }
        });
    }
}

Android CalendarView Output :

Finally we can show Android CalendarView on to the screen as below.

 

If you have any query’s in this tutorial on Android CalendarView do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates.

abhishek:
Related Post

This website uses cookies.