It's the little things
Thu, Apr 6, 2017It has been a little while since I’ve last worked on my little Android weather app, so I wanted to get back into it with a small, simple task. So, I decided to replace the text that describes the weather with little icons. Like so:
I did some StackOverflow searching to see how people map text to image resources. I found a reasonable solution here. However, instead of hardcoding the “format string” I added it to strings.xml
. Furthermore, there isn’t a one-to-one mapping from condition strings and icons. Consequently, I made a new resource file to map the formatted condition string to the actual drawable resource. A snippet of it here:
<item name="ic_flurries" type="drawable">@drawable/ic_weather_snow</item>
<item name="ic_fog" type="drawable">@drawable/ic_weather_cloud</item>
<item name="ic_hazy" type="drawable">@drawable/ic_weather_cloud</item>
I don’t have an icon for fog or haze, so I’m just using the cloud one for both. What my code ends up looking like then is:
Integer resourceId = getResources().getIdentifier(icon_str, "drawable", getActivity().getPackageName()); iconView.setImageResource(resourceId);
Notes from this experience:
- I need to hook up a keyboard shortcut for recording keyboard macros in Android Studio
- I still think there’s a better way to map these string values to the resource id. Maybe in a db?
- Android layouts still don’t make a ton of sense to me; I had to fuss a lot to rearrange the Views to be positioned how I wanted.
It really was such a small change, but I definitely appreciate how much better it looks now.
Icons made by Freepik from Flaticon is licensed by CC 3.0 BY