Isn't there a way to tell declaratively an element, position relative to many elements - if one is GONE then use the other? Of course if all elements are not GONE take only one (e.g. the first one).
A very simple solution is to use a LinearLayout inside of your RelativeLayout. So you can place elements inside the LinearLayout which then align to the previous element in the LinearLayout - either horizontally or vertically.
The framework itself does not support the feature to position an element relative to the visibility of many other elements.
The only thing I see even remotely related to that is android:layout_alignWithParentIfMissing and that says:
If set to true, the parent will be used as the anchor when the
anchor cannot be be found for layout_toLeftOf, layout_toRightOf,
etc. [boolean]
So I believe to get the functionality you want you would have to code it in yourself.
Related
As far as I understand Layouts in Android I just wanted to ask whether the following statement is true or not ?
Is it true that every RelativeLayout in Android can be programmed with a LinearLayout ?
I personally believe that every RelativeLayout can be also done with a LinearLayout. So why should someone use RelativeLayout rather than a Linear ?
Thank you
Is it true that every RelativeLayout in Android can be programmed with a LinearLayout ?
No.
First, RelativeLayout supports Z-axis ordering (i.e., widgets overlapping other widgets). LinearLayout does not.
Second, only a subset of RelativeLayout structures could be replaced by a single LinearLayout. It is conceivable that you could implement all non-overlapping RelativeLayout structures using many LinearLayout and Space widgets. The result may be substantially more complex, more memory-intensive, and possibly more CPU-intensive.
According with android reference guide:
LinearLayout: A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen.
RelativeLayout: Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
For more information: http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
Even if that statement was true, RelativeLayout is simply a better tool for certain use cases - so why don't use it? You might be able to recreate a layout using a LinearLayout, but the result will most likely be inefficient in terms of number of views and rendering times. Additionally, sometimes using RelativeLayout leads to a more simple and intuitive layout description, which means easier maintainability.
What is the exact use of FrameLayout in Android?
Where can we use it?
What is the difference between Frame and Relative layout?
LinearLayout arranges elements side by side either horizontally or vertically(rows vs columns).
RelativeLayout is a layout manager that helps you arrange your UI elements based on some rule. You can specify things like: align this to parents left edge, place this to the left/right of this elements etc.
AbsoluteLayout is for absolute positioning i.e. you can specify exact co-ordinates where the view should go.
FrameLayout allows placements along Z-axis. That is you can stack your view elements one above the other.
As per my opinion
RelativeLayout can use to make sure views lineup correctly in relation to each other.
FrameLayout is very similar except it's only using gravity to put display it's views (with no relation).
For more information, please check this official page:http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
I have some buttons, textboxes etc. in my android application, but when i drag them with my mouse in the xml file, their place doesn't change, or changes but they are not placed where i exactly wanted. How can i adjust their positions in the screen?
Thanks
Unfortunatly there is no such thing as absolute positionning in android ( RIP AbsoluteLayout deprecated since years.)
instead you have to position views according to their parents and according to other views in the same parent.
first you have to define wich parent you need ( if you want some viens in a single line go for a LinearLayout. a more custom layout: use a RelativeLayout ...)
then you can drag and drop views inside, but they will always snap a position relative to their parent and/or relative to the other views.
you can of course play with margins.
A list of layout type with some advanced techniques can be found on this page
Hope that helps.
You RelativeLayout as a group layout for your layout so positioning can somewhat easy using mouse.
Best is to arrange them from the xml code. Just Learn about using the Relative layout, LinearLayout and TableLayout
Learn how the XML works. For a LinearLayout, the items come in the order listed. For a RelativeLayout, the items are related by the values of their layout_XXX properties. Then you don't have to worry about the WYSIWYG tool not working.
FYI, the tool bundled with eclipse is extremely buggy. Don't count on whats on there being what's on your phone for anything non-trivial.
Like the others wrote it is easier to edit layout using xml editor. You can read more here http://developer.android.com/guide/topics/ui/declaring-layout.html.
I have read many articles regarding layout, but I am still quitely confused. My questions are:
When to use relative layout? Example?
When to use table layout and why we can't use it instead of relative layout?
When to use linear layout?
I just need brief answers.
When use which layout?
I think It depends on your UI, and most important thing that how you create optimized layout.
From definition : -
LinearLayout – designed to display child View controls in a single row or column. This is a very handy layout method for creating forms.
RelativeLayout – designed to display child View controls in relation to each other. For instance, you can set a control to be positioned “above” or “below” or “to the left of” or “to the right of” another control, referred to by its unique identifier. You can also align child View controls relative to the parent edges.
TableLayout – designed to organize child View controls into rows and columns. Individual View controls are added within each row of the table using a TableRow layout View (which is basically a horizontally oriented LinearLayout) for each row of the table.
References :
Creating Efficient Layouts
Common Layout Objects
And most important Hierarchy Viewer
at first there is some confusion about these layouts but as you start playing with these three layouts u will get idea where to use what.. I worked on relative-layout the most.
Consider i want to use a widget always at bottom of screen then with table or linear layout this is not possible always.. without feeling screen other two can not make item at bottom but relative can do.use of any type of layout depends on your screen requirements.
I started out using relativelayout. But recently I've switched to using mostly linearlayout.
The reason is kind of hard to explain, but take this as an example: Say I want a layout that has two images centered in the middle of the screen. Both images should take up 1/4 of the screen width and 1/4 of the screen height. This is impossible to do with relativelayout assuming you want it to work exactly the same on all devices. But you can do this with Linearlayout. By creating vertical and horizontal parents, you can create "boxes". To accomplish this you must learn about weigthsum and weigth. Parent layouts should have the weigthsum attribute and children should have the weight attribute.
Anyway, my point: Relativelayout is easy to use but it's also deceptive. You may think that your layout will look exactly alike on all device, but most likely, they won't look alike. The reason for this is:
With relativelayout you must define size with either dp or px(assuming you don't fill parent or wrap content).
Different devices have different aspect ratios.
I hoped that helped in terms of understanding relative and linearlayout.
Lets say I have a list on screen, which I always want to be usable. I also want a small image or textview to slightly overlap the listview. Is anything this possible without using absolute layout parameters?
I've never seen it in any android app or tutorial, but there are things like this in many iPhone apps. It adds a nice touch.
(Also, I don't have any code to show because I'm not sure where to start)
Relative Layouts also allow things to overlap. Views declared later in the xml will be on top. I believe that aligning view edges and use of margins should allow you to achieve this affect without great difficulty.
You could use RelativeLayout and set for example android:layout_marginTop="-50dip" android:layout_below="#id/my_list".
As well as RelativeLayouts, you can also use FrameLayouts to stack objects. Other than the z-order (last object declared = highest z-order), the child objects don't depend on the positioning of other objects in the group, so you can just set margins or gravity to position them.
So in your instance, just declare a TextView after your ListView, and position it wherever you want. It won't interfere with the ListView's positioning, and it will sit on top.