Android layout reference xml element later in file - android

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"
... />

Related

no resource found that matches the given name?

This is driving me mad. My project compiled fine a moment ago. I made some minor change somewhere else, and now I'm getting this error. Here's the error message in full:
no resource found that matches the given name (at 'layout_above' with value '#id/blank_view").
Here's my XML file where the error is occurring:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_right"
android:layout_above="#id/blank_view"
android:src="#drawable/button" />
<View
android:id="#+id/blank_view"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_bottom"
android:layout_alignParentBottom="true" />
</RelativeLayout>
How in the hell could it be failing to find a resource with the name "#id/blank_view" when that resource is just below it in the file?
Btw, the reason I have my layout like this is that I want the ImageButton to be aligned to the bottom of my relative layout, but offset up by a margin. For some reason, those two attributes (layout_alignParentBottom and layout_marginBottom) don't mix well in the same view.
I should also point out that this happened earlier as well, but I just removed the reference that was giving AndroidStudio such a problem, rather than trying to fix it. This, however, is too important to wave away like that.
This happens because the xml is parsed in a linear fashion and the views/objects created in a top to bottom order. So your xml tells the view builder to put your ImageButton above an item that does not exist yet.
Just move it below the blank view and the error should go away.
Try to add the property to your view:
<view
android:id="#+id/blank_view"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_bottom"
android:layout_alignParentBottom="true"
android:layout_below="#id/btn"
/>
And remove:
android:layout_above="#id/blank_view"

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>

RelativeLayout id references

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.

Getting ViewStub with #android:id/empty in Monodroid returns null

In Monodroid I have a ListActivity class with the following axml segment:
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="#layout/empty" />
I'm trying to access the ViewStub with:
FindViewById(Android.Resource.Id.Empty);
However, it always returns null. If I access the list with:
FindViewById(Android.Resource.Id.List);
it works or if I name the ViewStub something other than #android:id/empty it works. The ViewStub does work when my list is empty.
Thanks
Here are a few things I can think of that might be the issue.
The first issue could be that you have another UI page that defined the id of "empty" somewhere else. MonoDroid puts all IDs from all pages into the same resource file, so you obviously can't have the same name twice. Try putting empty2 to see if that works.
The next issue could be that you should have android:id=#id+/empty". The + tells MonoDroid to generate an ID for the control in the Resource file.
Last, use something other than "empty". Empty could be a reserved word by Android/MonoDroid, so I would avoid it.

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