Alternative Resources - Android: Programming and App Development for Beginners (2015)

Android: Programming and App Development for Beginners (2015)

Chapter 4. Alternative Resources

When you write your application, it should have alternative resources that support certain device configurations. For example, there should be alternative drawable resources, like images, for devices that have different screen resolutions. There should be alternative string resources to support different languages. At runtime, the operating system will detect what the configuration of the device is and will load in the right resources for the application.

To specify alternatives that are specific to configuration for a resource set, you should follow these steps:

· First, in res/ you should create a new directory with the name in this format - <resources_name>-<config_qualifier>. In this case, resources_name can be any of the resources that we talked about in the last chapter – see the table at the end of the chapter. The qualifier is to specify a single configuration that the resources are going to be used for.

· Second, you should save the alternative resources into the directory you have just created. The files must be names the same as the default resource files that are shown in the following example although the content of each file will be specific to the alternative resource.

This example shows images that are specified for a default screen resolution, coupled with alternatives for a high-resolution screen:

· MyProject/

· src/

· main/

· java/

· MyActivity.java

· res/

· drawable/

· icon.png

· background.png

· drawable-hdpi/

· icon.png

· background.png

· layout/

· activity_main.xml

· info.xml

· values/

· strings.xml

Here is a further example that shows the layout for default languages and then an alternative for the Arabic language:

· MyProject/

· src/

o main/

o java/

o MyActivity.java

· res/

· drawable/

· icon.png

· background.png

· drawable-hdpi/

· icon.png

· background.png

· layout/

· activity_main.xml

· info.xml

· layout-ar/

· main.xml

· values/

· strings.xml

Accessing Resources

When you are developing your app, you will have to access resources that are defined, be they in your code or located in the layout XML files. This next section shows you how to access the resources in either way:

Accessing Resources in Code

When you compile your Android application R class is automatically generated. This contains all the resource IDs for all the resources that are in your res/ directory. The R class can be used to access the specific resource using the specific sub directory and the resource.

For example, if you want to access the res/drawable/myimage.png and then set an ImageView, you would use this code:

· ImageView imageView = (ImageView) findViewById(R.id.myimageview);

· imageView.setImageResource(R.drawable.myimage);

The first line of this code uses R.id.myimageview to get the ImageView that has been defined with the id myimageview and is in a layout file. The second line uses R.drawable.myimage to get an image that is called myimage from the drawable sub directory in the /res directory.

Another example shows res/values/strings.xml with this definition:

· <?xml version="1.0" encoding="utf-8"?>

· <resources>

· <string name="hello">Hello, World!</string>

· </resources>

You can now set the text for a TextView object that has the ID msg by using a resource ID as such:

· TextView msgTextView = (TextView) findViewById(R.id.msg);

· msgTextView.setText(R.string.hello);

The next example shows a layout res/layout/activity_main.xml with this definition:

· <?xml version="1.0" encoding="utf-8"?>

· <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

· android:layout_width="fill_parent"

· android:layout_height="fill_parent"

· android:orientation="vertical" >

· <TextView android:id="@+id/text"

· android:layout_width="wrap_content"

· android:layout_height="wrap_content"

· android:text="Hello, I am a TextView" />

· <Button android:id="@+id/button"

· android:layout_width="wrap_content"

· android:layout_height="wrap_content"

· android:text="Hello, I am a Button" />

· </LinearLayout>

The code will load the layout for an Activity as follows in the onCreate() method:

· public void onCreate(Bundle savedInstanceState) {

· super.onCreate(savedInstanceState);

· setContentView(R.layout.main_activity);

· }

Accessing Resources in XML

Look at this resource xml file res/values/strings.xml and note that it has a color and a string resource in it:

· <?xml version="1.0" encoding="utf-8"?>

· <resources>

· <color name="opaque_red">#f00</color>

· <string name="hello">Hello!</string>

· </resources>

You can now use these resources in the layout file below to set the text string and text color like this:

· <?xml version="1.0" encoding="utf-8"?>

· <EditText xmlns:android="http://schemas.android.com/apk/res/android"

· android:layout_width="fill_parent"

· android:layout_height="fill_parent"

· android:textColor="@color/opaque_red"

· android:text="@string/hello" />

Now go back through the chapter where we created the “Hello, World!” example and you should now have a much better understanding of all the concepts that have been used.

Conclusion

As you can see, app development for the Android platform isn’t too difficult, but you do have to put in the time on learning how to program and on getting to grips with all the different components. To be fair, the Android SDK and Eclipse are user-friendly and you shouldn’t have too much trouble in getting your first app developed and ready for the market.