layout_gravity not working
Asked Answered
R

5

13

Why my TextView doesn't go right?

Update: Well, now I don't just need to set TextView to the right. Now it is very interesting why layout_gravity doesn't work as expected, namely - set the View to the position inside it parent container.

enter image description here

<?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="wrap_content"
              android:minHeight="?android:attr/listPreferredItemHeight"
              style="@style/activated_item"
              android:orientation="horizontal">

    <CheckBox
        android:id="@+id/star"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="8dp"
        style="?android:attr/starStyle"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginLeft="8dp"
        android:layout_gravity="right" //////// HERE I AM 
        android:textStyle="bold"
        android:textColor="@color/item_text_color"/>

</LinearLayout>
Regin answered 29/8, 2012 at 19:8 Comment(2)
where you want to set textview at extream right of linearlayout? can u just explain thatHenryson
@SUmant, yes, I want to set textview to the very right.Regin
N
20

Try to use gravity instead of layout_gravity.

Non answered 29/8, 2012 at 19:10 Comment(4)
True. But (!) isn't the gravity for components inside layout, and layout_gravity for the View itself? That's the reason I'm confused.Regin
@siik Yep, that is correct! Your TextView's width matches its parent (you specified it!), and therefore there is no sense in setting a layout_gravity. Set the android:background to a random color to see how large your TextView actually is. Alternatively, set android:layout_width to wrap_content, then it'll work with `layout_gravity".Non
I just set layout_width to wrap_content and changed it back to layout_gravity="right" and this doens't work.Regin
@siik Your LinearLayout's orientation is horizontal. Make it vertical. And yes, the layout_gravity thing won't work, because you are using a LinearLayout. Use a RelativeLayout if you want to use the wrap_content. Otherwise stick with android:gravity.Non
B
7

I have found 2 solutions to that:

  1. Set android:orientation="vertical".
  2. Use FrameLayout instead of LinearLayout as the parent for your text/checkbox widgets. It is not primary usage of FrameLayout but that advice is also noted in Android documentation:

    ... You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

Beatitude answered 25/3, 2018 at 18:24 Comment(1)
But is a FrameLayout can always be interchangeably used with LinearLayout ?Fleisig
S
7

Because you set orientation to horizontal, which means you cannot manually change horizontal position of your child views.

Streusel answered 16/9, 2019 at 16:13 Comment(0)
H
0

try this...

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:minHeight="?android:attr/listPreferredItemHeight"
          style="@style/activated_item"
          android:orientation="horizontal">

<CheckBox
    android:id="@+id/star"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="8dp"
    style="?android:attr/starStyle"/>

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_marginLeft="40dp"
    android:gravity="right" //////// HERE I AM 
    android:textStyle="bold"
    android:textColor="@color/item_text_color"/>

Henryson answered 29/8, 2012 at 19:25 Comment(3)
gravity applies inside to the view & layout_gravity applies inside the container . I hope your problem is solved? :)Henryson
@siik just check : #3483242 thinkandroid.wordpress.com/2010/01/14/…Henryson
Please don't say "try this" and post the code with your slight modification. Just tell what needs to be changed. Otherwise, one has to go through every line and compare with the original to find out what you did.Meredi
R
0

you could use weight also try this one

<?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="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal" >

<CheckBox
    android:id="@+id/star"
    style="?android:attr/starStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="8dp"
    android:layout_weight="1" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="8dp"
    android:text="TEST"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textStyle="bold" />

</LinearLayout>

the gravity is just to center the text ,what makes it go right is the weight

Relinquish answered 29/8, 2012 at 19:33 Comment(2)
Thanks, but I'm more interested why 'layout_gravity' does not work as expected.Regin
sandipchitale.blogspot.co.il/2010/05/… read hereRelinquish

© 2022 - 2024 — McMap. All rights reserved.