53

Android comes with lots of system resources (android.R) that can be used to save you time and make your application lighter.

For example, I recently discovered that Android provides localized strings for Yes (android.R.string.yes), No (android.R.string.no), Cancel (android.R.string.cancel) and Ok (android.R.string.ok), among other strings.

What other system resources do you recommend using? Or is there a reason to avoid using system resources?

Edit: As noted by Tomas, some of this resources might not produce the results you would expect (particularly, android.R.string.yes/no returns OK/Cancel instead of Yes/No, as reported here). For greater control, you can copy system resources from the Android source code.

3
  • 2
    How do you access these from xml? Dec 19, 2010 at 19:19
  • "Or is there a reason to avoid using system resources? " - In case you are using system resources such as icons, it is not guaranteed that the same icon is available across all version of android, which may result in your app being inconsistent.
    – codeMan
    Jul 3, 2014 at 10:06
  • Voting to close as too broad / opinion based. Feb 8, 2016 at 20:25

4 Answers 4

58

You can find a full listing of all system resources in the android package.

Every time I want to do something on Android I check to see if there's a system resource that covers what I want to do. It is helpful to import the Android source code (in particular, their /res/ folder) when searching for already-implemented resources that you might want, so you can see their specific implementation.

Personally, I find myself most often using:

  • Built-in Android layouts for standard tasks, such as spinner dropdowns.
  • Android ids (android.R.id), because you are often required to use these if you want to use some of Android's widgets (for example, TabHost/TabWidget requires you to use "android:id/tabhost", "android:id/tabs" and "android:id/tabcontent" if you want to implement an XML layout).
  • Built-in colors, especially android.R.color.transparent.
  • Android's built-in fade-in and fade-out animations in android.R.anim.
6
  • 10
    Bear in mind that those resources ship with your SDK. Look in the data/res/ directory underneath a specific platform directory. I would be careful reusing things that might get changed by OEMs -- drawable resources would be one example problem area. Apr 13, 2010 at 18:26
  • @CommonsWare When running on an actual device, where are the resources loaded from?
    – ThomasW
    Oct 18, 2017 at 6:41
  • @ThomasW: Beyond "somewhere in the device firmware", I cannot say, as I have never gone looking for specifics. Sorry! Oct 18, 2017 at 11:38
  • 1
    Wow. There are places even @CommonsWare hasn't ventured.
    – ThomasW
    Jan 15, 2018 at 2:23
  • @ThomasW resources are loaded from resources.arsc file. When you build your apk aapt compiles your resources and generates resources.arsc file which shipped inside apk.
    – Bek
    Jul 2, 2019 at 8:58
18

You can access system resources from xml by qualifying them with the android package name, i.e. "@android:string/ok"

6

As CommonsWare mentioned, you don't have to download the Android source repository to inspect the resources; just go to <android-sdk-dir>/platforms/android-X.X/data/res/. However, if you start using these you will quickly become disappointed to find that most of them are not available through the android.R class. Most of the time I have to import them into my Eclipse workspace anyway. :-(

My favorite resources are the drawables that show differently based on a control's focused/pressed/disabled states (like android.R.drawable.btn_default). I use those or tweak them to create custom buttons for my apps. As you can see if you look at /data/res/drawable/btn_default.xml, they are defined as a <selector> XML element, which gets inflated into a StateListDrawable at runtime. You could also create your own StateListDrawables and they're super useful, both as View backgrounds and as a button's "compound drawable" (see TextView.setCompoundDrawables()).

1
  • I wonder, when running on an actual device, where are the resources loaded from?
    – ThomasW
    Oct 18, 2017 at 6:41
3

Please note that the translations are really bad.

For example the German "no" in android-25 is "Abbrechen" which actually means "cancel". "Yes" is translated as "OK"...

I'm not sure how good the other languages are, so I would use an own translation.

2
  • It's not that the translations are bad. As OP noted in his question, it's that the android.R.string.yes/no returns OK/Cancel instead of Yes/No. This is likely because the developer thought android.R.string.yes/no was more concise than android.R.string.ok/cancel.
    – Patrick
    Sep 13, 2017 at 21:07
  • 3
    Also, android.R.string.ok doesn't appear to be translated. It is "OK" for every language I've tested. (For some languages, "OK" is not appropriate.)
    – ThomasW
    Oct 19, 2017 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.