Using the standard Android icons in your own application

Source code

When you make your own Android application, you usually put also your own graphic elements in it like custom icons and images, but it’s still a good idea to keep the standard Android icons for common user interactions. This helps the user to immediately understand the meaning of the options inside your application and it gives a familiar environment that is consistent with the Android platform. So, how do you get the standard Android icons? How do you deal with different Android versions (the style of the icons might change)?

There are two different options to accomplish this task:

  1. reference the icons with android.R.drawable;
  2. copy the standard Android icons from the SDK to your own application’s resources folder.

OPTION 1 – The good points about this option is that you automatically have the correct resource for the platform version where your application is running and the size of the application package doesn’t become higher in case you’re using more standard Android icons. This looks like the perfect solution, so why look for another one? Well, not all the icons are defined in android.R.drawable so you are forced to copy them locally in case you can’t find them in the class. Another bad point is what is explicitly stated in the guidelines documents for the menu icons and the status bar icons:

Warning: Because these resources can change between platform versions, you should not reference these icons using the Android platform resource IDs (i.e. status bar icons under android.R.drawable). If you want to use any icons or other internal drawable resources, you should store a local copy of those icons or drawables in your application resources, then reference the local copy from your application code. In that way, you can maintain control over the appearance of your icons, even if the system’s copy changes.”

OPTION 2 – You can find all the standard Android icons in your SDK local installation following the path ${ANDROID_SDK_INSTALL_DIRECTORY}\platforms\android-${PLATFORM_VERSION}\data\res, so let’s say that you installed the SDK under C:\AndroidSDK and you want the resources for the version 7 of the Android platform (i.e. Android 2.1), you can find everything under C:\AndroidSDK\platforms\android-7\data\res. You can copy for example the hdpi version of the icons from C:\AndroidSDK\platforms\android-7\data\res\drawable-hdpi. Following this approach, you need to copy the icons for different versions of the Android platform in case their style changed between one version and another. This is what happened for example for the menu and status bar icons, since Android 2.3 (platform version 9) the style changed, so you need a copy of the standard icons for Android 2.3 and higher and for Android 2.2 and lower. To do this, in your own application you need two different folders (this example refers to the hdpi icons):

/res/drawable-hdpi (for Android 2.2 and lower)
/res/drawable-hdpi-v9 (for Android 2.3 and higher)

Now let’s see a simple example application that uses the standard icons getting them in both ways. Here is the layout of the main activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Reference to android.R.drawable in the XML layout file:"/>

	<ImageView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:src="@android:drawable/ic_menu_add"/>

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Reference to android.R.drawable in the source code:"/>

	<ImageView android:id="@+id/img1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Reference to a local copy of the icon in the XML layout file:"/>

	<ImageView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:src="@drawable/ic_menu_add"/>

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Reference to a local copy of the icon in the source code:"/>

	<ImageView android:id="@+id/img2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>

</LinearLayout>

And here is the source code for the MainActivity class:

package com.devahead.standardandroidicons;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		// Set the image source referencing android.R.drawable
		ImageView img1 = (ImageView)findViewById(R.id.img1);
		img1.setImageResource(android.R.drawable.ic_menu_add);

		// Set the image source referencing a local copy of the icon
		ImageView img2 = (ImageView)findViewById(R.id.img2);
		img2.setImageResource(R.drawable.ic_menu_add);
	}
}

In this application I used the ic_menu_add standard Android icon. For this example to work, we need to put a copy of the original icon ic_menu_add.png from C:\AndroidSDK\platforms\android-8\data\res\drawable-hdpi to our local /res/drawable-hdpi and another copy from C:\AndroidSDK\platforms\android-9\data\res\drawable-hdpi to our local /res/drawable-hdpi-v9. In this way, the application will load the correct version of the local icon depending on the platform version, so for Android 2.3 and higher, the copy under /res/drawable-hdpi-v9 will be automatically chosen.

In the following images you can see the final result. If you run the application on Android 2.1, you see that there’s a glow around the icon while on Android 2.3 there is no outer glow. That’s because the style changed starting from Android 2.3 as it is written in the menu icons design guidelines, so our example application loads the correct icon depending on the platform version.

Example application running on Android 2.1:

Example application running on Android 2.3:

The option you choose to load the standard Android icons is up to you. With this post I just wanted to show the different possibilites, but they all have some good and bad points that must be evaluated to choose what’s right for you.

You can find the source code of the example application following the link on top of the post.

Comments

Smithc449
Reply

I am truly thankful to the holder of this website who has shared this fantastic article at here. kcakdbkfdgekfckg

Smithe527
Reply

I’m hunting for web sites that contain fantastic recommendations on what’s in fashion and just what top rated makeup products is.. eegefdeekakbcfab

GeorgeMn
Reply

I exclaimed she or he be the ideal defensive player contained in the gathering provided that she was located on the workforce if you are a younger. dreamingofjerusalem.com

cash
Reply

Hello!

for
Reply

Hello!

fax
Reply

Hello!

easy
Reply

Hello!

cost
Reply

Hello!

low
Reply

Hello!

Leave a comment

name*

email* (not published)

website