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 Build a Simple Android App (2014) Improving Our Code Simple Refactoring: Using a Class

Holly Fox
Holly Fox
2,808 Points

Can you move the facts to the String Resources?

My app was working until I tried to move all my facts to the Strings resources. I have tried to call the string ID's in my array but its not working. What am I doing wrong?

    public String[] mFacts = {
            Resources.getSystem().getString(R.string.fact1),
            Resources.getSystem().getString(R.string.fact2),
            Resources.getSystem().getString(R.string.fact3),
            Resources.getSystem().getString(R.string.fact4),
            Resources.getSystem().getString(R.string.fact5),
            Resources.getSystem().getString(R.string.fact6),
    };

1 Answer

They go into this in more detail in the Blog Reader project but in the meantime, here's the gist of it.

In 'strings.xml', put your facts inside the 'resources' tag like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array
        name="facts_array">
        <item>fact2</item>
        <item>fact3</item>
        <item>fact4</item>
     </string-array>
</resources>

Then in your activity, you can fill the facts array like so:

 public String[] mFacts = getResources().getStringArray(R.array.facts_array);

Hope this helps

Neat. I was wondering how to manage the facts in a group like this. I seem to have gone the long way with mine.

I have:

<string name="fact_ants">Ants stretch when they wake up in the morning.</string>
<string name="fact_ostriches">Ostriches can run faster than horses.</string>
<string name="fact_medals">Olympic gold medals are actually made mostly of silver.</string>
<string name="fact_bones">You are born with 300 bones; by the time you are an adult you will have 206.</string>
<string name="fact_light">It takes about 8 minutes for light from the Sun to reach Earth.</string>
<string name="fact_bamboo">Some bamboo plants can grow almost a meter in just one day.</string>
<string name="fact_florida">The state of Florida is bigger than England.</string>
<string name="fact_penguins">Some penguins can leap 2-3 meters out of the water.</string>
<string name="fact_habits">On average, it takes 66 days to form a new habit.</string>
<string name="fact_mammoths">Mammoths still walked the earth when the Great Pyramid was being built.</string>

calling them via:

     private int [] facts = {
            R.string.fact_ants,
            R.string.fact_bamboo,
            R.string.fact_bones,
            R.string.fact_florida,
            R.string.fact_habits,
            R.string.fact_light,
            R.string.fact_mammoths,
            R.string.fact_medals,
            R.string.fact_ostriches,
            R.string.fact_penguins
    };

            //and

    return context.getString(facts[randomNumber]);

I spent an exorbitant amount of time trying to figure out if I could grab the R.string.fact_* ID's dynamically, to no success.