This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between “#id/” and “#+id/” in Android
When you create a layout XML file for Android applications, you usually declare the ID of each layout element as:
#+id/elementID
Don't you? I guess the "+" means that this element's ID is just created and therefore you need the plus, right?
But what do you have to do when you refer to a layout element before it is created? Do you refer to it with "+" and then create it without "+"? Simply put, is the following code correct (in a RelativeLayout container)?
<ImageButton
android:id="#+id/helpButton"
android:layout_toLeftOf="#+id/moreButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#id/moreButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
I guess the "+" means that this element's ID is just created and therefore you need the plus, right?
Yes.
Do you refer to it with "+" and then create it without "+"?
Yes. The first occurrence of the ID gets the +. Second and subsequent occurrences can leave it off.
Simply put, is the following code correct (in a RelativeLayout container)?
Well, your ImageButtons are missing images... :-)
That being said, your use of the + sign there seems fine.
Related
So lastly I have had to rebuild not my XML layout file for android app. And I saw constructions like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/rev_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rev_arrow">
<!-- some stuff in here -->
</LinearLayout>
<ImageView
android:id="#+id/rev_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
/>
</RelativeLayout>
It is really annoying when I see that someone using #+id in not android:id attribute. When any developer will want to search this id he will find LinearLayout first instead ImageView.
My question is why google allow it? Is there any particular reason for it and I just didn't know it?
Sorry for my poor English.
Yes, there is a reason:
Sometimes, you have to set a View A relative to the position of a View B which is declared later in the XML-file (which is the case in your example. The LinearLayout is "View A" and the ImageView is "View B").
Imagine, the code you've got a problem with
android:layout_below="#+id/rev_arrow"
would look like this instead:
android:layout_above="#+id/rev_arrow"
The android:layout_above would be useless if you couldn't declare an id inside it.
Because it was mentioned in a few comments:
You have to use the "plus"-sign always at that place, where the id is first declared in the layout-file (from top to bottom). It is independet from the attribute, like "id" or "layout_below".
This is a valid use of the ID, as it tells the layout manager that the view identified by id/rev_main view is to be placed below the view identified by id/rev_arrow.
So in places other than android:id, the ids are used as references to views identified by the respective id.
I am a first time developer for Android, so you can say I've been learning as I was developing. For most of my code that doesn't have to do with the XML layout, I had no problem patching my rookie mistakes. With that said, my rookie mistakes has caught up to me in regards to two TextViews when I initially designed them with the GUI interface designer (my major rookie mistake).
My display_city tv and display_today_date tv seem to have a symbiotic relationship with each other. Removal of either one would crash the app. They seem so dependent on each other that changing each other's positioning is impossible (at least from the myriad of things I have tried such as setting layout margins).
<TextView
android:id="#+id/display_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dip"
android:layout_above="#+id/display_today_date"
android:layout_below="#+id/get_data"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/display_today_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/display_pollen_type"/>
My question is - how do I simply position display_today_date immediately after my display_city? When I first started this Android app, I relied a lot on the GUI builder. That was my first rookie mistake, which resulted in this symbiotic relationship I explained.
Currently this is what my app looks like:
I have tried changing display_today_date's layout to android:layout_below="#+id/display_city. This results in a crash. I checked logcat, but it did not give me relevant information to the reason of the crash within the XML file.
P.S. get_data is my TextEdit box.
You already have the city to show above the date with the line android:layout_above="#+id/display_today_date". You can't have 2 views in a relative layout each reference the other, or it won't be able to figure out what to do. If you don't want to put the city above the date, delete that line then add the code to place it where you want.
You could use a LinearLayout with the orientation set to horizontal. That way there is no reference to another view. So if you delete one the other one won't cause the app to crash.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is different between #+id/android:list and #id/android:list ??
What is the difference between #id/.. and #+id/..? I am not referring to the difference between
#android:id/.. and #id/..
Code Example:
<Button
android:id ="#id/add_button"
/>
<Button
android:id ="#+id/remove_button"
/>
What is the difference between the two id definitions above?
You must use the #+ notation on the first occurrence of the ID within an XML file. The second and subsequent times you can -- and should -- drop off the + sign. This will help catch typos.
For example, suppose that you have a RelativeLayout. You have a TextView in that RelativeLayout whose android:id is #+id/label. Later on in the layout XML file, you want to refer to that TextView from another one for positioning purposes (e.g., for android:layout_below).
If you typed in android:layout_below="#+id/labbel" (note the typo), at compile time, this will be considered OK. However, at runtime, things will not work, ranging from the widget being positioned incorrectly to an outright crash, depending on Android version.
If you typed in android:layout_below="#id/labbel" (note the typo and the missing + sign), then you would get a compile error.
UPDATE
Since I wasn't clear enough the first time, apparently, let's try again.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL:"
android:layout_alignBaseline="#+id/entry"
android:layout_alignParentLeft="true"/>
<EditText
android:id="#id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/label"
android:layout_alignParentTop="true"/>
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/entry"
android:layout_alignRight="#id/entry"
android:text="OK" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/ok"
android:layout_alignTop="#id/ok"
android:text="Cancel" />
</RelativeLayout>
Above, you will see a RelativeLayout. You will notice that the first occurrences of each ID get the + sign. The second and subsequent occurrences of each ID do not get the + sign.
You could use the + sign on all of them, but then if you make a typo, the compiler will not catch the problem.
The + sign effectively states "allocate a new ID". Not having the + sign states "use a previously-allocated ID, or fail at compile time if there is no such ID".
In the Android layout resource XML source files :
"#+id/anyId" : to add new id
"#id/anyId" : to refer existing id
You should use "#id/anyId" only when "anyId" is already added to R.java class using "#+id/anyId"
From Android Guide
For the ID value, you should usually
use this syntax form: "#+id/name". The
plus symbol, +, indicates that this is
a new resource ID and the aapt tool
will create a new resource integer in
the R.java class, if it doesn't
already exist.
So + is for assigning a new id, it will also work when using existed id but it is not necessary there.
The second one:
<Button android:id ="#+id/remove_button" />
defines a new id. You would use the first one, when you want to reference the layout element. For example, in a relative layout:
android:layout_below="#id/remove_button"
I have a Button that I am using 13 times in my Android application's main.xml file. I would like to have the XML for it defined once, so that I can make changes in one place instead of 13. Each of the 13 instances needs to have its own ID, though. What should I do to simplify my XML? I've tried using <include> but it hasn't worked for me. I must have been doing something wrong. I'd appreciate it if anyone could show me how to do it correctly. Thanks.
Here's the XML for the button that I'd like to reuse:
<Button
android:width="70dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
You should use Styles and Themes for that sort of things.
This page in the Android documentation defines an element id as follows:
<TextView android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
However this page defines it as:
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="#color/opaque_red"
android:text="Hello, World!" />
I thought I had a decent understanding of what was going on until I saw this second example. In the first case, you need the + character so that id 'label' is added to the R file, correct? In the second case, would the EditText's id not be added to the R file because it does not contain the + character?
Also, the second example does not include the android namespace on the id. Does having or not having the Android namespace affect whether that id will be added to the R file?
Thanks for any clarification.
This format without the android: namespace
id="text"
is from an earlier version of the Android SDK.
You are correct in your initial assessment. It's worth noting that the second id tag
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="#color/opaque_red"
android:text="Hello, World!" />
Is missing the android: namespace so it actually isn't an android xml tag. The first one is an example of how to add that view's id to the R file so you can access it in your code. To be honest, I'm not sure what the purpose of the id in the second example is*, but I know that android wouldn't know what to do with it. The first one is the correct syntax.
*This is just speculation, but I'm willing to bet it was a typo somebody didn't notice or didn't care to fix because they were trying to illustrate something else.
The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:
android:id="#android:id/empty"
Taken from Declaring Layout | Android Developers in the ID section.
However, in your second example there is no #android:id/ provided before the id text so to be brutally honest, I have never seen that notation before and wonder if that could be a typo on the author's part.
The second example is wrong. The attribute is always android:id and the value should be either #+id/myId (to create a new id called "myId") or #id/myId (to use an already defined id called "myId".) Using #android:id/theId lets you use ids defined by the android platform.