user1026605
user1026605

Reputation: 1430

Android: automatically truncate a text when no more space is available in a LinearLayout

I want to create some kind of toolBar in my app. On the left I'm displaying a fixed size imageView, on the right another one and in the middle I want to display a text, but it need to be truncated in case there isn't enought space... and that's where I'm failing

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/actionBar"
    android:layout_width="match_parent"
    android:layout_height="32dp"
    android:background="@android:drawable/title_bar"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="left|center_vertical"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/actionBarIcon"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginRight="7dp"
            android:adjustViewBounds="true"
            android:background="@drawable/podcast_addict"
            android:gravity="center_vertical"
            android:onClick="onHome" >
        </ImageButton>

        <TextView
            android:id="@+id/actionBarTitle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:singleLine="true"
            android:text="@string/app_name"
            android:textColor="#FFFFFF"
            android:textSize="15sp"
            android:textStyle="bold" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_weight="2"
        android:gravity="right|center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="3dp"
            android:background="@android:drawable/status_bar_item_app_background" />

        <ImageView
            android:id="@+id/actionButton"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:adjustViewBounds="true"
            android:background="@android:drawable/stat_notify_sync_noanim"
            android:gravity="center_vertical"
            android:onClick="onAction" >
        </ImageView>

        <ImageView
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp"
            android:background="@android:drawable/status_bar_item_app_background" />

        <ImageButton
            android:id="@+id/showHideButton"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:adjustViewBounds="true"
            android:background="@android:drawable/ic_menu_view"
            android:gravity="center_vertical"
            android:onClick="onShowHide" >
        </ImageButton>

        <ImageView
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp"
            android:background="@android:drawable/status_bar_item_app_background" />

        <ImageButton
            android:id="@+id/markReadButton"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:adjustViewBounds="true"
            android:background="@android:drawable/checkbox_on_background"
            android:gravity="center_vertical"
            android:onClick="onMarkRead" >
        </ImageButton>
    </LinearLayout>

</LinearLayout>

when the text is short it seems to work, but when it's too long the 1st imageView on the right is hidden behind the text... How can I fix this ?

Upvotes: 1

Views: 4410

Answers (3)

yorkw
yorkw

Reputation: 41126

Try using android:ellipsize:

<TextView
  android:singleLine="true"
  android:ellipsize="end"
  android:text="This is a long long text"/>

This will give you This is a long... on your screen when text is longer than TextView width.

Upvotes: 5

Kaediil
Kaediil

Reputation: 5535

Try this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionBar"
android:layout_width="fill_parent"
android:layout_height="32dp"
android:background="@android:drawable/title_bar"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginLeft="15dp"
    android:layout_weight="2"
    android:layout_gravity="right|center_vertical"
    android:layout_alignParentRight="true"
    android:orientation="horizontal" 
    android:id="@+id/buttons">

    <ImageView
        android:layout_width="1px"
        android:layout_height="fill_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="3dp"
        android:background="@android:drawable/status_bar_item_app_background" />

    <ImageView
        android:id="@+id/actionButton"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:adjustViewBounds="true"
        android:background="@android:drawable/stat_notify_sync_noanim"
        android:gravity="center_vertical"
        android:onClick="onAction" >
    </ImageView>

    <ImageView
        android:layout_width="1px"
        android:layout_height="fill_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginTop="3dp"
        android:background="@android:drawable/status_bar_item_app_background" />

    <ImageButton
        android:id="@+id/showHideButton"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:adjustViewBounds="true"
        android:background="@android:drawable/ic_menu_view"
        android:gravity="center_vertical"
        android:onClick="onShowHide" >
    </ImageButton>

    <ImageView
        android:layout_width="1px"
        android:layout_height="fill_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginTop="3dp"
        android:background="@android:drawable/status_bar_item_app_background" />

    <ImageButton
        android:id="@+id/markReadButton"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:adjustViewBounds="true"
        android:background="@android:drawable/checkbox_on_background"
        android:gravity="center_vertical"
        android:onClick="onMarkRead" >
    </ImageButton>
</LinearLayout>
<LinearLayout
    android:layout_toLeftOf="@id/buttons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="left|center_vertical"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/actionBarIcon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginRight="7dp"
        android:adjustViewBounds="true"
        android:background="@drawable/podcast_addict"
        android:gravity="center_vertical"
        android:onClick="onHome" >
    </ImageButton>

    <TextView
        android:id="@+id/actionBarTitle"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:text="loooooooooooooooooooooooooooooooooooooooooooooooong"
        android:textColor="#FFFFFF"
        android:textSize="15sp"
        android:textStyle="bold" >
    </TextView>
</LinearLayout>

Upvotes: 0

triggs
triggs

Reputation: 5900

Use layout weights on the linearlayouts

android:layout_weight="1"

will give both views equal space in the layout ie half and half, you can play around with the weights until you find the right balance for what you want.

Upvotes: 2

Related Questions