Android:Exercises Resources

From Juneday education
Jump to: navigation, search

Preparation

We will assume you have project similar the one we created in the previous chapter. In the coming exercises we will refer to the project as Throwaway project.

Exercises

Move Button text to String resource

Instead of having the text of our first button hardcoded to Update we will switch to using a string resource. Create a string called update_text with the content Update.

Suggested solution

Expand using link to the right to see the full content.

Add the string in the file res/values/string.xml between the resources tags.

    <string name="update_text">Update</string>

This means that the entire string.xml looks similar to this:

<resources>
    <string name="app_name">Throwaway</string>
    <string name="update_text">Update</string>
</resources>

Use the string in XML

Ok, we now should a string resource update_text that we can use. So, let's do so. Switch the hardcoded the Update in the file activity_main.xml to use the newly created string resource update_text instead.

Note: the result will not be seen by the user.

Suggested solution

Expand using link to the right to see the full content.

In the file activity_main.xml you should change:

    <Button
        .....
        android:text="Update"
        ..../>

to

    <Button
        .....
        android:text="@string/update_text"
        ..../>

"Hello world!" as a resource

Let's create a resource for the text Hello World!. And then use it in the activity_main.xml.

Note: the result will not be seen by the user.

Suggested solution

Expand using link to the right to see the full content.

Add the string in the file res/values/string.xml somewhere between the resources tags.

    <string name="hello_message">Hello World!</string>

In the file activity_main.xml you should change:

    <TextView
        .....
        android:text="Hello World!"
        ..../>

to

    <Button
        .....
        android:text="@string/hello_message"
        ..../>

Use in code - Log

We can also, and of course, use the string resource (hello_message) in Java - not only in XML. Using getString you should log, using Log for logging. Add the Log message in the onCreate method.

You can find a bit of information and link about getString in the section: Android:concepts#Id Id

Suggested solution

Expand using link to the right to see the full content.

In the method onCreate, in the file MainActivity.java, you should add:

        Log.d(TAG, "Using update text for button: " + getString(R.string.update_text));

When running the app you should see something like

D/MyActivity: Using update text for button: Update

in the Android_Monitor.

Add swedish translation

Add Swedish (or another language) text for the strings:

  • update_text
  • hello_message

Build and run the app. If your phone has been localized to english you will not see any changes. Using the Settings you should be able to switch language.

Suggested solution

Expand using link to the right to see the full content.

Create a directory called res/values-sv. Copy the file string.mxl from res/values into the new directory. We selected the file string.xml and pressed Ctrl-c and Ctrl-v. In the window that pops up we kept the name string.mxl and used a directory ending with res/values-sv. The file now looks like this:

<resources>
    <string name="app_name">Throwaway</string>
    <string name="update_text">Update</string>
    <string name="hello_message">Hello World!</string>
</resources>

So let's turn it into Swedish.

<resources>
    <string name="app_name">Throwaway</string>
    <string name="update_text">Uppdatera</string>
    <string name="hello_message">Hallå världen!</string>
</resources>

Note: The authors are not happy with how Android Studio presents this new directory, but we will not complain since you can use the translation editor.

Note: When creating a directory in Android Studio it is not seen in the program (Android Studio) but it can be seen by the operating system. If you create a translation using Android Studio's string manager a directory is created with different strings.xml files for different languages. These are editable


Resourcify the text "Don't push it"

Create a resource for "Don't push it"

In the file activity_main.xml you should see something like this:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/stupidButton"
        android:text="Don't push it!"
        android:onClick="onStupidClick"
        app:layout_constraintTop_toBottomOf="@id/button"/>

Turn the text:

        android:text="Don't push it!"

into a string resource. This time you should use the Translations Editor. Select the file res/values/string.xml and chose Open Translations Editor. Do the following steps:

  1. Press the "+" (Add Key)
  2. Enter the key: dont_push_text
  3. Enter the default vale: Don't push it
  4. Keep the resource folder

You should now have the key.

Note: do not add a Swedish translation.

Expand using link to the right if you want to see the content of the file res/values/string.xml.

<resources>
    <string name="app_name">Throwaway</string>
    <string name="update_text">Update</string>
    <string name="hello_message">Hello World!</string>
    <string name="dont_push_text">Don\'t push it</string>
</resources>

Use the resource

In the file activity_main.xml you should use the resource dont_push_text.

Suggested solution

Expand using link to the right if you want to see the Button definition in the file activity_main.xml.

   <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/stupidButton"
        android:text="@string/dont_push_text"
        android:onClick="onStupidClick"
        app:layout_constraintTop_toBottomOf="@id/button"/>

Switch between languages

When switching between languages (Swedish and English in the case of the authors) you should see the texts update_text and hello_message change but the the dont_push_text is kept between the language switches. Why?

Answer

Expand using link to the right if you want to see an answer.

Since Android can find translation for update_text and hello_message and uses them. Android can't find a translation for dont_push_text and uses the default.

