Introduction

Android is one of the most popular operating systems for mobiles. In this article, I will show you how to create a Calendar Android application. The calendar is an important thing to handle events and commitments. So, I will show you how to create a calendar in Android applications using Android Studio.
Requirements
Steps should be followed
Follow these steps to create a Native Calendar Android application using Android Studio. I have attached the source code too.
Step 1
Open Android Studio and start a New Android Studio Project.
Android
Step 2
You can choose your application name and choose where your project is stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
Android
Now, select the version of Android and select the target Android devices.
Android
Step 3
Now, add the activity and click the "Next" button.
Android
Add activity name and click "Finish".
Android
Step 4
Go to activity_main.xml, This XML file contains the designing code for an Android app.
Android
The XML code is given below.
  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="abu.calendar.MainActivity">
  8. <TextView
  9. android:id="@+id/date"
  10. android:layout_width="0dp"
  11. android:layout_height="51dp"
  12. android:text="Date"
  13. android:textAlignment="center"
  14. android:textSize="45dp"
  15. android:visibility="visible"
  16. app:layout_constraintBottom_toBottomOf="parent"
  17. app:layout_constraintHorizontal_bias="0.0"
  18. app:layout_constraintLeft_toLeftOf="parent"
  19. app:layout_constraintRight_toRightOf="parent"
  20. app:layout_constraintTop_toTopOf="parent"
  21. app:layout_constraintVertical_bias="0.219" />
  22. <Button
  23. android:id="@+id/btngocalendar"
  24. android:layout_width="266dp"
  25. android:layout_height="47dp"
  26. android:text=" Calendar"
  27. tools:layout_editor_absoluteX="47dp"
  28. tools:layout_editor_absoluteY="8dp" />
  29. </android.support.constraint.ConstraintLayout>
Step 5
Go to layout ⇒ New ⇒XML⇒Layout XML file.
Android
The layout XML code is given below.
  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. <CalendarView
  6. android:id="@+id/calendarView"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content" />
  9. </LinearLayout>
Step 6
Go to Java ⇒ New ⇒Java class.
Android
The calendar activity java code is given below.
  1. package abu.calendar;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.annotation.Nullable;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.util.Log;
  7. import android.widget.CalendarView;
  8. public class calendaractivity extends AppCompatActivity {
  9. private static final String TAG = "CalendarActivity";
  10. private CalendarView mCalendarView;
  11. @Override
  12. protected void onCreate(@Nullable Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.calendar_layout);
  15. mCalendarView = (CalendarView) findViewById(R.id.calendarView);
  16. mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
  17. @Override
  18. public void onSelectedDayChange(CalendarView CalendarView, int year, int month, int dayOfMonth) {
  19. String date = year + "/" + month + "/"+ dayOfMonth ;
  20. Log.d(TAG, "onSelectedDayChange: yyyy/mm/dd:" + date);
  21. Intent intent = new Intent(calendaractivity.this,MainActivity.class);
  22. intent.putExtra("date",date);
  23. startActivity(intent);
  24. }
  25. });
  26. }
  27. }
Step 7
Go to Main Activity.java. This Java program is the back-end language for Android. The Java code is given below.
  1. package abu.calendar;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. public class MainActivity extends AppCompatActivity {
  9. private static final String TAG = "MainActivity";
  10. private TextView thedate;
  11. private Button btngocalendar;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. thedate = (TextView) findViewById(R.id.date);
  17. btngocalendar = (Button) findViewById(R.id.btngocalendar);
  18. Intent incoming = getIntent();
  19. String date = incoming.getStringExtra("date");
  20. thedate.setText(date);
  21. btngocalendar.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. Intent intent = new Intent(MainActivity.this,calendaractivity.class);
  25. startActivity(intent);
  26. }
  27. });
  28. }
  29. }
Step 8
We need to make Calendar View. So, add calendaractivity in an AndroidManifest.xml.
Android
The AndroidManifest.xml Code is this,
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="abu.calendar">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:roundIcon="@mipmap/ic_launcher_round"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14. <category android:name="android.intent.category.LAUNCHER" />
  15. </intent-filter>
  16. </activity>
  17. <activity android:name=".calendaractivity"></activity>
  18. </application>
  19. </manifest>
Step 9
Now, either go to the menu bar and click "Make a project" or press ctrl+f9 to debug the error.
Android
Step 10
Then, click the "Run" button or press shift+f10 to run the project. Select the "virtual machine" option and click OK.
Android

Conclusion

We have successfully created a Calendar Android application using Android Studio.
Android
Android