Question

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

Was it helpful?

Solution

@+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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top