Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

firnaz luztian adiansyah
firnaz luztian adiansyah
5,295 Points

Stormy app - How to change temperature from F to C for the whole app using buttons that are located in MainActivity?

Hi so I customized my Stormy app and added more features like runtime permission, location manager and material design but I'm stuck in this F to C conversion

Basically, in MainActivity I have 2 buttons "Fahrenheit" and "Celcius" When the button is clicked I was able to change getTemperature() and use simple calculation to convert it into celsius. However when I navigate into Hourly or DailyActivity, the value is still in Fahrenheit. So basically only getTemperature() value in MainActivity that is changed. Is there an easy way to do this?

I know we can just create buttons into those activities as well but I want only 2 buttons in the MainActivity not in every activity. I saw one of the students have done it but I have no idea how?

Please check my project in google play to see the screenshot of my UI https://play.google.com/store/apps/details?id=firnazluztian.BlueStormy

3 Answers

Rebecca Rich
PLUS
Rebecca Rich
Courses Plus Student 8,592 Points

You could use Shared Preferences to set which temperature preference there is for the entire app (which could be edited by the Main Activity), and then use the shared preferences values across the app to adjust temperatures. Here is some information on how to use Shared Preferences:

http://www.tutorialspoint.com/android/android_shared_preferences.htm

firnaz luztian adiansyah
firnaz luztian adiansyah
5,295 Points

Thank you, Rebecca! I will give this a try

I see you can store a value with this Shared Preference but what about Content Provider and SQLite? whats the difference between them? I mean all of them are for storing data right? sorry I'm new to android

David Gómez
David Gómez
4,389 Points

Good point, Firnaz, all people know that C users hate using F, and viceversa. The buttons to change it are the solution!

David Gómez
David Gómez
4,389 Points

Besides doing what Rebecca already said, the API docs indicate in the section "Options" how to modify the request. It contains the different units that can be used (celsius, meters, kilometers, miles, etc.) https://developer.forecast.io/docs/v2 I found that just 10 minutes ago, so I'll be checking here in case someone knows how to include that change in the parameters into the Main Activity (I want to customize the app too!).

firnaz luztian adiansyah
firnaz luztian adiansyah
5,295 Points

You can do that too but it will only change the default temperature value Some Americans only read Fahrenheit and the rest of the world are using Celcius I don't want to receive bad comments from both sides :) So I want users to have control over it so they can change the temperature from F-C or C-F anytime within the app