I understand that in Android anything I place in my res/ directory will get compiled into the appropriate R class:
res/drawable -> R.drawable
res/layout -> R.layout
etc.
I know I can also use items that are in the android package:
android.R.id.text1
If I want to use my resources in xml I can do the following:
<TextView
...
android:text="#string/my_text" />
If I want to use something in the android package I can do the following:
<TextView
...
android:id="#android:id/text1" />
This doesn't always seem to be the case and I cannot figure out the distinction.
When using AppCompat Themes:
<TextView
android:theme="#style/Theme.AppCompat.Light"
android:textAppearance="#style/TextAppearance.AppCompat.Subhead" />
instead of
<TextView
android:theme="#android:style/Theme.AppCompat.Light"
android:textAppearance="#android:style/TextAppearance.AppCompat.Subhead" />
Theme.AppCompat.Light and TextAppearance.AppCompat.Subhead are not defined anywhere in my res/ directory. They are included as part of the support.v7.appcompat support library. Why don't I need to use the android prefix in this case?
Because they are part of your app, not part of the framework. It's not that you don't need it; if you try it, you will get a compile error.
The fact that appcompat-v7 happens to be a library is immaterial. It is no different than if you typed in the code yourself. Anything that is in your app does not use the android prefix. Resources that are part of the framework — and therefore are on the device, not in your app — get the android prefix.
Related
I'm trying to customize Android 10 AOSP Settings app (com.android.settings) using a Runtime Resource Overlay (RRO).
When using a RRO, I can successfully customize any string in Settings app by putting a new value in the RRO's file ./res/values/strings.xml.
However, I cannot manage to overlay not a string but a layout by putting the layout file in the RRO's ./res/layout/ folder.
The RRO gets built, but the ids are different than the ones in the original layout, so after installing the RRO on device, the Settings app will fail to find the elements it expects in the layout file using findViewById().
For instance, I create a RRO containing the following bluetooth_pin_confirm.xml layout file (copied from Settings app source code and simplified) in its res/layout/folder:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/pairing_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
...
</LinearLayout>
After installing the RRO on device, the Settings app will successfully inflate this file in BluetoothPairingDialogFragment.java
View view = getActivity().getLayoutInflater().inflate(R.layout.bluetooth_pin_confirm, null);
But then it won't be able to find the views by id:
TextView pairingViewCaption = (TextView) view.findViewById(R.id.pairing_caption);
R.id.pairing_caption cannot be found so pairingViewCaption will be null
So it seems that the IDs in the layout file from the RRO are different than the original ones.
Indeed, when running aap2 dump on Settings app apk I can see:
resource 0x7f0a02e0 com.android.settings:id/pairing_caption: t=0x12 d=0x00000000 (s=0x0008 r=0x00)
While in the RRO I can see:
resource 0x7f010000 com.foo.settings:id/pairing_caption: t=0x12 d=0x00000000 (s=0x0008 r=0x00)
So yes, the Ids are different.
Now, it should be the whole point of the RRO when overlaying a layout to be able to match the Ids so to keep original Ids but change the layout around them. But how?
I've played a bit with aapt2 options --emit-ids and --stable-ids to try to force generated id to be equal to the one in Settings.apk, but that fails miserably as well:
error: can't assign ID 0x7f0a0303 to resource com.foo.settings:id/pairing_caption with conflicting ID 0x7f010000.
Note that this issue is specific to Android 10 and earlier. It seems that in Android 11 there is a new & better way to define mapping between ids in target and overlay package.
I'm pretty stuck here. Any suggestion would be greatly appreciated
On the link rro
you can find the following
Note: If you overlay a layout file, make sure all the IDs and app namespace attributes are included in both overlays.xml and overlayable.xml. For example:
<overlay>
<item target="layout/car_ui_base_layout_toolbar"
value="#layout/car_ui_base_layout_toolbar" />
<item target="id/car_ui_toolbar_background"
value="#id/car_ui_toolbar_background" />
<item target="attr/layout_constraintTop_toBottomOf"
value="#attr/layout_constraintTop_toBottomOf" />
</overlay>
Surely I used #id after declared #+id and my App runs well with my code, what confused me is that when I preview my xml files, the reference #id seems not work at all and the layout was in my library project not main,which was dependenced by aar.
If I change the layout_below="#id/tv_1 to layout_below="#+id/tv_1, the preview will work well.
Or I declare it in id.xml files id my library proj,and use #id/tv_1, preview works well too.
all solutions above seems make no difference after I run my main project,they all works well. I just can't understand why preview has problems,and how the '+' works when preview a xml file.
Examples:
<RelativeLayout ..>
<TextView android:id="#+id/tv_1" ../>
<EditText android:id="#+id/et_1"
android:layout_below="#id/tv_1" ../>
</RelativeLayout>
#+id creates a new entry to R.java class which stores and identify assets.
#id simply refers to the previous created assest.
I was trying to use an icon from this page in my layout as follows
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_backspace_black_24dp"
/>
but it could not be rendered because of an error
Color value '#drawable/ic_backspace_black_24dp' must start with #
I found two related issues in SO here and here which did not help resolve the issue.
There is no dash ('-') in the filename, the file-format is a 'png' and there is no res/color directory in my project.
All my colors defined in colors.xml start with a #.
So how to fix this error?
Simply rename your file name by removing the numbers and only keep lower case letters or underscores.
Problem solved: I forgot to remember that the android sources on the internet are really outdated, and almost none of the documentation works as-is!
The folder res/drawable as suggested in the android developer guide is deprecated! The folder is now named mipmap! So the simple solution is to have the following layout to fix the problem:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_backspace_black_24dp"
/>
Never trust the android developer documentation ... !
Try removing the '_" underscores and make the name shorter. Not sure why but that worked for me.
Check the file drawable/ic_backspace_black_24dp.
If you downloaded the SVG version you will able to open as a text file. Check if the android:fillColor has the #.
Anyway if you are using Android Studio those icons can be downloaded very easily using Vector Asset Studio:
Right click in drawable
new
vector assets
choose the icon you want
You have more info of this feature here.
So I'm trying to decide whether it would be worth it to refactor out my current use of id's in all of my android layouts to an ids.xml file, or to just leave my view code the way it is (with multiple views sharing ids and both views using "#+id/id_name).
Is there a significant compile/runtime performance benefit to refactoring out the ids to the ids.xml file? How about if the application gets bigger?
Related resources:
http://developer.android.com/guide/topics/resources/more-resources.html#Id
Thank you for your time.
I used <item type="id"> resources in my app because I have TextEdit views that serve a similar purpose in more than one Activity.
ids.xml has the following advantage: all ids were declared, so compiler can recognize them. If something like this:
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBelow="#id/text2"
android:text="...."/>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="...."/>
Can result in compiling error because text2 was refered before declared
After taking a look at theming for Fede's UberMusic I came across the file album_screen.xml. Below is the source of that file. Basically I noticed that his themes have the ability to use custom views that are a part of his application, and that they have this XML namespace at the top theme. I am missing the connection as to how he is able to apply his attributes to views that he does not control, and how Eclipse will compile the cod below without fail. I placed the URL http://schemas.uberdroidstudio.com/theme into my browser's address bar but nothing came up, and I cannot figure out where/ how Eclipse knows the attributes that the namespace supports. Thank you ahead of time for your help.
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:theme="http://schemas.uberdroidstudio.com/theme">
<TextView
android:id="#id/artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
theme:textFilter="uppercase" /> <!-- This is the interesting line -->
I suspect that the theme:textFilter="uppercase" line isn't actually having an effect on the (apparently vanilla) TextView.
As for the URL of the namespace, it is interesting that you can't access it, since it does not appear to be a local styleable (Android would have you refer to a local styleable namespace as http://schemas.android.com/apk/res/your.package). +1 for novelty.
The solution is actually not as complicated as I originally thought. XML namespaces are arbitrary strings that just need to be unique. If your namespace does not start with http://schemas.android.com/apk/res then it is not validated and the APK package is not checked for declare-styleable or the like.
Now a custom namespace if very simple to implement, as illustrated by this code snippet in GitHub. Finally, applying custom XML attributes to "normal" widgets can be accomplished by using the LayoutInflater.Factory and calling setFactory before you inflate your views. Throw it all together and you have a highly theme-able, XML driven application like Fede's UberMusic.