I'm done with almost every other setup for my app, now I've been trying for hours to figure out how I can display weather icons for my app according to its city's response but I still have no idea how to do that.
I've searched several sites for tutorials on it, but none were helpful, so I decided to come here to find help. And I will surely appreciate it if anyone can help.
Following the API instructions, I'm using https://openweathermap.org/weather-conditions there are 9 main weather conditions.
This is my goal using it:
*When the app is open at first, display no icon.
*If a user searches for a city and the response he gets is a clear sky, display the Clear Sky icon;
- Otherwise, if the response is Few Clouds in that city, display the Few Clouds icon
- Otherwise, if the response is Scattered clouds in that city, display the Scattered clouds icon
- Otherwise, if the response is Broken Clouds in that city, display the Broken Clouds icon
- Otherwise, if the response is Shower rain in that city, display the Shower rain icon
- Otherwise, if the response is Rain in that city, display the Rain icon
- Otherwise, if the response is Thunderstorm in that city, display the Thunderstorm icon
- Otherwise, if the response is Snow in that city, display the Snow icon
- Otherwise, if the response is Mist there, display the Mist icon.
I don't plan on using the API icons provided by OpenWeatherMap, rather I have all the 9 icons I want to use on my drawable.
I just need to know the right way to use them on my app.
I feel I will need to create a method, along with an if-else and maybe case statement, but still, I don't know the right way to go about it.
I don't have a specific response on the app, as it can search for any city, but an example of the response it gets is like this:
...
"weather":[
{
"id":801,
"main":"Clouds",
"description":"few clouds",
"icon":"02n"
}
],
...
My Fragment class(I'm using viewmodel):
public class FirstFragment extends Fragment {
private WeatherDataViewModel viewModel;
public FirstFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
final TextView current_temp = rootView.findViewById(R.id.textView10);
final TextView current_output = rootView.findViewById(R.id.textView11);
final TextView rise_time = rootView.findViewById(R.id.textView25);
final TextView set_time = rootView.findViewById(R.id.textView26);
final TextView temp_out = rootView.findViewById(R.id.textView28);
final TextView Press_out = rootView.findViewById(R.id.textView29);
final TextView Humid_out = rootView.findViewById(R.id.textView30);
final TextView Ws_out = rootView.findViewById(R.id.textView33);
final TextView Visi_out = rootView.findViewById(R.id.textView34);
final TextView Cloud_out = rootView.findViewById(R.id.textView35);
viewModel = new ViewModelProvider(this).get(WeatherDataViewModel.class);
viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> {
if (data != null) {
current_temp.setVisibility(View.VISIBLE);
current_temp.setText(data.getMain().getTemp() + " ℃");
current_output.setVisibility(View.VISIBLE);
current_output.setText(data.getWeather().get(0).getDescription());
rise_time.setVisibility(View.VISIBLE);
rise_time.setText(data.getSys().getSunrise() + " ");
set_time.setVisibility(View.VISIBLE);
set_time.setText(data.getSys().getSunset() + " ");
temp_out.setVisibility(View.VISIBLE);
temp_out.setText(data.getMain().getTemp() + " ℃");
Press_out.setVisibility(View.VISIBLE);
Press_out.setText(data.getMain().getPressure() + " hpa");
Humid_out.setVisibility(View.VISIBLE);
Humid_out.setText(data.getMain().getHumidity() + " %");
Ws_out.setVisibility(View.VISIBLE);
Ws_out.setText(data.getWind().getSpeed() + " Km/h");
Visi_out.setVisibility(View.VISIBLE);
Visi_out.setText(data.getVisibility() + " m");
Cloud_out.setVisibility(View.VISIBLE);
Cloud_out.setText(data.getClouds().getAll() + " %");
} else {
Log.e("TAG", "No City found");
current_temp.setVisibility(View.GONE);
current_output.setVisibility(View.GONE);
rise_time.setVisibility(View.GONE);
set_time.setVisibility(View.GONE);
temp_out.setVisibility(View.GONE);
Press_out.setVisibility(View.GONE);
Humid_out.setVisibility(View.GONE);
Ws_out.setVisibility(View.GONE);
Visi_out.setVisibility(View.GONE);
Cloud_out.setVisibility(View.GONE);
Toast.makeText(requireActivity(), "No City found", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
public void getWeatherData(String name) {
viewModel.setCityName(name);
}
}
What I have tried:
The little part i tried failed totally;
private void setWeatherIcon(int actualId) {
int id = actualId / 100;
Drawable sun = "";
if (actualId == 800){
sun = getActivity()
}
}