5

This was Initially asked about 20 days ago, I was trying to display weather icons on my app according to its city's response(from the drawable folder) from the number of weather conditions listed on the weather's official API doc https://openweathermap.org/weather-conditions (which you can always view by checking the edit history). There are 9 main weather conditions from the API.

This still remains my goal:

  • 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 in that city, display the Mist icon.

With Magdalena Rowicka's help, I've been able to achieve a few things but the issue has still not been fully solved even after trying to fix it myself and that's why I'm re-bountying the post.

The first thing I did was to create a separate enum class with the following sets of data:

public enum WeatherIcon {
    Sun, Cloud1, Cloud2, Cloud3, Rain1, Rain2, Thunder, Snow, Mist
}

Then I added this code final ImageView imageofWeather = rootView.findViewById(R.id.imageView2); in the place where i declared my textviews on fragment.

Then I added this int drawableResource; // here define default icon for example R.drawable.default_weather_icon after the viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> { on fragment.

Then i finally added this code:

switch (data.getWeather().get(0).getIcon()) { 
                    case WeatherIcon.Sun:
                        drawableResource = R.drawable.sun; //reference to drawable id
                        break;
                    case WeatherIcon.Cloud1:
                        drawableResource = R.drawable.broken_clouds; //reference to drawable id
                        break;
                    case WeatherIcon.Cloud2:
                        drawableResource = R.drawable.few_clouds; //reference to drawable id
                        break;
                    case WeatherIcon.Cloud3:
                        drawableResource = R.drawable.scattered_clouds; //reference to drawable id
                        break;
                    case WeatherIcon.Rain1:
                        drawableResource = R.drawable.small_rain; //reference to drawable id
                        break;
                    case WeatherIcon.Rain2:
                        drawableResource = R.drawable.shower_rain; //reference to drawable id
                        break;
                    case WeatherIcon.Thunder:
                        drawableResource = R.drawable.thunderstorm; //reference to drawable id
                        break;
                    case WeatherIcon.Snow:
                        drawableResource = R.drawable.snow; //reference to drawable id
                        break;
                    case WeatherIcon.Mist:
                        drawableResource = R.drawable.mist; //reference to drawable id
                        break;



        imageofWeather.setImageDrawable(drawableResource);
            }

Under the if statement in fragment to access the API directly and display the weather icons. (The respective 9 icons are currently shown visibly on the numbering line of the firstfragment class).

The issues from this current setup is that:

  • For each case statement, I get the following error:

The value R.drawable.sun(and the rest) assigned to 'drawableResource' is never used.

and

  • The line imageofWeather.setImageDrawable(drawableResource); shows this error:

Required type: Drawable, Provided: int

I will surely appreciate it if anyone can help.

