tags:

views:

219

answers:

6

I have a simple LinearLayout with one TextView and one ImageView. I want the text in the TextView to be aligned to the right, but the result show that the Text was aligned to the left. Is there anything wrong with my layout xml? Thanks.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:background="#E8E3E4">
    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        >
        <TextView android:layout_width="260dp" android:layout_height="wrap_content"
            android:text="More Comments" android:textStyle="bold" android:background="#ff0000"
            android:textColor="#121222" android:layout_gravity="right" />
        <ImageView android:layout_width="50dp"
            android:layout_height="wrap_content" android:src="@drawable/arrow_right"
            android:layout_gravity="center" android:scaleType="centerInside" />
    </LinearLayout>
</LinearLayout>
+1  A: 

I've done only a few projects with android, but android:layout_gravity specifies the alignment for the View within the parent, android:gravity specifies the alignment of the Views contained within the View whose gravity you are specifying. Make sure you have the proper one specified. The documentation does not distinguish between these two.

It would be easier to help you if you posted some sample XML.

EDIT:

I see you have posted the XML now. It does look like you have the proper attribute set, but if you are aligning the text to the right, why would you put it first in the linear layout? I suggest changing it to go left to right.

Smelch
+2  A: 

LinearLayouts with a horizontal orientation don't support right/left gravity. You can find a workaround here.

Michael Krauklis
A: 

Here is some code I wrote for my project. It accomplishes what you're looking for. I'd just change the control types and such and you should be off and running.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:background="#ffdddddd">
    <TextView android:layout_width="260dp" 
        android:layout_height="wrap_content"
        android:text="More Comments" 
        android:textStyle="bold" 
        android:background="#ff0000"
        android:textColor="#121222" 
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true" />
    <ImageView android:layout_width="50dp"
        android:layout_height="wrap_content" 
        android:src="@drawable/arrow_right"
        android:scaleType="centerInside"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>
Jared
Hi Jared, I tried your code by replace ToogleButton with TextView. It didn't work: the text in the TextView was not aligned to the right.
This should work just copy it over.
Jared
A: 

Thanks, Michael. But the workaround you mentioned doesn't work either.

I tried to use RelativeLayout, but it still gave me result like: More Comments (Icon)

What I expected is More Comments (Icon)

The RelativeLayout xml is below. Is there anything wrong with it?

<?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="fill_parent">
    <TextView android:id="@+id/label" android:layout_width="260dp"
        android:layout_height="wrap_content" android:text="More Comments"
        android:textStyle="bold" android:background="#ff0000"
        android:textColor="#121222" android:layout_gravity="right" />
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content"
        android:src="@drawable/arrow_right" android:layout_gravity="center"
        android:scaleType="centerInside" android:layout_toRightOf="@id/label" />
</RelativeLayout>
A: 

Following the workaround in Michael's comment, I have the XML below. I expected the output to be:

     RightLeft

but the actual output is:

Right             Left

So the workaround doesn't work for me. Any thoughts?

   <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
           <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_gravity="right"
                   android:layout_weight="1.0"                       
                   android:text="RIGHT"/>
           <TextView
                   android:layout_width="wrap_content"

                   android:layout_height="wrap_content"
                   android:layout_gravity="left"
                   android:text="LEFT"/>
   </LinearLayout>

+1  A: 

I figured out what is wrong with my xml. I should have used gravity instead of layout_gravity to right-align the text in TextView. The following XML works as expected.

<?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="fill_parent">
    <TextView android:id="@+id/label" android:layout_width="260dp"
        android:layout_height="wrap_content" android:text="More Comments"
        android:textStyle="bold" android:background="#ff0000"
        android:textColor="#121222" android:gravity="right" />
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content"
        android:src="@drawable/arrow_right" android:layout_gravity="center"
        android:scaleType="centerInside" android:layout_toRightOf="@id/label" />
</RelativeLayout>
Then you should mark this answer as 'accepted' (click the tick-mark to the left of the question).
David Thomas