RelativeLayout id references - android

These two views are inside of RelativeLayout. The IDE throws an error there is no #id/et_pass, but if I set #+id/et_pass it is OK. Why is that?
<ImageView
android:id="#+id/devider_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/et_pass" <!-- Error is here -->
android:src="#drawable/registration_line" />
<EditText
android:id="#+id/et_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/devider_first"
android:background="#android:color/transparent"
android:layout_gravity="left"
android:ellipsize="start"
android:ems="8"
android:hint="#string/password"
android:inputType="textPassword"
android:layout_marginTop="#dimen/register_layout_edittext_margin_top"
android:maxLines="1" />

The difference between #+id/something and #id/something is that the first one is creating an id, and the second one is referencing an id that has already been created. The first time that you mention an id you have to create it using #+id/, and anything after that can use #id/.
When you give a view the attribute android:id you do not have to use #+id/ if you have already used that somewhere earlier in your file.
Because of the way android compiles XML files, it reads your image view first, reaches the point where you write #id/ searches for an id in the R file, and can't find it. But if you call #+id/ the eclipse searches for the id in the R file, can't find it, and adds it.
Also, this is not specific to RelativeLayouts, if you put the same code in a linear layout, you would also get that error

#+id instructs the parser to create the ID if it does not exist. #id is used to refer to an existing ID.

Related

What is the accepted practice for #id vs. #+id usage in Android development?

I am new to Android development.
I noticed that sometimes #+id was used instead of #id. From what I could find, apparently when you compile a program, it first scans the source data and the #+id tells it to add the id to the R file, and then after that it is considered okay to use #id.
Is there a good practice to use for this? Could I theoretically always use #+id to be safe? What about using #+id once to add it to R, and then removing the plus sign now that it's already present?
Or is there an accepted practice of where the #+id version is to be declared if #id is to be used everywhere else?
I think generally if you are assigning an id to a view #+id, referring to a view #id
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/start"
/>
#+id is used at the first occurrence of that id . This of course implies that whenever we are assigning an id the #+id is used. Besides this practice, whenever we are referring to an id for first time, eg in RelativeLayout if we use above for a particular id, and we assign that id to an element (Button or TextView) below that line of code, we have to use ...above="#+id/not_yet_assigned"
For example:
<Button
android:id="#+id/btn_first"
android:layout_above="#+id/btn_second"
android:text="ONE"
/>
<Button
android:id="#id/btn_second"
android:text="TWO"
/>

Android layout reference xml element later in file

How do I reference a later XML element?
Here's a specific use case. Let's say I have a form with a root LinearLayout, containing LinearLayouts for multiple rows, each row having one or more text input areas.
Here's a visual of what I'm going for. First pic is from Venmo's app, second is a rendering of the following XML.
Such a layout could look like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/row_card_number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/card_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nextFocusDown="#id/month"/>
</LinearLayout>
<LinearLayout
android:id="#+id/row_date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/month"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:nextFocusDown="#id/year"/>
<EditText
android:id="#+id/year"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
</LinearLayout>
In this use case, forward referencing is necassary in order to set the next focus element. That way, when you press the next button on the keyboard, it'll go to the correct view. In this sample xml, without the nextFocusDowns, pressing next would go from name to month, and never go to year.
However, if you try to compile this, you'll get an error:
Error:(18, 36) No resource found that matches the given name (at 'nextFocusDown' with value '#id/month').
This is because the id month hasn't yet been initialized when I'm trying to reference it, since that's later in the file. How can I reference an id in xml that appears later in the file?
The simplest solution is just to replace
android:nextFocusDown="#id/month"
with
android:nextFocusDown="#+id/month"
When the compiler is parsing your XML to add the id's to R.java, it just reads top to bottom. When you have #id/month, it searches through the existing id's, and fails to find it.
However, if you do #+id/month, it creates a new id, and links to that. When it gets to android:id=#+id/month in the actual month view, it links it to the same id that we already created.
This brings up the question: If you can replace #id/ with #+id/, and #+id/ will work regardless of the order of elements, why even bother to use #id/?
The reason for this is if the id doesn't exist, #id/ will throw a compiler error, while #+id/ will log a warning at runtime.
Consider this XML:
<EditText
android:id="#+id/month"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:nextFocusDown="#+id/SOME_RANDOM_ID"/>
<EditText
android:id="#+id/year"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
When this is parsed, a new id element SOME_RANDOM_ID is created. However, when Android tries to apply it at runtime, it can't find an element with that id. If you look at Logcat, you'll see this:
W/View﹕ couldn't find view with id 2131689604
This log message is both hard to find and hard to debug. One small typo in a #+id/ and you'll have a bug that could be incredibly difficult to debug. However, if we had done:
android:nextFocusDown="#id/SOME_RANDOM_ID"
Then we'd get a compiler error, something like:
Error:(18, 36) No resource found that matches the given name (at 'nextFocusDown' with value '#id/SOME_RANDOM_ID').
This is much easier to find and debug.
tl;dr: You can use #+id/ instead of #id/ and you'll be able to forward reference, but note that that can make small typos incredibly difficult to debug.
You might be able to use a RelativeLayout to make all the Views exist in reverse order in the xml, but that seems like overkill to me.
I had the same issue recently and I used #+id/my_new_id the first time I referenced the element and later in the XML in the element definition, I assigned #id/my_new_id to the android:id attribute. It seems it works fine and it's not necessary write #+id with the same id more than one time avoiding possible warnings.
For example:
<LinearLayout
...
android:layout_toLeftOf="#+id/my_new_id"
... >
...
</LinearLayout>
<ImageButton
android:id="#id/my_new_id"
... />