Here's My Fragment code:

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
        // all field in Java should be type in camelCase, so not current_temp but currentTemp, not Cloud_out but cloudOut
        // Capitalize name is for class not field
        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);
        final ImageView imageofWeather = rootView.findViewById(R.id.imageView2);


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

        // And whenever the data changes, refresh the UI
        viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> {

            int drawableResource; // here define default icon for example R.drawable.default_weather_icon

            if (data != null) {
                current_temp.setVisibility(View.VISIBLE);
                current_temp.setText(data.getMain().getTemp() + " ℃"); // for that you can use strings resource and templates more in https://developer.android.com/guide/topics/resources/string-resource.html#formatting-strings
                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() + " %");

                // get actual weather.
                switch (data.getWeather().get(0).getIcon()) { //or data.getWeather()[0].getIcon() i don't remember how it work in Java
                    case WeatherIcon.Sun:
                        drawableResource = R.drawable.sun; //reference to drawable id
                        break;
                    case WeatherIcon.Cloud1:
                        drawableResource = R.drawable.broken_clouds; //reference to drawable id
                        break;
                    case WeatherIcon.Cloud2:
                        drawableResource = R.drawable.few_clouds; //reference to drawable id
                        break;
                    case WeatherIcon.Cloud3:
                        drawableResource = R.drawable.scattered_clouds; //reference to drawable id
                        break;
                    case WeatherIcon.Rain1:
                        drawableResource = R.drawable.small_rain; //reference to drawable id
                        break;
                    case WeatherIcon.Rain2:
                        drawableResource = R.drawable.shower_rain; //reference to drawable id
                        break;
                    case WeatherIcon.Thunder:
                        drawableResource = R.drawable.thunderstorm; //reference to drawable id
                        break;
                    case WeatherIcon.Snow:
                        drawableResource = R.drawable.snow; //reference to drawable id
                        break;
                    case WeatherIcon.Mist:
                        drawableResource = R.drawable.mist; //reference to drawable id
                        break;



                    imageofWeather.setImageDrawable(drawableResource);
                }

            } 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);
    }
}
Richard Wilson
  • 297
  • 4
  • 17
  • Hi, checking in if you have solved the issue otherwise I will be happy to help. Are you having a list of cities on your screen, and with every city you have their respective icon based on the weather? Or just one city shown on the screen and the corresponding weather icon? – Basu Jul 19 '21 at 20:04
  • @Basu Someone is currently helping me. But if she's unable to get it right, I'll let you know, Thanks. I can get any city I want, but it must be one at a time with corresponding weather icon – Richard Wilson Jul 20 '21 at 09:06
  • @Basu I've re-bounted the code since it hasn't been fully solved – Richard Wilson Aug 02 '21 at 18:53
  • Hi Richard. I've seen the code and found the error. I am sending the answer in few minutes. Will let know if I need more info – Basu Aug 03 '21 at 18:08

4 Answers4

2

Maybe try like this:

  1. Create a weather class with an enum specifying which icon to display.
  2. after downloading the data, convert to the created class
  3. A separate function that will decide what to display for a given enum

edited:

public class Example{
  // add to your definition
  private WeaterIcon icon;

  public WeaterIcon getIcon() {
      return icon;
  }

  public void setIcon(WeaterIcon icon) {
      this.icon = icon;
  }
}

enum WeaterIcon {
  SUN, FROG, //type all what want
}

in onCreateView

//there are yor reference to xml object
    final ImageView imageOfWeather = rootView.findViewById(R.id.imageView); // add this reference

and choose right icon

