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. <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="Go to calendar"
  27. tools:layout_editor_absoluteX="47dp"
  28. tools:layout_editor_absoluteY="8dp" />
  29. </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. <CalendarView
  6. android:id="@+id/calendarView"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content" />
  9. </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. 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 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. 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. /**
  9. * Created by ganesh on 6/10/2017.
  10. */
  11. public class CalendarActivity extends AppCompatActivity {
  12. private static final String TAG = "CalendarActivity";
  13. private CalendarView mCalendarView;
  14. @Override
  15. protected void onCreate(@Nullable Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.calendar_layout);
  18. mCalendarView = (CalendarView) findViewById(R.id.calendarView);
  19. mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
  20. @Override
  21. public void onSelectedDayChange(CalendarView CalendarView, int year, int month, int dayOfMonth) {
  22. String date = year + "/" + month + "/"+ dayOfMonth ;
  23. Log.d(TAG, "onSelectedDayChange: yyyy/mm/dd:" + date);
  24. Intent intent = new Intent(CalendarActivity.this,MainActivity.class);
  25. intent.putExtra("date",date);
  26. startActivity(intent);
  27. }
  28. });
  29. }
  30. }
  31. Step 10
  32. Add the calendaractivity into the androidmanifest.xml so add below code into the AndroidManifest.xml.
  33. AndroidManifest.xml code
  34. <?xml version="1.0" encoding="utf-8"?>
  35. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  36. package="ganeshannt.calendarview">
  37. <application
  38. android:allowBackup="true"
  39. android:icon="@mipmap/ic_launcher"
  40. android:label="@string/app_name"
  41. android:roundIcon="@mipmap/ic_launcher_round"
  42. android:supportsRtl="true"
  43. android:theme="@style/AppTheme">
  44. <activity android:name=".MainActivity">
  45. <intent-filter>
  46. <action android:name="android.intent.action.MAIN" />
  47. <category android:name="android.intent.category.LAUNCHER" />
  48. </intent-filter>
  49. </activity>
  50. <activity android:name=".CalendarActivity"></activity>
  51. </application>
  52. </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.