OSD600 - My Fourth Hacktoberfest Contribution
Tags: opensource, hacktoberfest, android, java
Published: 2021-10-30 08:32:27

## 1. Introduction
This semester, I also attend to the Mobile App Development - Android (MAP524) which is about developing mobile applications for the Android platform. I feel quite interested in designing a mobile app so I searched for all android projects with Hacktoberfest label in Git Hub. Luckily, I found one named [Androapps](https://github.com/aadityamp01/Androapps), this project contains a lot of Android applications which were contributed by many developers. And I decided to engage in.

## 2. The issue
The [issue](https://github.com/aadityamp01/Androapps/issues/53) I was assigned is to create a Weather forecast application. The maintainer is quite easy-going, he was not be strict about how the app should look like but gave me the freedom to be creative.

## 3. The solution
Here are steps I did to create my application:
* Find a weather API
 - After a quick research, I decided to use [Open Weather Map](https://openweathermap.org/guide) which returns JSON objects. For more information about how to use this, please read the [guide](https://openweathermap.org/guide).
* Design the UI
 - Since I just learned about some layouts and basic views in MAP524 so I am going to design a simple UI with an `EditText` to get user input, a `Button` to submit the input, some `TextView` to display information and a `ImageView` for app background.
 
 - The UI design:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38gps0ecqc3fkeui2n1e.png)
 
* Implement Java code
 1) Using [Volley](https://developer.android.com/training/volley) library to request data from the API.
 2) Get JSON objects/data from response:
```
 // gets JSON objects
JSONObject mainWeather = response.getJSONObject("main");
JSONObject weather = response.getJSONArray("weather").getJSONObject(0);
JSONObject windInfo = response.getJSONObject("wind");
JSONObject sunInfo = response.getJSONObject("sys");
int humidity = mainWeather.getInt("humidity");
double windSpeed = windInfo.getDouble("speed");
```
   3) Calculate data:
```
// converts unix time stamp to date
Date dateInfo = new Date(response.getLong("dt")*1000);
Date sunRiseD = new Date(sunInfo.getLong("sunrise") * 1000);
Date sunSetD = new Date(sunInfo.getLong("sunset") * 1000);
int timezone = response.getInt("timezone");

 // formats output and changes timezone
@SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, dd MMM yyyy");         
dateFormat.setTimeZone(TimeZone.getTimeZone(ZoneOffset.ofTotalSeconds(timezone)));

@SuppressLint("SimpleDateFormat") SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm a");           
timeFormat.setTimeZone(TimeZone.getTimeZone(ZoneOffset.ofTotalSeconds(timezone)));

 int tempC = (int) (Math.round(mainWeather.getDouble("temp") - 273.15));
 int feelTemp = (int)(Math.round(mainWeather.getDouble("feels_like") - 273.15));
```
4) Display data appropriately to the UI:
```
currentDate.setText(dateStr);
currentTime.setText(timeStr);
temperature.setText(tempC + "°");
location.setText(city.toUpperCase());          
displayWeather.setText(weather.getString("main"));
feels.setText("Feels like: " + feelTemp + "°C");
humid.setText(humidity + "%");
wind.setText(windSpeed + "m/s");
sunRise.setText(sunRiseStr);
sunSet.setText(sunSetStr);
```
5) Function to set `ImageView` and text color based on time
```
void setBackground(int hour, ImageView imgV){...}
```
[View the app](https://drive.google.com/file/d/16VumiNXevAgXSQW-tmHzRN1CEc7xQjML/view?usp=sharing)


The maintainer reviewed the PR very quick, he merged my PR without any requirements.

If you want to get more information about the app, please take a look to this [PR](https://github.com/aadityamp01/Androapps/pull/67).

## 4. Overall
I gained a lot of new information after completing this issue. It was painful and enjoyable at the same time.
Thank you for reading the post.

Happy coding!