Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_first, container, false);
        // For displaying weather data
        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);

        // Get our ViewModel instance
        viewModel = new ViewModelProvider(this).get(WeatherDataViewModel.class);

        // And whenever the data changes, refresh the UI
        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) {
        // The ViewModel controls loading the data, so we just
        // tell it what the new name is - this kicks off loading
        // the data, which will automatically call through to
        // our observe() call when the data load completes
        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()
        }
    }
Posted
Comments
David Crow 8-Jul-21 22:51pm    
I did a similar thing here. See if that helps.
Chinedum Ezeozue 9-Jul-21 17:40pm    
I checked it out and while it seems quite comprehensive, it focuses majorly on requesting the device's current location. You also make use of JSON while my app is based on retrofit, then you use AccuWeather API while I use OpenWeatherMap, and lastly, I saw no part that talks about setting up icons. So please I still need your help on my app if there's any way you can assist. Unfortunately, I'm in college and short of funds if not I'd have hired a tutor but the little funds I have are what I'll use in the release and ads of my app when it's done. So if you could just suggest some snippets to try out, so that I could eventually take it to StackOverflow and ask for further help. Taking it there with the current state will only heed some downvotes. I'll appreciate your help
David Crow 10-Jul-21 18:24pm    
"...and lastly, I saw no part that talks about setting up icons."

In either of the adapter's onBindViewHolder() method, you should've noticed the following three statements:
String sIcon = "i" + details.m_nIcon;
int nIcon = activity.getResources().getIdentifier(sIcon, "drawable", activity.getPackageName());
holder.mImage.setImageBitmap(BitmapFactory.decodeResource(activity.getResources(), nIcon));
All of the images were downloaded once from AccuWeather's web site and stored in the drawable folder. The files were named i#.png, with # ranging from 1 to 42.

"...I could eventually take it to StackOverflow and ask for further help. Taking it there with the current state will only heed some downvotes."

SO's inhabitants are all about points, ratings, and reputation. A large part of the regulars here at CP couldn't care less about those things. We just want to help, period.
Chinedum Ezeozue 10-Jul-21 18:38pm    
Sure, I respect you guys method here, that's why I come help. Thanks a lot, I'll try it out

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900