You can see this quite easily if you add some Log messages the method onCreate in MainActivity.java:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Using update text for button:    " + getString(R.string.update_text));
        Log.d(TAG, "Using hello text for textview:   " + getString(R.string.hello_message));
        Log.d(TAG, "Using dont push text for button: " + getString(R.string.dont_push_text));
    }

When running the app you see something like this in the Android Monitor when using Swedish:

03-07 10:38:51.071 17283-17283/se.juneday.throwaway.throwaway D/MyActivity: Using update text for button:    Uppdatera
03-07 10:38:51.071 17283-17283/se.juneday.throwaway.throwaway D/MyActivity: Using hello text for textview:   Hallå världen!
03-07 10:38:51.071 17283-17283/se.juneday.throwaway.throwaway D/MyActivity: Using dont push text for button: Don't push it

and after having switched to English

03-07 10:41:03.611 17283-17283/se.juneday.throwaway.throwaway D/MyActivity: Using update text for button:    Update
03-07 10:41:03.611 17283-17283/se.juneday.throwaway.throwaway D/MyActivity: Using hello text for textview:   Hello World!
03-07 10:41:03.621 17283-17283/se.juneday.throwaway.throwaway D/MyActivity: Using dont push text for button: Don't push it

Swedish "Don't push it"

You shoud also add a Swedish text for the same resource. One more time, select the file res/values/string.xml and chose Open Translations Editor. Click on the empty swedish text for dont_push_text and enter "Tryck inte".

When switching between languages (Swedish and English in the case of the authors) you should see the texts update_text and hello_message change as well as the the dont_push_text. Why?

Answer

Expand using link to the right if you want to see an answer.

Since Android can find translation for update_text and hello_messgae and uses them. Android can now find a translation for dont_push_text and uses that.

You can see this quite easily if you add some Log messages the method onCreate in MainActivity.java. See the previous question's suggested solution to see the code for that.

When running the app you see something like this in the Android Monitor when using Swedish:

03-07 10:44:01.011 25646-25646/se.juneday.throwaway.throwaway D/MyActivity: Using update text for button:    Uppdatera
03-07 10:44:01.011 25646-25646/se.juneday.throwaway.throwaway D/MyActivity: Using hello text for textview:   Hallå världen!
03-07 10:44:01.011 25646-25646/se.juneday.throwaway.throwaway D/MyActivity: Using dont push text for button: Tryck inte

and after having switched to English

03-07 10:44:32.661 25646-25646/se.juneday.throwaway.throwaway D/MyActivity: Using update text for button:    Update
03-07 10:44:32.661 25646-25646/se.juneday.throwaway.throwaway D/MyActivity: Using hello text for textview:   Hello World!
03-07 10:44:32.661 25646-25646/se.juneday.throwaway.throwaway D/MyActivity: Using dont push text for button: Don't push it

Add int for button press threshold

So far, we've added string resources. Now it's about time to add an int. The idea behind the int is to use that to not switch the "Update" text until pressed 10 times. Keeping the threshold (10) in a resource makes your easier to maintain - no changes in the code if you want to change the threshold.

So let's a an int resource called update_change_threshold and assign it the value 10. Select the values directory in Android Studion, right-click and chose New->Value resource file. In the new window fill in:

  • File name: integers

and press OK. In the new file add:

    <integer name="update_change_threshold">10</integer>

Your file (res/values/integers.xml) should now look something like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="update_change_threshold">10</integer>
</resources>

Log the value of the threshold

Log the value of the new integer resource, using Log, in the method onCreate in the file MainActivity.java.


Expand using link to the right to see a hint.

Check out the page More Resource Types to get an idea of how to access the value. You need to get a reference to a resource, using getResources() for your app and the use getInteger passing R.integer.update_change_threshold as argument.


Suggested solution

Expand using link to the right to see the full content.

You can see this quite easily if you add some Log messages the method onCreate in MainActivity.java:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Using update text for button:    " + getString(R.string.update_text));
        Log.d(TAG, "Using hello text for textview:   " + getString(R.string.hello_message));
        Log.d(TAG, "Using dont push text for button: " + getString(R.string.dont_push_text));
        Log.d(TAG, "Threshold:                       " + getResources().getInteger(R.integer.update_change_threshold));
    }

Alter code to only change button text after threshold times

Add an int instance variable to your MainActivity class. Use that variable to count the number of clicks and when reaching the threshold value (update_change_threshold) update the text as before.

Suggested solution

Expand using link to the right to see the full content.

Add the following to your class:

    private int updateChangeCounter; //  initialised to 0 since an instance variable

and then change the method onUpdateClick to:

    public void onUpdateClick(View v) {
        Log.v(TAG, "View: " + v);
        updateChangeCounter++;
        Log.d(TAG, "update count : " + updateChangeCounter + " (" + getResources().getInteger(R.integer.update_change_threshold) + ")");
        if (updateChangeCounter >= getResources().getInteger(R.integer.update_change_threshold)) {
            ((Button) v).setText("Wow!");
        }
    }

Links

Books this chapter is a part of

Android - the practical way

Book TOC | previous chapter | next chapter