This article shows how to create an android application for displaying the Calendar using CalendarView. It also provides the selection of the current date and displaying the date. The setOnDateChangeListener Interface is used which provide onSelectedDayChange method.
- onSelectedDayChange: In this method, we get the values of days, months and years that is selected by the user.
Below are the steps for creating Android Application of the Calendar.
- Step 1: Create a new project and you will have a layout XML file and java file. Your screen will look like the image below.
- Step 2: Open your xml file and add CalendarView and TextView. And assign id to TextView and CalendarView. After completing this process, the xml file screen looks like given below.
- Step 3: Now, open up the activity java file and define the CalendarView and TextView type variable, and also use findViewById() to get the Calendarview and textview.
- Step 4: Now, add setOnDateChangeListener interface in object of CalendarView which provides setOnDateChangeListener method. In this method we get the Dates(days, months, years) and set the dates in TextView for Display.
- Step 5: Now run the app and set the current date which will be shown on the top of the screen.
Complete code of MainActivity.java or activity_main.xml of Calendar is given below.
activity_main.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
RelativeLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<!-- Add TextView to display the date -->
<
TextView
android:id
=
"@+id/date_view"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_marginLeft
=
"150dp"
android:layout_marginTop
=
"20dp"
android:text
=
"Set the Date"
android:textColor
=
"@android:color/background_dark"
android:textStyle
=
"bold"
/>
<!-- Add CalenderView to display the Calender -->
<
CalendarView
android:id
=
"@+id/calender"
android:layout_marginTop
=
"80dp"
android:layout_marginLeft
=
"19dp"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
>
</
CalendarView
>
</
RelativeLayout
>
chevron_rightfilter_noneMainActivity.java
package
org.geeksforgeeks.navedmalik.calendar;
import
android.support.annotation.NonNull;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.widget.Button;
import
android.widget.CalendarView;
import
android.widget.TextView;
public
class
MainActivity
extends
AppCompatActivity {
// Define the variable of CalendarView type
// and TextView type;
CalendarView calender;
TextView date_view;
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// By ID we can use each component
// which id is assign in xml file
// use findViewById() to get the
// CalendarView and TextView
calender = (CalendarView)
findViewById(R.id.calender);
date_view = (TextView)
findViewById(R.id.date_view);
// Add Listener in calendar
calender
.setOnDateChangeListener(
new
CalendarView
.OnDateChangeListener() {
@Override
// In this Listener have one method
// and in this method we will
// get the value of DAYS, MONTH, YEARS
public
void
onSelectedDayChange(
@NonNull
CalendarView view,
int
year,
int
month,
int
dayOfMonth)
{
// Store the value of date with
// format in String type Variable
// Add 1 in month because month
// index is start with 0
String Date
= dayOfMonth +
"-"
+ (month +
1
) +
"-"
+ year;
// set this date in TextView for Display
date_view.setText(Date);
}
});
}
}
chevron_rightfilter_noneOutput:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.