String
String

Reputation: 3728

Limit the text in TextView

I have a textview Which Contains some text changes dynamically within the UI like below

layout:

<TableRow android:weightSum="2" >

            <ImageButton
                android:id="@+id/archive"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="90dp"
                android:background="@drawable/archive" />

            <ImageButton
                android:id="@+id/facebook"
                android:layout_width="-200dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:background="@drawable/facebook" />
        </TableRow>

        <TableRow android:layout_marginTop="2dp" >

            <TextView
                android:id="@+id/titlename1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="4dp"
                android:text="@string/nowplaying"
                android:textColor="#000000"
                android:textSize="12sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/titlename"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="4dp"
                android:layout_weight="1"
                android:ellipsize="none"
                android:gravity="right"
                android:maxEms="25"
                android:maxLines="1"
                android:scrollHorizontally="false"
                android:textColor="#000000"
                android:textSize="11dp" />
        </TableRow>

        <TableRow android:layout_marginTop="2dp" >

            <FrameLayout
                android:id="@+id/mainlayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginLeft="0dp"
                android:orientation="vertical" >

                <ImageButton
                    android:id="@+id/playbtn"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="@drawable/play" />

                <ImageButton
                    android:id="@+id/pausebtn"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="@drawable/pause" />
            </FrameLayout>

            <SeekBar
                android:id="@+id/seekBar1"
                android:layout_width="500dp"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/mainlayout"
                android:layout_marginLeft="-203dp"
                android:layout_marginRight="0dp" />
        </TableRow>

My Problem is that when i execute my Program,my layout output:

Imagebutton1     ImageButton2

NowPlaying:    SongDetails

Play/Pause      SeekBar

Problem is If my SongDetails text lenght Contains 2 or 3 lines My layout is getting Disturbed?Could any one Suggest?My Requirement is to limit the text like

Song Detailssss etc......in a Same Row

Upvotes: 1

Views: 364

Answers (5)

ajaykoppisetty
ajaykoppisetty

Reputation: 370

Try setting android:ellipsize paremeter to "end" if you want to trim the content at the end.

android:ellipsize="end"

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 82958

Simply use android:singleLine="true", and android:ellipsize="end". So You need to update TextView code as

<TextView
                .....
....
                android:ellipsize="end"
               android:singleLine="true" />

Refrences :

  1. android:singleLine
  2. android:ellipsize

Upvotes: 4

Anickyan
Anickyan

Reputation: 414

Just remove wrap_content. That should work(haven't tried it myself recently, but I used it a couple of months ago).

Otherwise, you could cut the text manually in Java, and cut it at the first newline found.

Upvotes: -1

CRUSADER
CRUSADER

Reputation: 5472

Try setting android:ellipsize paremeter to "end"

Like this

android:ellipsize="end"

Upvotes: 1

Gunaseelan
Gunaseelan

Reputation: 15525

try to use

android:singleLine="true"

in your textview

also you can set marquee.

Note that this marquee will work if your text line is longer then your screen size only.

Upvotes: 1

Related Questions