android.R -vs- R


#1

Just a quick note - I think it might be good to warn users in a callout about the difference between R and android.R - and not to accidentally import android.R when writing the CrimeListFragment. You do mention referencing one of the list item built-in layouts earlier in the book, but when writing the sample, IDEs try to auto import classes… That can be confusing for the beginner.


#2

Is that something IntelliJ is trying to do? I think we talk a bit about that for Eclipse, but I don’t think Eclipse is quite as aggressive about autoimport as IntelliJ is.


#3

Yes, it is… I am using the AndroidStudio instead of eclipse on this and it is more aggressive - for some reason it wants to import android.R even though the package-less R is already visible. I lost a few hours trying to figure this out until I realized the import was up there.

Ken


#4

Thanks for the feedback. We’ll take note of that for the next edition.


#5

In Eclipse, if your use ctl-shift-O to have the imports added, or you put your mouse cursor over the R in the listing (when it still has a red underline), you get the option to add android.R or com.bignerdranch.Android.criminalintent.R. At first, I was a tad confused until I saw that Eclipse automatically created the package com.big… under gen to put the project’s R.java file in. Later on, we actually use something in the system android.R file.

Another thing that took me a while to figure out (can’t remember what chapter this was in), is where the default themes and styles are, so we can see what other things we might want to change. I was able to find these links:

Android - Styles and Themes: http://developer.android.com/guide/topics/ui/themes.html
Android: Accessing Resources: http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromXml
Android - themes.xml: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml
Android - styles.xml: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml

I am still unsure how we would actually customize things tho. Hopefully, a future chapter covers this better?


#6

Eclipse will also silently import android.R if com.mypackage.R is not available for some reason. This doesn’t happen as often as it used to in Eclipse, thankfully, but apparently might once again pose a problem in IntelliJ.

I wish that our coverage of custom styles and themes were better than it is. It’s on my list of things to improve in the future.


#7

I had this problem (in Android-Studio/intelliJ) recently as well. As noted before, it’s a known bug.
It (along with some other pesky bugs) seems to have disappeared with the 0.1.8 update.

Google appears to be working really hard and fast to get Studio ready for prime time, but in the meantime, remember that it’s just a “preview”.