Search code examples
androidr.java-file

When would I use android.R over my local R.?


If I want to set content view then I use:

setContentView(R.layout.test);

But when I set a theme, why do I have to add android to the beginning of R.style.x?

setTheme(android.R.style.Theme_Light);
setContentView(R.layout.test);

Solution

  • android.R is a reference to the Android R.java file. It is not custom to your app. It contains a reference to every style, resource, anything that you would ever use in an Android app.

    On the other hand, R.x (without the android prefix) refers to your local app. It contains specific styles, resources, etc. that have been defined for your app, and is much more specific.

    So as to your question of which to use:

    Use android.R if you want to reference a standard style. This would be something like android.R.style.holo. It could also be animations, but keep in mind these are not custom, these are the building blocks.

    Use R.x when you are declaring something local. This will be something that you have definded, such as R.style.MyTheme or R.layout.MyLayout.