Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Android Development
Previous Page Home Next Page

Using Resources in Code

Using resources in code is just a matter of knowing the full resource ID and what type of object your resource has been compiled into. Here is the syntax for referring to a resource:




Where resource_type is the R subclass that holds a specific type of resource. resource_name is the name attribute for resources defined in XML files, or the file name (without the extension) for resources defined by other file types. Each type of resource will be added to a specific R subclass, depending on the type of resource it is; to learn which R subclass hosts your compiled resource type, consult the Available Resources document. Resources compiled by your own application can be referred to without a package name (simply as R.resource_type.resource_name). Android contains a number of standard resources, such as screen styles and button backgrounds. To refer to these in code, you must qualify them with android, as in android.R.drawable.button_background.

Here are some good and bad examples of using compiled resources in code:

// Load a background for the current screen from a drawable resource.

// WRONG Sending a string resource reference into a 
// method that expects a string.

// RIGHT Need to get the title from the Resources wrapper.

// Load a custom layout for the current screen.

// Set a slide in animation for a ViewFlipper object.

// Set the text on a TextView object.
TextView msgTextView = (TextView)findViewByID(;
Android Development
Previous Page Home Next Page

  Published under the terms fo the Apache 2.0 License Design by Interspire