Android text layout question: two textviews, side-by-side, with different layout alignments and weights

Posted by thx1200 on Stack Overflow See other posts from Stack Overflow or by thx1200
Published on 2010-12-27T01:47:50Z Indexed on 2010/12/27 1:53 UTC
Read the original article Hit count: 964

Filed under:
|

I'm still a bit of an Android noob, forgive me if this is simple and I'm just not seeing it.

There are two portions of text in a view that spans the entire width horizontally, but is only as high as one line of text. The left side must always be displayed in full, but should take no more horizontal space than it needs. The right side should be pushed over by the left side and fill up the remainder of the screen width. If the right side text is smaller than this width, the text should be right-aligned horizontally. If the text is greater than the width, it should scroll horizontally.

The text on the right side will be updated frequently and should slide up with new text when the app tells it (explaining the TextSwitcher in the layout).

I have tried two different layout styles. In both situations, I can get the left side to "push" the layout, the right side to scroll, but I can't figure out how to get the right side to right align. It is always left aligned. Here is a picture showing what is happening...

http://img10.imageshack.us/img10/5599/androidlayout.png

In addition (but less important), in my layout code I have android:fadingEdge="none" on the TextViews, but it still has a faded edge on the left and right side when it scrolls. Why is that?

Here are the two layouts I created, which yield the results shown, but not the results I want.

Using a horizontal LinearLayout...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayoutStatusBar"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2px"
    android:background="#555555"
>
    <TextView
        android:id="@+id/TextViewTimer"
        android:textSize="18px"
        android:textColor="#FFFFFF"
        android:layout_gravity="left"
        android:layout_weight="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0px"
        android:layout_marginRight="3px"
        android:text="Left Side"
    >
    </TextView>
    <TextSwitcher
        android:id="@+id/TextSwitcherDetails"
        android:inAnimation="@anim/push_up_in"
        android:outAnimation="@anim/push_up_out"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginLeft="3px"
        android:layout_marginRight="0px"
    >
        <TextView
            android:id="@+id/TextViewDetails1"
            android:textSize="18px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:fadingEdge="none"
            android:text="Right Side 1"
        >
        </TextView>
        <TextView
            android:id="@+id/TextViewDetails2"
            android:textSize="18px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:fadingEdge="none"
            android:text="Right Side 2 - This is a really long text this is long and fun and fun and long"
        >
        </TextView>
    </TextSwitcher>
</LinearLayout>

And the RelativeLayout style...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayoutStatusBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2px"
    android:background="#555555"
>
    <TextView
        android:id="@+id/TextViewTimer"
        android:textSize="18px"
        android:textColor="#FFFFFF"
        android:layout_gravity="left"
        android:layout_weight="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0px"
        android:layout_marginRight="3px"
        android:layout_alignParentLeft="true"
        android:text="Left Side"
    >
    </TextView>
    <TextSwitcher
        android:id="@+id/TextSwitcherDetails"
        android:inAnimation="@anim/push_up_in"
        android:outAnimation="@anim/push_up_out"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3px"
        android:layout_marginRight="0px"
        android:layout_toRightOf="@+id/TextViewTimer"
        android:layout_alignParentRight="true"
        android:fadingEdge="none"
        android:fadingEdgeLength="0px"
    >
        <TextView
            android:id="@+id/TextViewDetails1"
            android:textSize="18px"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:fadingEdge="none"
            android:fadingEdgeLength="0px"
            android:text="Right Side 1"
        >
        </TextView>
        <TextView
            android:id="@+id/TextViewDetails2"
            android:textSize="18px"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:fadingEdge="none"
            android:fadingEdgeLength="0px"
            android:text="Right Side 2 - This is a really long text this is long and fun and fun and long"
        >
        </TextView>
    </TextSwitcher>
</RelativeLayout>

So how do I get that text on the right side to right-align. Thanks!

© Stack Overflow or respective owner

Related posts about android

Related posts about android-layout