Why won't this center the contents in the view?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/messageTextField"
android:layout_weight="1"
android:inputType="number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/messageTextFieldPlaceholder"
/>
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sendButtonTitle"
android:onClick="sendMessage"
/>
</LinearLayout>
I saw a few SO threads about vertical alignment and I have also seen this, but I only have a horizontal LinearLayout, so setting the gravity will adjust the views from side to side.
Since the LinearLayout has the parent's dimensions, does it not fill the whole screen?
Shouldn't android:layout_gravity="center_vertical" align it's subviews in the center (vertically) of it's dimensions?
This is what makes sense to me across the coding that I've done, so why is android not making sense?
Do I need another LinearLayout that is vertical that holds everything else inside of it and have that with android:layout_gravity="center_vertical"?
layout_gravity:
Standard gravity constant that a child can supply to
its parent.
gravity:
Specifies how to place the content of an object, both
on the x- and y-axis, within the object itself.
So if you want your content to be vertically centered you need gravity not layout_gravity.
Instead of using android:layout_gravity="center_vertical", please use android:gravity="center_vertical", It will work...:)
Related
I know we can set the following values to the android:gravity and android:layout_gravity properties:
center
center_vertical
center_horizontal, etc.
But I am confused regarding both of these.
What is the difference between the usage of android:gravity and android:layout_gravity?
Their names should help you:
android:gravity sets the gravity of the contents (i.e. its subviews) of the View it's used on.
android:layout_gravity sets the gravity of the View or Layout relative to its parent.
And an example is here.
Inside - Outside
gravity arranges the content inside the view.
layout_gravity arranges the view's position outside of itself.
Sometimes it helps to see a picture, too. The green and blue are TextViews and the other two background colors are LinearLayouts.
Notes
The layout_gravity does not work for views in a RelativeLayout. Use it for views in a LinearLayout or FrameLayout. See my supplemental answer for more details.
The view's width (or height) has to be greater than its content. Otherwise gravity won't have any effect. Thus, wrap_content and gravity are meaningless together.
The view's width (or height) has to be less than the parent. Otherwise layout_gravity won't have any effect. Thus, match_parent and layout_gravity are meaningless together.
The layout_gravity=center looks the same as layout_gravity=center_horizontal here because they are in a vertical linear layout. You can't center vertically in this case, so layout_gravity=center only centers horizontally.
This answer only dealt with setting gravity and layout_gravity on the views within a layout. To see what happens when you set the gravity of the of the parent layout itself, check out the supplemental answer that I referred to above. (Summary: gravity doesn't work well on a RelativeLayout but can be useful with a LinearLayout.)
So remember, layout_gravity arranges a view in its layout. Gravity arranges the content inside the view.
xml
Here is the xml for the above image for your reference:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e3e2ad"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center"
android:text="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#d6c6cd"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="layout_gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="left"
android:background="#bcf5b1"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#aacaff"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="#bcf5b1"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#aacaff"
android:text="center" />
</LinearLayout>
</LinearLayout>
Related
Difference between a View's Padding and Margin
Match_parent vs wrap_content
How to set both gravity and layout gravity of a LinearLayout programatically
The difference
android:layout_gravity is the Outside gravity of the View. Specifies the direction in which the View should touch its parent's border.
android:gravity is the Inside gravity of that View. Specifies in which direction its contents should align.
HTML/CSS Equivalents
(if you are coming from a web development background)
Android | CSS
————————————————————————+————————————
android:layout_gravity | float
android:gravity | text-align
Easy trick to help you remember
Take layout-gravity as "Lay-outside-gravity".
Short Answer: use android:gravity or setGravity() to control gravity of all child views of a container; use android:layout_gravity or setLayoutParams() to control gravity of an individual view in a container.
Long story: to control gravity in a linear layout container such as LinearLayout or RadioGroup, there are two approaches:
To control the gravity of ALL child views of a LinearLayout container (as you did in your book), use android:gravity (not android:layout_gravity) in layout XML file or setGravity() method in code.
To control the gravity of a child view in its container, use android:layout_gravity XML attribute. In code, one needs to get the LinearLayout.LayoutParams of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:
import android.widget.LinearLayout.LayoutParams;
import android.view.Gravity;
...
Button button = (Button) findViewById(R.id.MyButtonId);
// need to cast to LinearLayout.LayoutParams to access the gravity field
LayoutParams params = (LayoutParams)button.getLayoutParams();
params.gravity = Gravity.BOTTOM;
button.setLayoutParams(params);
For horizontal LinearLayout container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity to center_horizontal has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_gravity value is -1 but Android put it center vertically.
To change the horizontal positions of child views in a horizontal linear container, one can use layout_weight, margin and padding of the child view.
Similarly, for vertical View Group container, the vertical gravity of its child view is top-aligned one below another and cannot be changed. The default horizontal gravity is center (or center_horizontal) and can be changed to left or right.
Actually, a child view such as a button also has android:gravity XML attribute and the setGravity() method to control its child views -- the text in it. The Button.setGravity(int) is linked to this developer.android.com entry.
From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.
I think this is right but the best way to find out is to play around.
Look at the image to be clear about gravity
If we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) with in its parent view then we will use "android:layout_gravity".
An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.
Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view
Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms:
Layout Gravity affects your position in the superview. Gravity affects the position of your subviews within you.
Said another way, Layout Gravity positions you yourself while Gravity positions your children.
There is many difference in the gravity and layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).
Use Of Gravity and Layout-gravity in FrameLayout .....
Note:-
Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.
Layout-gravity is used with the parent View as some User have answer.
Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....
We can set Child View any where in the FrameLayout using layout-gravity .
We can use every single value of gravity inside the FrameLayout (eg:- center_vertical, center_horizontal, center,top, etc), but it is not possible with other ViewGroup Layouts .
FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with FrameLayout).
have look on example ......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center"
android:text="Center Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#1b64b9"
android:gravity="bottom"
android:layout_gravity="bottom|center"
android:text="Bottom Layout Gravity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#d75d1c"
android:gravity="top"
android:layout_gravity="top|center"
android:text="Top Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="100dp"
android:textColor="#d71f1c"
android:gravity="top|right"
android:layout_gravity="top|right"
android:text="Top Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="Top Left Layout Gravity"/>
</FrameLayout>
Output:-
Use Of Gravity and Layout-gravity in LinearLayout .....
Gravity working same as above but here differnce is that we can use Gravity inside the LinearLayout View and RelativeLayout View which is not possible in FrameLayout View.
LinearLayout with orientation vertical ....
Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called center_horizontal)).
have look on example :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="Center Layout Gravity \nor \nCenter_Horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="right"
android:text="Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="Left Layout Gravity"/>
</LinearLayout>
Output:-
LinearLayout with orientation horizontal ....
Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called center_vertical)).
have look on example :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="bottom"
android:text="Bottom \nLayout \nGravity"/>
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="center"
android:text="Center \nLayout \nGravity"/>
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:text="Left \nLayout \nGravity"/>
</LinearLayout>
output:-
Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....
Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.
If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.
I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.
The basic difference between the two is that-
android:gravity is used for child elements of the view.
android:layout_gravity is used for this element with respect to parent view.
android:gravity is used to specify how to place the content of the object within the object itself. In another word, android:gravity is used to specify the gravity of the content of the view.
android:layout_gravity is an attribution the child can supply to its parent, to specify the gravity the view within its parents.
For more details you can visit
http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html
Gravity: Allow you move the content inside a container. (How sub-views will be placed).
Important: (MOVE along X-axis or Y-axis within available space).
Example: Let's say if you were to work with LinearLayout (Height: match_parent, Width: match_parent) as root level element, then you will have full frame space available; and the child views says 2 TextViews (Height: wrap_content, Width: wrap_content) inside the LinearLayout can be moved around along x/y axis using corresponding values for gravity on parent.
Layout_Gravity: Allow you to override the parent gravity behavior ONLY along x-axis.
Important: (MOVE[override] along X-axis within available space).
Example: If you keep in mind the previous example, we know gravity enabled us to move along x/y axis, i.e; the place TextViews inside LinearLayout. Let's just say LinearLayout specifies gravity: center; meaning every TextView needs to be center both vertically and horizontally. Now if we want one of the TextView to go left/right, we can override the specified gravity behavior using layout_gravity on the TextView.
Bonus: if you dig deeper, you will find out that text within the TextView act as sub-view; so if you apply the gravity on TextView, the text inside the TextView will move around. (the entire concept apply here too)
Gravity is used to set text alignment in views but layout_gravity is use to set views it self. Lets take an example if you want to align text written in editText then use gravity and you want align this editText or any button or any view then use layout_gravity, So its very simple.
gravity: is used for simple views like textview, edittext etc.
layout_gravity: is used for current view only gravity in context of it's relative parent view like linear Layout or FrameLayout to make view in center or any other gravity of its parent.
The android:gravity sets the gravity (position) of the children whereas the android:layout_gravity sets the position of the view itself. Hope it helps
android:gravity
is used to adjust for content of the view relative to its specify position (allocated area). android:gravity="left" would not do anything if layout_width is equal to the "wrap_content"
android:layout_gravity
is used for view itself relative to the parent or layout file.
android:gravity -> Sets the gravity of the content of the View its used on.
android:layout_gravity -> Sets the gravity of it's Parent's view or Layout
gravity--Applies to its own view.
layout-gravity---Applies to view related to its parent.
Gravity and layout_gravity both are the XML attributes.
The android:gravity attribute is used to arrange the position of the content inside a view (for example text inside a Button widget).
The android:layout_gravity is used to arrange the position of the entire View relative to it’s container.
For further details you can see this link.
I have two buttons in a horizontal LinearLayout. They are currently next to each other and the very left. I want to move the second button to the right end of the LinearLayout.
I tried android:gravity on these buttons but this didn't change the position of them at all.
Thanks
You cannot achieve this using a LinearLayout.
Use a RelativeLayout instead and place each button relative to RelativeLayout right or left. Something like below example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:text="Button" />
</RelativeLayout>
The easiest way to do this is to use RelativeLayout. You can give your Button you want on the right the property alignParentRight="true".
<Button
...
android:layout_alignParentRight="true"/>
For a horizontal LinearLayout, android:layout_graivty (which is what you would want instead of android:gravity) left and right won't do anything because the Views are already placed from right to left.
See this answer on the difference between android:gravity and android:layout_gravity if you are uncertain about those.
Edit
Depending on exactly what you need/want, it is possible to do this with a LinearLayout though probably still much easier and more flexible with a RelativeLayout. Anyway, you can use weight to achieve something similar and play with the values. The following gives me a Button on the left and a Button on the right.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Left Button"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Right Button"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Right Button"/>
</LinearLayout>
Try setting the left button's layout_gravity to left (or start) and the right button's layout_gravity to right (or end).
The problem is that you are currently using gravity which
Specifies how an object should position its content, on both the X and Y axes, within its own bounds.
Instead, you should use layout_gravity that is
Standard gravity constant that a child supplies to its parent. Defines how the child view should be positioned, on both the X and Y axes, within its enclosing layout.
In other words - you are currently telling the buttons how to align their child views, instead of telling them how to be aligned within their parent.
You can set the android:layout_weight='1' and both buttons will share the screen equally(side by side) or if you want the extra space between them, you can place a button each in a linear layout and set the android:layout_gravity to left and rightfor each.
Add a RelativeLayout and set values to layout_marginLeft, layout_marginTop, etc.
eg.
android:layout_marginLeft="32dp"
android:layout_marginTop="160dp"
make linearLayout orientation Vertical and set button's gravity => center and as you want..
I imagine this should be a fairly easy one to answer, if you understand XML Layouts better than I do that is. I don't seem to get what I was thinking I should when using the match_parent layout_height.
I have a LinearLayout root element with android:orientation="vertical". Inside this LinearLayout I want three elements:
- TextView
- ListView
- TextView
For both the TextViews I set android:layout_height="wrap_content" so that they will be only as tall as is necessary to display their contents. The thing is, I want the one TextView to sit at the top of the form, the other one to sit at the bottom of the form while the ListView fills up whatever space is available on the form. So here is what my xml layout looks like:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Top TextView" />
<ListView
android:id="#+id/listView_Species"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Bottom TextView" />
But it doesn't work. Here's what I get. I've selected the ListView so that it will be highlighted. Notice how it extends all the way to the bottom of the form, pushing the bottom TextView off the form.
When I change the layout_height property of the ListView to some fixed value, like 180dp, this is what the form looks like. I'm just posting this to prove that the bottom TextView is there but I still don't know how to get it to be fixed to the bottom of the screen while the ListView takes up whatever space remains, but in between the two TextViews.
Thanks in advance.
While the other answers try to fix your problem (which they don't actually--they suggest you do something that looks similar but may or may not look good on different devices), no one has filled in the gaps in your knowledge of LinearLayouts and match_parent. And these gaps are very common--Google's documentation is still far below stellar.
First, how do Views work within a LinearLayout? Let's go through the process of drawing a LinearLayout, using orientation="vertical" for simplicity.
Examine the height of the first child of the LinearLayout (LL for short). If the height is match_parent or fill_parent (old name for the same thing) then the height of the View is stretched to fill the entire viewing area. If the height is wrap_content, then measure the vertical space the View takes and use that space for the View. If the height is a non-zero number, use exactly that many pixels for the View's height (may clip if too small). If the height is 0 see below.
Put the next view below the view in 1. Check its height and act accordingly.
Continue for all the Views. If a View is pushed off the bottom, go ahead and stop calculating because no one will see it or any succeeding Views (assuming no ScrollView).
If the height of a View is 0, check it's gravity. This requires a second pass, storing the gravity of all the views and then allocating their heights proportionally. As you can guess, the second pass doubles the time layout takes, which isn't significant for simple layouts.
Explanation of your example: The first child of the LL (the first TextView) is measured and takes a certain amount of pixels. Then your ListView takes all the remaining space (via match_parent). And then your second TextView is not drawn at all as it's off the bottom of the screen. Which is pretty much what you observed, but now you understand why.
Solution: Use RelativeLayout. Works perfectly in this case.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/top_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:text="Top TextView" />
<TextView
android:id="#+id/bottom_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textSize="30sp"
android:text="Bottom TextView" />
<ListView
android:id="#+id/listView_Species"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/top_tv"
android:layout_above="#id/bottom_tv"
/>
</RelativeLayout>
The RelativeLayout tells the layout inflater to draw the first TextView at the top, then draw the second TextView at the bottom, and then fill the rest of the space with your ListView. I believe this is exactly what you want.
Welcome to Android. You'll be using this pattern a LOT!
Change the ListView height to 0dp and add weight=1
i.e.:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Top TextView" />
<ListView
android:id="#+id/listView_Species"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Bottom TextView" />
use android:layout_weight to define weights to your widgets inside the outermost layout. Declare their height as 0dp and then define android:layout_weight to each one of them .
Total weigh sum of the three of them should be 1. According to your need you can deine 0.1 weight to both top and bottom TextView's and define 0.8 to ListView.
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight = "0.1"
android:textSize="30sp"
android:text="Top TextView" />
<ListView
android:id="#+id/listView_Species"
android:layout_width="match_parent"
android:layout_weight = "0.8"
android:layout_height="0dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:textSize="30sp"
android:layout_weight = "0.1"
android:text="Bottom TextView" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="top" <!--this line-->
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="56dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="36dp" />
</LinearLayout>
Shouldn't that make the smaller "0" t-view align to the top of the inner LinearLayout?
Since the other "0" is larger in height, it increases the overall height of the inner LinearLayout.
Alternatively, if you included this:
android:layout_gravity="top"
within the t-view of the smaller 0, it also does nothing.
Why is this?
Does the wrap_content of the LinearLayout wrap each individual view independent of others?
If so, then why does setting gravity to "center" work? In the sense that the smaller zero is vertically centered to its parent.
I know you can just set the smaller 0's height to match parent and set its own gravity to top. I'm just trying to understand this. Thanks.
The default behavior of the layout is to align the baselines of the text, which in English are at the bottom of the view. Adding android:baselineAligned="false" to your LinearLayout will align the second TextView at the top of the view.
gravity attribute applies to widgets within the LinearLayout, while layout_gravity tells the parent of LinearLayout where to place the child (LinearLayout).
Also, in a vertical LinearLayout the gravity="top" attribute won't work.
In the layout you have now, both text views will be stacked one on top of the other, wrapped with a linear layout with no space in-between - so the "top" or "center" values won't do anything because there is no extra space to move the text views up or down.
If you want to understand this better, try giving your linear layout and both text views backgrounds of different colors, like this:
android:background="#color/mycolor"
Then you will see the bounds of each widget.
You can use android:layout_weight="10" and distribute on the other components.
Why does FOOBARZ get layed out all the way at the bottom when no elements are layout_height="fill_parent" in other words, all elements are wrap_content for height?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/feed_u"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginLeft="5dip"
android:scaleType="centerCrop"
android:drawableTop="#android:drawable/presence_online"
android:text="U" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/feed_u">
<ImageView
android:id="#+id/feed_h"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_minus" />
<ImageView
android:id="#+id/feed_ha"
android:layout_toLeftOf="#id/feed_h"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_plus" />
<TextView
android:id="#+id/feed_t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title">
</TextView>
<TextView
android:id="#+id/feed_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Band"
android:layout_below="#id/feed_t">
</TextView>
<TextView
android:id="#+id/feed_s"
android:layout_below="#id/feed_a"
android:text="S"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</TextView>
<TextView
android:id="#+id/feed_tm"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="FOOBARZ"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</TextView>
</RelativeLayout>
</RelativeLayout>
From the RelativeLayout doc:
Class Overview
A Layout where the positions of the children can be described in relation to each other or to the parent.
Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM
Class documentation
Which is exactly your case. RelativeLayout can not do that.
For those looking for a solution to this, like I did, you can use FrameLayout instead of RelativeLayout.
Then you can set the gravity the intended object to bottom right as below
<TextView
android:layout_gravity="bottom|right"
android:text="FOOBARZ"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</TextView>
You have set the RelativeLayout to "wrap_content"
and the TextView to android:layout_alignParentBottom="true", so it automatically tries to stretch the RelativeLayout to the bottom. Don't use such dependencies with Relative Layout, as it can count as "circular dependencies".
From the docs for RelativeLayout:
Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a
RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.
Try to align your TextView to something other than the parent RelativeLayout, but watch out for this problem as well:
Circular dependencies, need some help with exact code
Alternatively, try to add more sophisticated inner layouts.
Dont use alight_Parent type properties with the child views
You can use frame layout instead of RelativeLayout with respective gravity
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_gravity="bottom|right"
android:text="Hello "
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</TextView>
</FrameLayout>
FrameLayout is usually good for placing different views one on top of each other (where the most recent child is on top of the previous child). In your case, you'd like to place views one next to each other (above, below, start, end), so I think ConstrainLayout fits better because it's exactly what it does.
Unlike RelativeLayout, you'd be able to set the ConstrainLayout width to wrap_content and still arrange its children views as you wish, for example instead of
android:layout_alignParentTop="true"
you can use
grid:layout_constraintTop_toTopOf="parent"
and instead of
android:layout_alignParentBottom="true"
you can use
grid:layout_constraintBottom_toBottomOf="parent"
Good answers. Now if you don't have layout_alignParentBottom="true" and still getting this issue watch out for android:background="#drawable/bkgnd" where bkgnd is a biggie.
I'm not sure why the clean and obvious way of accomplishing this hasn't been posted yet. This performant solution works for any View MyView with a known height.
Wrap your RelativeLayout with height wrap_content in a FrameLayout:
<!-- width here should constrain RelativeLayout -->
<FrameLayout
android:layout_width="#dimen/my_layout_width"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<MyView
...
android:layout_gravity="bottom" />
</FrameLayout>
Just note that the view at the bottom of the FrameLayout will be on top of your RelativeLayout content, so you'll need to add padding to the bottom of that layout to accomodate it. If you want that view to be variable height, you can either Subclass FrameLayout to add padding in code based on the measured view height, or just change the FrameLayout to vertical LinearLayout if you're not worried about the performance, i.e. it's not a listview item, or the views are relatively lightweight.
Not sure why all the answers here suggest FrameLayout, which is designed to render a single view or views layered in the z axis. OP's problem is a sequence of views stacked vertically, which should be in a LinearLayout.