Android | Creating a Calendar View app
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 are selected by the user.
Below are the steps for creating the 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 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" > <!-- 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 CalendarView to display the Calendar --> < CalendarView android:id = "@+id/calendar" android:layout_marginTop = "80dp" android:layout_marginLeft = "19dp" android:layout_width = "wrap_content" android:layout_height = "wrap_content" > </ CalendarView > </ RelativeLayout > |
MainActivity.java
package org.w3wiki.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 calendar; 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 calendar = (CalendarView) findViewById(R.id.calendar); date_view = (TextView) findViewById(R.id.date_view); // Add Listener in calendar calendar .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); } }); } } |
Output:
Contact Us