layout_gravity isn't working as expected - just partially

layout_gravity isn't working as expected - just partially



I have the following layout:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:text="@string/title_day"
android:textStyle="bold"
android:textSize="20sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:text="Tuesday"
android:textSize="16sp" />

</LinearLayout>

<TextView
android:id="@+id/tv_fail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+22"
android:textSize="16sp"
android:layout_gravity="end|center_vertical" />

</LinearLayout>

</LinearLayout>



And this looks like this:



enter image description here



And I want the last TextView with id tv_fail to be pinned to right end of screen. I suppose that


tv_fail


android:layout_gravity="end|center_vertical"



should handle it, but this instruction centers TextView vertically, but doesn't move it to end. How to fix it?




2 Answers
2



I suppose that


android:layout_gravity="end|center_vertical"



should handle it, but this instruction centers TextView vertically, but doesn't move it to end.



This is due to how LinearLayout works; your understanding of layout_gravity is generally correct.


LinearLayout


layout_gravity



LinearLayout takes its children and lays them out in a line, and the children will all be packed towards the "start" of the LinearLayout. In other words, a horizontal LinearLayout will ignore the horizontal component of the layout_gravity attribute of a child.


layout_gravity



There are a few ways to work around this. The one that I think works best for your scenario is to make the TextView stretch to fill all the remaining space in the LinearLayout (using layout_weight), and then have the TextView position its text at the end of its content area (using gravity).


layout_weight


gravity


<TextView
android:id="@+id/tv_fail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+22"
android:textSize="16sp"
android:layout_gravity="center_vertical"
android:gravity="end"/>





Thank you! I did the same even without layout_weight, just with layout_width="match_parent" and gravity="end". But why does LinearLayout ignore layout_gravity?
– Ksenia
Sep 5 '18 at 17:45





layout_gravity is a sort of "general purpose" attribute that different ViewGroup subclasses interpret differently. LinearLayout already knows how to align its children along its primary axis (vertically or horizontally), so it ignores that component of the layout_gravity attribute. RelativeLayout and ConstraintLayout will ignore the attribute altogether! You'll just have to experiment to get used to how the attribute behaves for different parents, though once you've done it enough you'll get a general sense for what to expect.
– Ben P.
Sep 5 '18 at 17:48


layout_gravity


layout_gravity



If you just want to set text inside of your last TextView to end then you should use :


text


TextView


android:gravity="end"



for TextView gravity use:


TextView


android:layout_gravity="center"



Difference between android:gravity & android:layout_gravity is that first one arrange content inside of any view with given gravity while second one arranges view according to given gravity.


android:gravity


android:layout_gravity



Updated code:




<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:text="title_day"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:text="Tuesday"
android:textSize="16sp" />

</LinearLayout>

<TextView
android:id="@+id/tv_fail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="+22"
android:textSize="16sp" />

</LinearLayout>







No, I know this, but I want to set TextView widget to end of layout, not to set text inside of the last TextView to end.
– Ksenia
Sep 5 '18 at 17:40






Do you want it to be created by LinearLayout only? Try using ConstraintLayout. It is more flexible than this. Let me know if you want it to be created by ConstraintLayout.
– Jeel Vankhede
Sep 5 '18 at 17:46


LinearLayout


ConstraintLayout


ConstraintLayout





No, thanks, I know how to do it with ConstraintLayout, but it was my intent to create this layout namely with LinearLayout.
– Ksenia
Sep 5 '18 at 17:47



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)