3

I read this

" It used to be that Android would use a single pass to process RelativeLayout-defined rules. That meant you could not reference a widget (e.g., via android:layout_above) until it had been declared in the XML. This made defining some layouts a bit complicated. Starting in Android 1.6, Android uses two passes to process the rules, so you can now safely have forward references to as-yet-undefined widgets. "

I do not know what is the problem maybe is eclipse problem, but even I use 2.3 I still have problems when I reference some view that is not declared jet so for me it seems like android doesn't uses two passes to process the rules for relative layout.

note: I always use @+id/widget_name when I declare the widget and @id/widget_name when I reference that widget from other widget. I have noticed that I can use @+id/widget_name even when I just want to reference that widget. I guess that is wrong but why sometimes is works without any complaints ? In my opinion one widget should be allowed to be declared only ones...

My questions is is really android uses two passes ? and I need some guidelines (best practices) for working with relative layouts

I am little confused about how this relative layout parings are made, so any explanations are welcomed

Thanks

Lukap
  • 31,523
  • 64
  • 157
  • 244
  • I also have this problem and it's terrible to have to reorganize the layout to fix the references. Would not be this a task for the compiler, instead of the programmer? – User May 29 '12 at 14:09

2 Answers2

2

@+id/name creates a new id, if it doesn't already exist. @id/name references an existing id, and will never create one.

I'm not sure if you can use @id/name before @+id/name in the same file. If not, I can think of two workarounds:

  1. Always use @+id/name.

  2. Define all id's in the ids.xml file, and always use @id/name.

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
  • I got a question about number one "Always use @+id/name" , what side effects should I expect ? I mean I could not be normal to define name 5 times... – Lukap Sep 12 '11 at 14:19
  • There is a risk of miss-spelling an id. Depending on where the id is used, you could get an error on compile time, or run time. With `@id/name`, you would always get a compile time error when you misspell an id. – Markus Jarderot Sep 12 '11 at 14:40
1

This is general information on how Android draw views. I think that Android passes twice through all the view, but it doesn't pass through each single view once. So if you have a reference from one xml to another it will always work fine, but if you have references inside a single xml you must be carefull to order the elements in the xml correctly. For example, I have view1 and view2 in my RelativeLayout. If I want to refer to view2 from view1 I must declare view2 before view1.

superM
  • 8,605
  • 8
  • 42
  • 51