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

AsyncTask: Only the original thread that created a view hierarchy can touch its views. ( android.view.ViewRootImpl$CalledFromWrongThreadException:)
Parse complex HTML into Android TextView
How to avoid this issue is EditText?
Random Orange border appearing on android
move event was interrupted by another touch event
Problems after setting/declaring Custom OverflowButton Style. Please see the details
Can you view your applications SQLite database remotely?
How to restart android adb using terminal on macOS?
my activity xml can't see com.google.android.gms.maps.SupportMapFragment
How is the title bar hidden?
Find if the Layout has particular text view or not
Using http instead of https in AppEngine endpoint to avoid SSLHandshakeException
onServiceConnected is not called on Android 5.1.1 using InAppBillingService
How would you create a reusable component for this layout?
GoogleAccountCredential name is null despite calling setSelectedAccountName (Android 6.0)
Android 6 getAccountName() Missing android.permission.GET_ACCOUNTS

Categories

HOME
grok
jax-rs
owl-carousel
xbee
nsbundle
android-json
qpython
openstreetmap
drop-down-menu
frequency-distribution
cumulocity
docx
parameter-passing
keytool
linear-algebra
atlassian-stash
sse
cgbitmapcontext
dataflow-diagram
monit
android-maps
intersystems-cache
failover
cognos-bi
sql-server-2000
catch-unit-test
activexobject
installscript
navigator
parse-tree
xcode7.3
graphhopper
setup.py
comparison-operators
ogc
vmware-fusion
levenshtein-distance
microsoft-metro
avcapturedevice
photos
opencmis
automapper-5
geotiff
singleinstance
reindex
otp
oozie-coordinator
cd-burning
ampps
testrail
vcloud-director-rest-api
simplepie
pacemaker
xcode-server
installshield-2010
avspeechsynthesizer
jclouds
findfirst
cycle2
concept-insights
atan2
forwarding
nsurlsessiondatatask
zxspectrum
delayed-execution
gii
cuba
functional-java
gnumeric
memset
clickbank
cffile
cnf
kaleidoscope
stacky
alice
nokia-s40
fragmenttransaction
thttpd
android-holo-everywhere
load-time
object-database
web-garden
opml
echo3
software-lifecycle
dsoframer

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