Get Events to Your Android App Using Google Calendar API

Enes Koçer
6 min readJan 28, 2023

--

In this article, I will explain how to integrate Google Calendar into our Native Android application, which I had a very difficult time finding resources to take advantage of.

With this way, you can add, delete, update or get as many events as you want to the calendar of your google account that you are connected to from within your application.
In this article, I will only talk about the get request in order not to prolong the subject and bore you.

First, let’s open our project and add the necessary libraries to the dependency section in build.gradle(:app).

After adding our libraries, let’s add the necessary permissions to our AndroidManifest.xml file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

Now we need to go to Google Cloud Platform and enter the console section, where we will enter the name of our project and create it in the new tab that opens by saying New Project in the upper left section.

On the new page that opens, we have to select this project and select Credentials from the left side menu.

In this section we need to create OAuth 2.0 Client IDs. For this we need to click on the Create Credentials button at the top and click on OAuth Client ID.
First, we will select our configuration as external.

Now we will be presented with 4 stages to fill in.
→The first one is the OAuth consent screen. Here you will only need to fill in the required fields.
→In the second phase we just kept going.
→In the third part, you will need to add the e-mail addresses with which google account/accounts you will test the calendar feature during the development phase of your application.
→In the fourth step you can return to the dashboard screen.

After this stage, you need to follow the steps below again and connect your project to this OAuth Key.
APIs and Services > Credentials > Create Credential > OAuth client ID
On the page that opens, select the application type Android.
On the next page, in the “Name” field, type a name for the credential. This name is only shown in Google Cloud Console. Enter the package name of your AndroidManifest.xml file in the “Package name” field. Finally, you can create your SHA-1 certificate after entering it. (If you don’t know how to generate a SHA-1 certificate, you can find it in another part of this article).

Now our key is created. You can download our json file and open the Project level in our project and throw it into the app file.

Let’s add our Google Calendar library in our console. For this, we go to the library, type Google Calendar API in the search field, click on the result and set it to Enable.

Now we will start writing our code, but I will only talk about the code blocks related to our topic. You can access all the code from my Github repository, which I will leave at the end of the article.

First, let’s create a Constants file and write our code and key in it, which will make it easier for us to check at which stage the user is in the requirements of the system we are integrating.

We will create a model for our data from the calendar. I will only get the title and start date of the event.

Now we create two variables inside our Fragment. One of them is for logging into our Google account through our application and the other one is for accessing our Google calendar.

We need to define these variables. First we will define our mCredential variable because if we don’t have an account, we don’t have a calendar. For this we will write the following function and at the end we will call the function where we define our calendar and give mCredential as a parameter. Because our calendar will be the calendar of the connected account, we will also need a defined credential when defining our mService variable.
We need to call our initCredentials() function inside our onCreate() function.

Now we can create the function where we define our mService variable. Give setApplicationName the name of your application.

The function we are going to write now will allow us to perform the necessary checks and perform the necessary operations until the stage of retrieving the data from the calendar.

→Our first if block checks if the device supports google play service. If not, we will call the acquireGooglePlayServices() function.
With this function, if our device does not support Google play services, we show our message in a dialog on the screen.

→In our second if block, we check if there is a google account logged into the application. If there is no existing account logged in, we call our chooseAccount() function.
This function checks if we are not logged in with our account and opens the dialog screen to log in. In this way, we log in.

→In our third if block, we check whether our device is connected to the internet. If it is not connected, you can take the necessary actions.

→If all of these conditions are met, we call our makeRequestTask() function where we will throw the get request.
Since this function has a complex structure, I will first explain the structures we use in it.

First of all, since we will need to use Coroutines, I wrote an extension. With this customization, I determined our stages in my makeRequestTask() function.

We also call a separate function in our makeRequestTask() function where the get request is thrown and the data in the calendar is retrieved. This function is as follows.
Here we specify the properties of the information coming from the calendar via mService and execute it. We add the data from the calendar to the list we created and return.

Now let’s write our makeRequestTask() function as follows.

We start our Coroutine scope that I wrote as an extension.

onStart() in which we optionally show our progress design.
doInBackground() If there is no error, we call our getDataFromCalendar() function and our data list from the calendar is returned from this scope.
onPostExecute() in the other scope, we get the returned data. If the data list returned from doInBackground is not empty, we can use the data in the else scope as we want.
onCancelled() contains the checks that will take place in case our operations fail.

Since it is a very detailed subject, I tried to explain it as simplified as I could. But if you do not understand anything, you can contact me on Linkedin and ask me without hesitation.

Source code of the application:
https://github.com/kocerenes/GetEventCalendar

--

--