How To Create Calendar View In Android Application

Introduction

 
Android is one of the most popular operating systems for mobile. Users can access the internet through the browser. Each Android mobile has a calendar view. The calendar is an important thing to handle events and commitments. So, I will show you how to create a calendar view in Android applications using an android studio. Android is a kernel-based operating system. It allows the user to modify the GUI components and source code.
 
Requirements
Steps should be followed
 
Carefully follow my steps to create a calendar view in android applications using an android studio and I have included the source code below.
 
Step 1
 
Open the android studio to start the new project.
 
Android
 
Step 2
 
Put the application name and company domain. If you wish to use c++ for code the project, mark the Include c++ support then click next.
 
Android
 
Step 3
 
Select the android minimum SDK. After you chose the minimum SDK it will show the approximate percentage of people use that SDK then click next.
 
Android
 
Step 4
 
Choose the basic activity then click next.
 
Android
 
Step 5
 
Put the activity name and layout name. Android studio basically takes the java class name is what you provide the activity name and click finish.
 
Android
 
Step 6
 
Go to activity_main.xml then click the text bottom. This XML file contains the designing code for the android app. Into the activity_main.xml copy and paste the below code.
 
Activity_main.xml code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="ganeshannt.calendarview.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/date"  
  11.         android:layout_width="0dp"  
  12.         android:layout_height="51dp"  
  13.         android:text="Date"  
  14.         android:textAlignment="center"  
  15.         android:textSize="45dp"  
  16.         android:visibility="visible"  
  17.         app:layout_constraintBottom_toBottomOf="parent"  
  18.         app:layout_constraintHorizontal_bias="0.0"  
  19.         app:layout_constraintLeft_toLeftOf="parent"  
  20.         app:layout_constraintRight_toRightOf="parent"  
  21.         app:layout_constraintTop_toTopOf="parent"  
  22.         app:layout_constraintVertical_bias="0.219" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/btngocalendar"  
  26.         android:layout_width="266dp"  
  27.         android:layout_height="47dp"  
  28.         android:text="Go to calendar"  
  29.         tools:layout_editor_absoluteX="47dp"  
  30.         tools:layout_editor_absoluteY="8dp" />  
  31.   
  32. </android.support.constraint.ConstraintLayout>  
 
Android
 
Step 7
 
Create a new calendar_layout.xml file (File ⇒ New ⇒Activity⇒Empty_activity).
 
Go to calendar_layout.xml then click the text bottom. This XML file contains the designing code for android app. Into the calendar_layout.xml copy and paste the below code.
 
calendar_layout.xml code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <CalendarView  
  7.         android:id="@+id/calendarView"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content" />  
  10. </LinearLayout>  
 
Android
 
Step 8
 
Into the MainActivity.java copy and paste the below code.java programming is the backend language for android. Do not replace your package name otherwise, the app will not run.
 
MainActivity.java code
  1. package ganeshannt.calendarview;    
  2.     
  3. import android.content.Intent;    
  4. import android.os.Bundle;    
  5. import android.support.v7.app.AppCompatActivity;    
  6. import android.view.View;    
  7. import android.widget.Button;    
  8. import android.widget.TextView;    
  9.     
  10. public class MainActivity extends AppCompatActivity {    
  11.     
  12.     private static final String TAG = "MainActivity";    
  13.     
  14.     private TextView thedate;    
  15.     private Button btngocalendar;    
  16.     
  17.     @Override    
  18.     protected void onCreate(Bundle savedInstanceState) {    
  19.         super.onCreate(savedInstanceState);    
  20.         setContentView(R.layout.activity_main);    
  21.         thedate = (TextView) findViewById(R.id.date);    
  22.         btngocalendar = (Button) findViewById(R.id.btngocalendar);    
  23.     
  24.         Intent incoming = getIntent();    
  25.         String date = incoming.getStringExtra("date");    
  26.         thedate.setText(date);    
  27.     
  28.         btngocalendar.setOnClickListener(new View.OnClickListener() {    
  29.             @Override    
  30.             public void onClick(View v) {    
  31.                 Intent intent = new Intent(MainActivity.this,CalendarActivity.class);    
  32.                 startActivity(intent);    
  33.             }    
  34.         });    
  35.     }    
  36. }   
Step 9
 
Create a new CalendarActivity.java file (File ⇒ New ⇒Java class).
 
Into the CalendarActivity.java copy and paste the below code.java programming is the backend language for android. Do not replace your package name otherwise, the app will not run.
 
CalendarActivity.java code
  1.     package ganeshannt.calendarview;    
  2.         
  3.     import android.content.Intent;    
  4.     import android.os.Bundle;    
  5.     import android.support.annotation.Nullable;    
  6.     import android.support.v7.app.AppCompatActivity;    
  7.     import android.util.Log;    
  8.     import android.widget.CalendarView;    
  9.         
  10.     /**  
  11.      * Created by ganesh on 6/10/2017.  
  12.      */    
  13.         
  14.     public class CalendarActivity extends AppCompatActivity {    
  15.         
  16.         private  static final String TAG = "CalendarActivity";    
  17.         private CalendarView mCalendarView;    
  18.         @Override    
  19.         protected void onCreate(@Nullable Bundle savedInstanceState) {    
  20.             super.onCreate(savedInstanceState);    
  21.             setContentView(R.layout.calendar_layout);    
  22.             mCalendarView = (CalendarView) findViewById(R.id.calendarView);    
  23.             mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {    
  24.                 @Override    
  25.                 public void onSelectedDayChange(CalendarView CalendarView, int year, int month, int dayOfMonth) {    
  26.                   String date = year + "/" + month + "/"+ dayOfMonth ;    
  27.                     Log.d(TAG, "onSelectedDayChange: yyyy/mm/dd:" + date);    
  28.                     Intent intent = new Intent(CalendarActivity.this,MainActivity.class);    
  29.                     intent.putExtra("date",date);    
  30.                     startActivity(intent);    
  31.         
  32.                 }    
  33.             });    
  34.         }    
  35.     }    
  36.   
  37. Step 10  
  38.    
  39.    
  40. Add the calendaractivity into the androidmanifest.xml so add below code into the AndroidManifest.xml.  
  41.    
  42.    
  43. AndroidManifest.xml code  
  44.   
  45.     <?xml version="1.0" encoding="utf-8"?>    
  46.     <manifest xmlns:android="http://schemas.android.com/apk/res/android"    
  47.         package="ganeshannt.calendarview">    
  48.         
  49.         <application    
  50.             android:allowBackup="true"    
  51.             android:icon="@mipmap/ic_launcher"    
  52.             android:label="@string/app_name"    
  53.             android:roundIcon="@mipmap/ic_launcher_round"    
  54.             android:supportsRtl="true"    
  55.             android:theme="@style/AppTheme">    
  56.             <activity android:name=".MainActivity">    
  57.                 <intent-filter>    
  58.                     <action android:name="android.intent.action.MAIN" />    
  59.         
  60.                     <category android:name="android.intent.category.LAUNCHER" />    
  61.                 </intent-filter>    
  62.             </activity>    
  63.             <activity android:name=".CalendarActivity"></activity>    
  64.         </application>    
  65.         
  66.     </manifest>   
Step 11
 
This is our user interface of the application. Click the make project option.
 
Android
 
Deliverables
 
Here calendar view in the android application was successfully created and executed.
 
Android
 
Android
 
Android
 
Android
 
Android
 
Don’t forget to like and follow me. If you have any doubts just comment below.


Similar Articles