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, 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 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 which returns JSON objects. For more information about how to use this, please read the 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, aButton
to submit the input, someTextView
to display information and aImageView
for app background. - The UI design:
- Since I just learned about some layouts and basic views in MAP524 so I am going to design a simple UI with an
Implement Java code
1) Using 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){...}
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.
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!
Top comments (0)