In this project, we will be building a weather application. This application will show the temperature of a location. To fetch weather information we will need an API. An API(Application Programming Interface) is a function that allows applications to interact and share data using various components and microservices. For this project, we will be using WeatherBit API for fetching weather data. WeatherBit API provides a fast and elegant way to fetch weather data. Note that we are going to implement this project using the Kotlin language.
In this project, we will build an app that will find the device’s location coordinates(longitude and latitude). Then we will send this data to the API via an API key(which we will see later). The API will send us a JSON from which we will extract the required data that is the temperature and city of the location.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Before going to the coding section first you have to do some pre-task
Go to the app > res > drawable file and paste this image to this file. Now right-click on the drawable folder > New > Drawable resource File and name the file as btn_bg6. Below is the code for the btn_bg6.xml file.
Reference Article: How to Add Image to Drawable Folder in Android Studio?
Step 3: Get the API key
To get the API key simply sign-in on WeatherBit and subscribe for a free API of Current Weather Data API. After doing so you will receive an API key and you are good to go.
Step 4: Permission Check
For this app to work we need to ask for three permissions from the system –
- Coarse Location – <uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
- Fine Location – <uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>
- Internet – <uses-permission android:name=”android.permission.INTERNET”/>
We will add code for use permission in the AndroidManifest.xml file. See the below code
Step 5: Building the Layout
We will add a Button and TextView in the one and only screen of the application. When the user will click the Button the temperature and city of that location will be shown in the TextView. See the code below:
Step 6: Getting Device’s Coordinates
In this step, we will get the last location of the device using Google Play Services –
- Setup google play services by downloading its components in the SDK manager.
- Add dependency in build.gradle for location – “implementation com.google.android.gms:play-services-location:17.1.0″, the version might change later.
- In the onCreate() method create an instance of the Fused Location Provider Client.
- After this use lastlocation() method to call the last location. See the Kotlin code after Step 5.
Step 7: Parse JSON after getting it
To get JSON we need to use Volley Library to make an HTTP client request
- Add ‘implementation com.android.volley:volley:1.1.1’ to the gradle app file.
- Create the http for the JSON. For example – “https://api.weatherbit.io/v2.0/current?” + “lat=” + location?.latitude +”&lon=”+ location?.longitude + “&key=”+ api_id1. Here we have already generated API key as well as the location coordinates.
- Make a request from this URL and get the data.
Step 8: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Note: Before running the application make sure the location in the device is turned on and the application has access to that.
GitHub link: https://github.com/njdunk07/NJ-Weather-GFG