Developing a Weather Application with Kotlin on Android Studio Part-1

Muhammed Salih Guler
6 min readAug 4, 2017

--

Credit: https://kotlinlang.org/assets/images/twitter-card/kotlin_800x320.png

After Kotlin is announced as an official language for Android development, it became the popular kid in the Android neighbourhood. Kotlin has more history than the announcement for sure, but we are not going to talk about that in this article.

I wanted to do something so i can learn this language deeper. With the guidance of my colleague Matthias (He is a really good developer who loves sharing his knowledge, Thank you Matthias!), i have dive into this world.
I did the Kotlin Koans and created a project by taking reference of Matthias’ project here. Afterwards i wanted to share my approach in this article, as a person who learns Kotlin, after developing in Java.

In this article, we will create an application, which we can search for a city name and get the 7 day forecast for the city. For retrieving data we will use OpenWeatherAPI, and we will be needing an API key for it. Before proceeding please get an API key by registering.

By doing this application we will be covering up how to make network calls, parse the data and use it in a RecyclerView with an Custom Adapter with Kotlin. To continue on this article to develop this weather application, you need to have at least a basic understanding on Dagger, Retrofit, Glide and Kotlin (obviously). For Kotlin, i recommend you to finish Kotlin Koans.

Before proceeding with the development, you need to do your Kotlin Configurations on Android Studio either from here or you can download the Android Studio 3.0 Canary from here and configurations would be ready. I will be using Android Studio 3.0 in this article.

If you are ready, Let’s go!

Creating the Android Studio Project

For the people who uses Android Studio with the Stable version, when you do the tutorial above, you have already created a Kotlin project. But we still need to show how to create a project with Kotlin on Android Studio 3.0 Canary versions.

Let’s open Android Studio and click a new project by following the path File|New|New Project… Then we will have a window in front of us as follows.

Creating Project Window

To Application Name section, write the name of the application, to Company Domain write a domain (if you have) or write a domain that does not exist, because if you want to publish this application someday, the package name below should be unique, and it is generated with the combination of your application name and domain.

Most importantly click the check box next to Include Kotlin Support. Now you have included Kotlin in your project, Click Next and decide for your minimum and target sdk levels in your next page. Minimum Sdk is the minimum sdk level that you will be supporting in your application and Target Sdk is the API Level on which the application is designed to run. It is better to use the highest for this one.

After you click next, select Empty Activity and finish creating the application.

Adding Dagger, Retrofit, Glide dependencies

Let’s start with adding the Dagger dependency. As stated earlier it would be better to have a basic understanding, but still if you do not have any idea about dagger, you can check this link out.

Okay let’s open our app gradle file and add the lines above, under dependencies. Afterwards, let’s click the Sync Now to update our gradle file. When it is synced, we can continue with adding Retrofit dependencies.

For parsing data from API calls i will be using GSON. GSON is a Java serialization/deserialization library to convert Java Objects into JSON and back. It makes a lot of things easier and more readable. If you have not worked with GSON with Retrofit before, you can check this link out.

Okay let’s continue with adding Retrofit and Glide dependencies. After we add those, our gradle file will look more or less like this:

After adding these let’s click the Sync Now button and we can move on with the next steps!

Let’s start writing some Kotlin

Let’s proceed with adding the network call part of our application. First, i have created a package called api so we can separate the api related classes from the others. Under that package let’s create our api interface for api call. Right click on the package and click New|Kotlin File/Class to create a Kotlin file. I have created with a name OpenWeatherAPI.
When we create it , file will look empty. We need to create the class from scratch. For our case, it will look like as follows.

In getForecast we are passing two variables. “q” variable is the city name that we are going to search for and the “cnt” is the number of days that we want to see the forecast of.

After you do Koans, you are more or less familiar with the function creation but you may be wondering about what is this “companion object”. In java we are able to create a static variable and call it with this pattern “ClassName.variableName” easily. But in Kotlin we do not have static variable for class, therefore we are creating a companion variable and this is doing the same thing for us.

Next let’s create our Interceptor. We will use interceptor to hook up the header and query parameters to the request. An interceptor can be thought as a stop before you make the request, so we can add monitor, rewrite, and retry to the calls. But of course if you have not used the interceptors before, this is a good idea to read the official document to have an idea about it.

You did great job so far! I know that it is plenty of new stuff, but best way to learn something is to use it!

Adding Dagger Module and Components

We will proceed now with adding the Dagger pieces. Let’s create a package called injection and let’s create two packages under it with names component and module.

If you have read about Dagger with the links that i have provided or worked with it before, this words will at least tell you which files we are going to put under that package.

Let’s start with GSONModule. Let’s create a GSON Module under module package. It simply creates a gson for parcing data.

Now we will create our OpenWeatherAPIModule for providing OpenWeatherApi object which is generated for making network calls. We will also include the Gson provider to this module so we can add it to our mechanism.

From the code above, first we are adding our interceptor to our client. Then we are following our Builder pattern with client, base url and Gson to create OpenWeatherAPI object.

Last thing to do for this part is, to create our component. We will create a component class under component folder, this will be used to inject our OpenWeatherAPIModule.

Probably you are wondering about MainPresenter over there, because we have not created any class like that yet, this will cause error. Since we do not have anything functional right now, you can comment that line out for this part of the article.

Congratulations! You have done plenty of important stuff ready for the project. Let’s take a step back and look what we have done so far. What are the things that are different than Java implementation? Let’s have some brain storming and understand everything more deep.
I will prepare the second part really soon!

If you have any questions or suggestions, feel free to add a comment.

Happy Coding.

Salih.

--

--