Two views referencing each other

I have two textviews declared as below. My intention is to have two views side by side, where left view (displaying user name) has bigger text size, right view displays time of last message send and should be always visible (even if user has really long name, that's why I'm using android:layout_toLeftOf). However, left view is smaller and I want to align its baseline to right view. It's really nice dependency where and I'm not able to solve it.
Partially acceptable solution is to use "android:layout_toRightOf" in right view, but if user has really long name, then time (right view) will be ellipsized (it's declared in AppTheme.TextView.SingleLine).
So basically, my questions is, is it possible for two views to reference each other? I understand why I'm getting this error, but I'm not able to solve it.
I remember from my C/C++ times that it was possible to declare function in the top of the file and then define it somewhere else (so the compiler doesn't complain) and I think it's something what I need here.
<TextView
android:id="#+id/fragment_messages_item_sender_name"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/fragment_messages_item_last_msg_time"
android:textSize="#dimen/global_text_large"/>
<TextView
android:id="#+id/fragment_messages_item_last_msg_time"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/fragment_messages_item_sender_name"
android:layout_alignParentEnd="true"
android:gravity="right"/>
All I'm getting is
Error:(27, 38) No resource found that matches the given name (at 'layout_toLeftOf' with value '#id/fragment_messages_item_last_msg_time').
in R class id like
fragment_messages_item_last_msg_time
not exist
for avoid that problem need to use "+" before declaration field with id relation
android:layout_toLeftOf="+#id/fragment_messages_item_last_msg_time"
you could move the android:layout_alignBaseline in the other TextView. Be aware of id loops, that usually generated nasty crashes.
About your issue, you have to remember the entries inside R are marked as public static final, and that the + generates a new entry for the specify id, if it does not exists, So you can have:
android:layout_toLeftOf="#+id/fragment_messages_item_last_msg_time"
in the first TextView, and
android:id="#id/fragment_messages_item_last_msg_time"
to the second one. As I mentioned before, loops in RelativeLayout are not allowed, and those will make your app crash
Hi use below code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/fragment_messages_item_sender_name"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manish" />
<TextView
android:id="#+id/fragment_messages_item_last_msg_time"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Khamar" />
</LinearLayout>

Why isn't Eclipse accepting valid Android IDs in nextFocusForward?

I have some EditText views in a RelativeLayout. The first one receives focus correctly, but when the user clicks "Finished" on the keyboard, it usually doesn't send focus to the view the user expects. Assuming that the "Finished" button uses the FOCUS_FORWARD ID, I have tried to fix this behaviour by using the android:nextFocusForward attribute like so:
<EditText
android:id="#+id/editTextName"
...
android:nextFocusForward="#id/editTextNameColour" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextNameColour"
...
android:nextFocusForward="#id/editTextBackgroundColour" />
<EditText
android:id="#+id/editTextBackgroundColour"
... />
It doesn't compile because Eclipse gives me an error message like: "error: Error: No resource found that matches the given name (at 'nextFocusForward' with value '#id/editTextNameColour')".
I know the name is correct because I selected it from a dropdown list in Eclipse and all files have been saved.
Android Developers Reference says that EditText is the right type (a View).
If there's a better way than using android:nextFocusXxx attributes, including doing it in the Java code, that's fine too.
I'm not open to solving this by using a LinearLayout.
Also, am I right that the "Finished" button uses the FOCUS_FORWARD?
Thanks
Solution: I need to use #+id... rather than #id because I am referencing objects declared later in the code; and the keyboard appears to use FOCUS_DOWN rather than FOCUS_FORWARD.
It's because of #id vs #+id. Just use #+id. You're trying to use an ID before its been assigned a resource. You can also switch the order around that you're declaring stuff, if its in relativeLayout.
If that is the order, then you are referencing elements that hasn't been defined yet. You should do something like:
<EditText
android:id="#+id/editTextName"
...
android:nextFocusForward="#+id/editTextNameColour" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextNameColour"
...
android:nextFocusForward="#+id/editTextBackgroundColour" />
<EditText
android:id="#+id/editTextBackgroundColour"
... />

what is the difference betwenn #id/ and #+id/ in android? [duplicate]

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"

Categories

Resources