CalendarView can be defined as below –
"Android CalendarView is a widget that are used to display and select dates. However, you can configure range of dates to be shown in the calendar."
Some of the popular attributes of android CalendarView are –
S. No. | XML Attributes | Description |
1 | android: id | This is unique id of the Calendar View to uniquely identify the Calendar View. |
2 | android: height | Height of the Calendar View. |
3 | android: width | Width of the Calendar View. |
4 | android: dateTextAppearance | The text appearance for the day numbers in the calendar grid. |
5 | android: firstDayOfWeek | Defines first day of the calendar. |
6 | android: maxDate | Defines maximal date shown in calendar view in mm/dd/yyyy format. |
7 | android:minDate | Defines minimal date shown in calendar view in mm/dd/yyyy format. |
8 | android: alpha | Sets alpha to the view. |
9 | android: background | Sets drawable to the background. |
10 | android: backgroundTint | Sets tint to apply to the background. |
11 | android: clickable | Specifies whether the view is clickable or not. |
12 | android: elevation | Sets elevation of the view. |
13 | android: focusable | Specifies whether this view can take focus or not. |
14 | android: visibility | Specifies the visibility(VISIBLE, INVISIBLE, GONE) of the view. |
Some of the popular attributes of android CalendarView inherited from FrameLayout are –
S. No. | XML Attributes | Description |
1 | android: foregroundGravity | Defines the gravity to apply to the foreground drawable. |
2 | android: measureAllChildren | Defines whether to measure all children or only those in VISIBLE or INVISIBLE state when measuring. |
Some of the popular attributes of android CalendarView inherited from viewGroup are –
S. No. | XML Attributes | Description |
1 | android: animateLayoutChanges | Specifies whether to run layout transition when there is any change in layout. |
2 | android: animationCache | Specifies whether to create drawing cache for children by layout animation. |
3 | android: clipToPadding | Defines whether the View Group will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero. |
4 | android: layoutAnimation | Specifies the layout animation to be used when the View Group is laid out for the first time. |
5 | android: layoutMode | Defines the layout mode of the View Group. |
At first, we will create android application. Then, we will use ImageButton in the application.
Follow steps below to create new project. Please ignore the steps if you have already created a new application.
S. No. | Steps |
1 | Open Android Studio. |
2 | Click on Start a new Android Studio Project Write application name as CalendarView. Then, click next button. |
3 | Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button. |
4 | Then, select Empty Activity => click next => click finish. |
5 | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail. |
Now, we will modify xml and java file to use CalendarView in the application.
No values folders have been modified. So, we are not going to mention them here.
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<CalendarView
android:id="@+id/CalendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"/>
</android.support.constraint.ConstraintLayout>
In activity_main.xml file, we have defined CalendarView. Now, we will access this CalendarView in java file to perform some operations on it.
Open src/main/java/com.ukacademe.calendarview/MainActivity.java file. Then, add below code into it.
package com.ukacademe.calendarview;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CalendarView calendarView = findViewById(R.id.CalendarView);
if (calendarView != null) {
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
// Note that months are indexed from 0. So, 0 means January, 1 means february, 2 means march etc.
String msg = "Selected date is " + dayOfMonth + "/" + (month + 1) + "/" + year;
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
}
In MainActivity.java file, we have accessed CalendarView. Then, we have set a date change listener that shows selected date as toast message.
Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ukacademe.calendarview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When we run the application, we will get output as shown below.