int drawableResource; // here define default icon or not
            switch(data.getIcon()) {
                case WeaterIcon.SUN:
                    drawableResource = R.drawable.sun_icon //reference to drawable id
                    break;
                case WeaterIcon.FROG:
                    drawableResource = R.drawable.frog_icon//reference to drawable id
                    break;
                //add all
            }


            imageOfWeather.setImageDrawable(drawableResource);
  • Sorry for code but I haven't used Java for a long time e.g. ``` private void displayIcon(IconState iconState) { swich(){ case IconState.Sun: { iconWeather.setDrawable(R.drawable.icon_sun); break; } } } ``` – Magdalena Rowicka Jul 15 '21 at 12:27
  • Please I'm a newbie still learning to code and I find it hard to understand your suggestion. Tried the snippet you sent and it failed with 6 compile errors – Richard Wilson Jul 15 '21 at 14:48
  • Okay, I have inputted the first snippet of the code to this in my example class: public class Weather { @SerializedName("icon") private WeatherIcon icon; public WeatherIcon getIcon() { return icon; } public void setIcon(WeatherIcon icon) { this.icon = icon; } I as well inputted the enum(as an outer class) successfully and I get no errors in both. https://hastebin.com/ebirokulup.kotlin I posted the code in this link for better understanding – Richard Wilson Jul 16 '21 at 14:50
  • I have also inputted the onCreateView part as well. But the problem is that when i type the switch statement on this part (data.getIcon()), it give an error: cannot resolve method 'getIcon' in 'Example'. I don't know why – Richard Wilson Jul 16 '21 at 14:51
  • Okey I start form the last. "data" must be a type where put getter of icon. Put WheatherIcon object in class that convert your response from API. – Magdalena Rowicka Jul 17 '21 at 15:06
  • I already put weatherIcon inside my example class, it's the class that converts my API response. Then it's on the inner class "weather". (According to the API) – Richard Wilson Jul 17 '21 at 21:04
  • In essence, It's directly inside the class that converts my response, so I don't think that's the issue. Maybe it's because I didn't call String or something – Richard Wilson Jul 17 '21 at 21:10
  • Could you past your code (ViewModel, Fragment and Example) with error in comments. That will help understand the problem. – Magdalena Rowicka Jul 18 '21 at 09:16
  • Okay, please I posted the full code here https://hastebin.com/oduqisakuh.properties to reduce complexity – Richard Wilson Jul 18 '21 at 13:36
  • 1
    Ok, I know what happend here. Tell me where you keep the weather This is the "Example" class, the "weather" field, which is a list of Weather objects? – Magdalena Rowicka Jul 19 '21 at 12:38
  • If yes I change your code https://hastebin.com/utojituroh.properties and add some comments – Magdalena Rowicka Jul 19 '21 at 13:58
  • Yes it is. Sorry for coming online late... Was having exams in college. The link you sent was blank and didn't display anything when I clicked it. Sad, you have to save it before sharing. I hope you still know what you wrote, you can click the save icon on the top right corner to save, then copy the link and share. Sorry to disturb, I appreciate your concern. – Richard Wilson Jul 19 '21 at 23:05
  • Maybe now? https://hastebin.com/dozipesezu.php As you can see we are in a different time zone so it doesn't matter ;) I hope you did well in the exam – Magdalena Rowicka Jul 20 '21 at 07:10
  • (data.getWeather().get(0).getIcon()) for getting the icon is not showing any error and the icons have shown on the firstfragment class. But the problem now is that I get the following new 3 errors: An enum switch case label must be the unqualified name of an enumeration constant and the value 'R.drawable. assigned to 'drawableResource' is never used and Variable 'drawableResource' is assigned but never accessed. I tried adding this code at the end of the last break; line - imageOfWeather.setImageDrawable(drawableResource); but they wrote 'cannot resolve symbol imageOfWeather' – Richard Wilson Jul 20 '21 at 09:24
  • 1
    change imageOfWeather to imageofWeather (that is your name reference), try add all case statement, add default icon to drawableResource e. g. `int drawableResource = R.drawable.sun_icon `. Rest of errors, please try research in google. I'm last code in Java like 4 year ago? Copy error and past to google I'm sure that you find something on stack – Magdalena Rowicka Jul 20 '21 at 12:45
  • Okay, thanks a lot for your help. But last question, please where do I add int drawableResource = R.drawable.sun_icon to my code. I've been trying to do that since – Richard Wilson Jul 20 '21 at 13:55
  • 1
    In your code onCreateView method and before check if data is not null you define drawableResource (int drawableResource;) change to some resource e. g. int drawableResource = R.drawable.sun_icon; – Magdalena Rowicka Jul 21 '21 at 06:32
  • I already wrote int drawableResource; before. My confusion is do I write int drawableResource = R.drawable.sun_icon; in the next line again? Or how do I do that – Richard Wilson Jul 21 '21 at 14:04
  • No no. Replace or don't write. – Magdalena Rowicka Jul 21 '21 at 17:32
  • Okay...Though it didn't fix, Thanks a lot for your help. I really appreciate, I will award the bounty to you in due time because you gave ample idea about how to achieve my aim. Then I'll look through the code and probably rebounty later after my exams, thanks a lot once again. – Richard Wilson Jul 21 '21 at 19:02
1

The line imageofWeather.setImageDrawable(drawableResource); shows this error:

Required type: Drawable, Provided: int

It means that setImageDrawable() requires (expects) a Drawable parameter, but the provided (found) parameter is an int.

To fix this, use setImageResource() instead which takes an int drawable resource instead of a Drawable

For each case statement, I get the following error:

The value R.drawable.sun(and the rest) assigned to 'drawableResource' is never used.

This is raised because of the previous error, where it considers that drawableResource is not used by the line of setImageDrawable(), and therefore it warns you that you assigned it a value but never used it.

This warning should be fixed by fixing the first one; Although, I'd suggest to rewrite the enum with a private constructor that takes in an int drawable resource value, where it'll handle the switch statement instead of making the fragment does that.

The new enum:

public enum WeatherIcon {

    Sun(R.drawable.sun),
    Cloud1(R.drawable.broken_clouds),
    Cloud2(R.drawable.few_clouds),
    Cloud3(R.drawable.scattered_clouds),
    Rain1(R.drawable.small_rain),
    Rain2(R.drawable.shower_rain),
    Thunder(R.drawable.thunderstorm),
    Snow(R.drawable.snow),
    Mist(R.drawable.mist);

    private int drawable;

    WeatherIcon(int drawable) {
        this.drawable = drawable;
    }

    public int getDrawable() {
        return drawable;
    }

}

Then instead of the switch statement in the fragment you can remove it and simplify it by getting the drawable using the enum's getDrawable() method as follows:

WeatherIcon icon = data.getWeather().get(0).getIcon();
int drawableResource = icon.getDrawable();
imageofWeather.setImageResource(drawableResource);
Zain
  • 37,492
  • 7
  • 60
  • 84
  • Okay. Thanks a lot for a detailed explanation of it. Having done what you suggested, it shows no error but the enum class gives this warning for each field: i.e Field 'Sun' is never used. – Richard Wilson Aug 03 '21 at 12:34
  • @RichardWilson It's just a compile time warning, because it sees that you don't directly use the `Sun` resource (or any other resource), i.e. you don't use `setImageResource(R.drawable.sun)` at compile time, because the value of the variable `drawableResource` doesn't actually being determined at compile time, but at runtime (i.e. when the app is launched); but actually at runtime you do use these resources via the variable `drawableResource` – Zain Aug 03 '21 at 13:21
  • Okay, when I ran the app like that, I got the following error at runtime: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.viz.lightweatherforecast.Retrofit.WeatherIcon.getDrawable()' on a null object reference – Richard Wilson Aug 03 '21 at 15:06
  • @RichardWilson So, turns out that `data.getWeather().get(0).getIcon()` is `null`.. Proabably it's nullable by the API.. How about using `if (icon != null) { int drawableResource = icon.getDrawable(); imageofWeather.setImageResource(drawableResource); }` ? – Zain Aug 03 '21 at 15:13
  • Now it's not showing any error, but also not displaying the icons. i.e a city showing light rain displaying nothing – Richard Wilson Aug 03 '21 at 16:35
  • This I believe it's because the fields are never used. They are coated with light ash instead of purple in the class which is normal with unused fields on Android – Richard Wilson Aug 03 '21 at 16:49
  • Can you show how the `data.getWeather()` looks like? – Zain Aug 03 '21 at 17:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235592/discussion-between-zain-and-richard-wilson). – Zain Aug 03 '21 at 17:34
0

You have to make activity-alias in your manifest file where you are setting icons as per weather conditions.

<?xml version="1.0" encoding="utf-8"?>
<manifest  xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.erikjhordanrey.livebinding">

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">

    <activity android:name="io.github.erikjhordanrey.livebinding.view.DcCharacterActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <activity-alias
        android:name=".MainActivityAlias"
        android:enabled="false"
        android:icon="@drawable/R.drawable.sun"
        android:label="@string/app_name"
        android:roundIcon="@drawable/R.drawable.sun"
        android:targetActivity="io.github.erikjhordanrey.livebinding.view.DcCharacterActivity">

        <intent-filter>

            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity-alias>

    <activity-alias
        android:name=".MainActivityAlias"
        android:enabled="false"
        android:icon="@drawable/R.drawable.broken_clouds"
        android:label="@string/app_name"
        android:roundIcon="@drawable/R.drawable.broken_clouds"
        android:targetActivity="io.github.erikjhordanrey.livebinding.view.DcCharacterActivity">

        <intent-filter>

            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity-alias>

    <activity-alias
        android:name=".MainActivityAlias"
        android:enabled="false"
        android:icon="@drawable/R.drawable.few_clouds"
        android:label="@string/app_name"
        android:roundIcon="@drawable/R.drawable.few_clouds"
        android:targetActivity="io.github.erikjhordanrey.livebinding.view.DcCharacterActivity">

        <intent-filter>

            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity-alias>

    <!--and so on....-->
</application>

</manifest>

In MainActivity file you have to change icons as per your weather conditions.

private void newicon() {
      
      // enable old icon
    PackageManager manager=getPackageManager();
    manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivity")
            ,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
      
      // enable new icon
    manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivityAlias")
            ,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
    Toast.makeText(MainActivity.this,"Enable New Icon" ,Toast.LENGTH_LONG).show();
}

you can find whole Article in this link : https://www.geeksforgeeks.org/how-to-change-app-icon-of-android-programmatically-in-android/

Yahya M
  • 392
  • 3
  • 13
0

Please try this code.

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
        // all field in Java should be type in camelCase, so not current_temp but currentTemp, not Cloud_out but cloudOut
        // Capitalize name is for class not field
        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);
        final ImageView imageofWeather = rootView.findViewById(R.id.imageView2);


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

        // And whenever the data changes, refresh the UI
        viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> {

            Drawble drawableResource; // default added in switch

            if (data != null) {
                current_temp.setVisibility(View.VISIBLE);
                current_temp.setText(data.getMain().getTemp() + " ℃"); // for that you can use strings resource and templates more in https://developer.android.com/guide/topics/resources/string-resource.html#formatting-strings
                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() + " %");

                Drawable drawableResource; // default icon is set in the switch
            switch (data.getWeather().get(0).getIcon())  {
                case WeaterIcon.SUN:
                    drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.sun);
                    break;
                case WeatherIcon.Cloud1:
                        drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.broken_clouds); //reference to drawable id
                        break;
                case WeatherIcon.Cloud2:
                        drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.few_clouds); //reference to drawable id
                        break;
                case WeatherIcon.Cloud3:
                        drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.scattered_clouds); //reference to drawable id
                        break;
                case WeatherIcon.Rain1:
                        drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.small_rain); //reference to drawable id
                        break;
                case WeatherIcon.Rain2:
                        drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.shower_rain); //reference to drawable id
                        break;
                case WeatherIcon.Thunder:
                        drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.thunderstorm); //reference to drawable id
                        break;
                case WeatherIcon.Snow:
                        drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.snow); //reference to drawable id
                        break;
                case WeatherIcon.Mist:
                        drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.mist); //reference to drawable id
                        break;

                //add a default of any background if error occurs
                default:
                    drawableResource = ContextCompat.getDrawable(requireActivity(), R.drawable.any_background);
                    break;
           
                }
             imageOfWeather.setImageDrawable(drawableResource);

            } 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);
    }
}
Basu
  • 763
  • 8
  • 27
  • After trying the code, I got the following two compile errors: 1. Cannot resolve symbol 'context' and 2. Branch in 'switch' is a duplicate of the default branch. – Richard Wilson Aug 03 '21 at 21:09
  • I have updated the code for error 1 (you were supposed to send the context). For error 2, check if any of the two weather icons is same. – Basu Aug 04 '21 at 19:18
  • I'm sorry but @zain has helped me already. But thanks anyways – Richard Wilson Aug 04 '21 at 21:04