Can Android ID resources be forwarded forward?

Can id resources reference an element later in a file?

Here I have two fields: one for the username and one for the password. I am creating a new ID resource (with the syntax @+id/ ) for the password field, but the username field cannot find it in the android:nextFocusDown . I skipped the other fields from the xml layout because they are not relevant.

How do I need to declare identifiers in this case?

 <EditText android:id="@+id/login_username" android:nextFocusDown="@id/login_password" /> <EditText android:id="@+id/login_password" android:nextFocusUp="@id/login_username" android:nextFocusDown="@id/login_submit" /> 

Building with gradle, I get an error that looks like this: Error:(41, 32) No resource found that matches the given name (at 'nextFocusDown' with value '@id/login_password').

I am not getting an error for the android:nextFocusUp="@id/login_username" field android:nextFocusUp="@id/login_username" , which makes me think that you must declare the identifier before using it.

I also get compilation errors in my code because the R.java file is not created, most likely because the resources it builds from are not built either.

With all the fancy build tools in android, I am surprised that this is a problem. Is this a known issue or desired behavior?

+5
source share
2 answers

you can pre-create some identifiers by creating the values ​​of /ids.xml files with something like this

 <resources> <item name="login_password" type="id"/> </resources> 

Here is your case fooobar.com/questions/154670 / ...

+4
source

You can specify @ + id in the first value

 <EditText android:id="@+id/login_username" android:nextFocusDown="@+id/login_password" /> <EditText android:id="@+id/login_password" android:nextFocusUp="@id/login_username" android:nextFocusDown="@id/login_submit" /> 

There is no harm in using multiple @ + id. This can make typo detection a little more difficult, but if you need it, everything will be fine.

+6
source

Source: https://habr.com/ru/post/1205921/


All Articles