android


Android limit a text view length


I have a layout containing two text views and an image button, like below figure.
This is the code of the layout
<LinearLayout
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/gc_background"
android:gravity="center"
android:padding="6dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
</LinearLayout>
My problem is that if the text in either of the text views gets too long, the button "gets pushed away", like the figure below.
Instead, I would like that the text view limits the text contained, something like the red underline in the figure below.
How do I do this?
The trick is to use android:ellipsize="end" and limit number of lines with android:maxLines="1".
Also, to prevent your ImageButton from getting pushed away, the easiest way is to set android:layout_weight="1" to the LinearLayout with TextViews and android:layout_width="wrap_content" to the LinearLayout containing the button.
This is the result:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/gc_background"
android:gravity="center"
android:padding="6dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:layout_weight="1"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="This is a long text that will take up a lot of space and push the button away"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
</LinearLayout>
try to set to your TextView set together
android:ellipsize="end"
android:singleLine="true"
try this
Add
android:ellipsize="end"
android:singleLine="true"
to TextView you want to be single line and with "..." at end.
try like this,
<LinearLayout
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/gc_background"
android:gravity="center"
android:padding="6dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
</LinearLayout>
Try this way.. Added weight to layout..
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:layout_weight="3"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
Try using this layout:
<RelativeLayout
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/gc_background"
android:padding="6dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2"
android:layout_toLeftOf="#id/ibGlobalControlPlayPause"
android:layout_toStartOf="#id/ibGlobalControlPlayPause" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:singleLine="true"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_pause_over_video" />
</RelativeLayout>

Related Links

Android : How to integrate dialer in app like true caller
How get save apk name in server on android?
OpenCV Compare 2 Images and return result based on Ranking order
Creating multiple instances of xml android layout
How to retrieve information from database using name
Transferring data from bluetooth to the android app
Post in retrofit [duplicate]
Android - Crash when clicking on RecyclerView when SwipeRefreshLayout loading
common runonUIthread for whole application
how to Fused location Run in Background service continuously
React-Native Twitter login for Android
How to dismiss progress bar once otp read from Inbox in android?
Idling resource exits even when isIdleNow() returns false in espresso
When adding Facebook SDK “ appeventslogger.activateapp(this)” is shown deprecated
Android marshmallow WifiManager leaking IntentReceiver
Adding ffmpeg OMX codec to Genymotion Android 4.4.2 emulator

Categories

HOME
eclipse
time-complexity
merge
datastore
zend-framework
alsa
limit
pearson-correlation
flume
special-characters
teechart
extjs4.2
gson
ycsb
onedrive-api
cumulocity
shortest-path
drive
xunit
android-sharedpreferences
ms-access-web-app
outliers
bringtofront
autodesk-designautomation
anki
scatter3d
database-comparison
testbed
turfjs
quickfixj
carriage-return
installscript
arduino-ide
alphabetical
amazon-ses
connection-pool
setup.py
branching-and-merging
procfile
pom.xml
jquery-ui-draggable
objectscript
filenet
branch-prediction
uicolor
neon
beamer
dup
appscale
project-organization
nupic
crouton-os
decimalformat
phonertc
binomial-theorem
slash
lasso
vacuum
mirror
j-security-check
broadband
showdialog
machine-instruction
psr-4
paste
vsx
authlogic
vbe
synapse
gd-graph
mongohq
beaker-testing
flex++
process-explorer
epiceditor
swfobject
web-notifications
vfs
relative
denied
role
android-holo-everywhere
xcelsius
google-ajax-api
entitydatasource
tournament
xui
non-clustered-index
folding
zend-rest
mathematical-notation

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App