12

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>
Swayam
  • 16,294
  • 14
  • 64
  • 102
Eugene
  • 59,186
  • 91
  • 226
  • 333

5 Answers5

18

Try to use gravity instead of layout_gravity.

poitroae
  • 21,129
  • 10
  • 63
  • 81
  • 7
    True. But (!) isn't the gravity for components inside layout, and layout_gravity for the View itself? That's the reason I'm confused. – Eugene Aug 29 '12 at 19:11
  • 2
    @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". – poitroae Aug 29 '12 at 19:14
  • 1
    I just set layout_width to wrap_content and changed it back to layout_gravity="right" and this doens't work. – Eugene Aug 29 '12 at 19:17
  • 1
    @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`. – poitroae Aug 29 '12 at 19:22
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.

dpetruha
  • 1,214
  • 12
  • 12
7

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

Sam Chen
  • 7,597
  • 2
  • 40
  • 73
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"/>

Sumant
  • 2,775
  • 2
  • 22
  • 30
  • gravity applies inside to the view & layout_gravity applies inside the container . I hope your problem is solved? :) – Sumant Aug 29 '12 at 19:33
  • @siik just check : http://stackoverflow.com/questions/3482742/android-gravity-and-layout-gravity http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/ – Sumant Aug 29 '12 at 19:35
  • 4
    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. – Jamol Nov 19 '13 at 16:07
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

idanakav
  • 1,466
  • 1
  • 13
  • 23