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.
Related
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.
When I do changes to a layout resource file it's not updated/reflected when the apk file is buildt and installed from Android Studio 2.0 (Preview 3b).
Example:
I had a EditText and I added a TextInputLayout like this:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<EditText
android:id="#+id/edit_text_new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_your_new_password"
android:inputType="textPassword"
android:saveEnabled="false" />
</android.support.design.widget.TextInputLayout>
The result after updating the app is the same as before I added the TextInputLayout, just the EditText without TextInputLayout.
What I've tried:
Build/Clean Project
Build/Rebuild Project
File/Invalidate Caches / Restart..
Uninstall app first
Turn off Instant Run
I suspect this is probably a bug with the Preview 3b version of Android Studio 2.0 causing this. Any ideas? Maybe it's just a settings/configuration?
Temporary solution:
If you make a copy of the layout file and inflate it instead. Then the changes are updated in the app? But this is not ideally the best solution!
Also make sure you make the changes in the layout-v<target-api> folder if you have set specific layout for specific api level. For example layout-v23 in mine case.
This issue is typically caused by only updating the xml file in one layout folder and not the other folders containing the file's compatibility variants as well. For example, only editing the xml file in the layout system folder and not the layout-v14 folder too will cause this issue.
The fix: update the instance of the layout xml file not only in your layout folder, but the instances in all other layout folders too (layout-v14, layout v-21, etc.)
I wasn't able to reproduce this issue. I tried the same layout changes with Preview 3b and the changes showed up in the emulator fine. I simply hit the run button again.
Was this working for you in a previous version of Android Studio?
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.
I'm modifying the source code of a terminal emulator program for Android that contains the following confusing xml code in the linear layout of main.xml:
<com.vtrandal.bluesentry.EmulatorView
android:id="#+id/emulatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
This same xml code also appears in another xml file also in /res/layout along with main.xml. I can see that it appears to begin with a custom tag created using my package name. But why? How is this possible and why would someone want something like this?
When you extend a View to create a custom component, you need a way to add this objects to the xml layout. You do that by using the full class name, including the package name. The link to the dev guide the Nicholas posted previously explains it.
The attributes used inside that tag can be either attributes used for the View you are extending, or custom attributes defined in a new schema.
You can follow a nice tutorial about this here: http://www.anddev.org/creating_custom_views_-_the_togglebutton-t310.html
This is the syntax for creating custom components and views in Android.
Not sure what exactly is confusing. I'll assume its com.vtrandal.bluesentry.EmulatorView
part. This is name of the class that will be instantiated when inflating xml file.
If you got used to something like this:
<TextView
android:id="#+id/emulatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
Then you should know that this is because TextView is located in android.widget, and you should've wrote this:
<android.widget.TextView
android:id="#+id/emulatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
But because inflater knows about several special packages (android.widget, android.view, android.webkit) you can skip them in xml.