Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Android ID resources refer forwards?

Can id resources refer to an element later in the file?

Here I have two fields, one for the username, and another for the password. I create a new ID resource (with the @+id/ syntax) for the password field, but the username field cannot find it in the android:nextFocusDown field. I have omitted other fields from the layout xml, because they aren't relevant.

How would I need to declare the IDs 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'm getting 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 do not get an error for android:nextFocusUp="@id/login_username" field, which leads me to believe you must declare the id before you use it.

I also get compile errors in my code, because no R.java file is being generated, most likely because the resources it builds from aren't building either.

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

like image 965
pensono Avatar asked Oct 31 '14 05:10

pensono


2 Answers

You can specify a @+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 to having multiple @+id's around. It could make detecting typos a bit more difficult, but if you need it, it'll work well.

like image 132
PearsonArtPhoto Avatar answered Sep 28 '22 07:09

PearsonArtPhoto


you can pre-generate some ids by creating file values/ids.xml with something like this

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

Here is your case https://stackoverflow.com/a/12247971/4178124

like image 39
Dmitry Avatar answered Sep 28 '22 09:09

Dmitry