Site icon C1CTech

Android TextView ellipsize property example

&NewLine;<p>This article is about how to work with Android TextView ellipsize property&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Android Ellipsize<&sol;strong><&sol;span><&sol;h3> &NewLine;<p>Android TextView <strong><span style&equals;"color&colon; &num;008000&semi;">ellipsize<&sol;span><&sol;strong> property Causes words in the text that are longer than the view&&num;8217&semi;s width to be ellipsized &lpar; means to shorten text using an <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"http&colon;&sol;&sol;en&period;wikipedia&period;org&sol;wiki&sol;Ellipsis" rel&equals;"noreferrer">ellipsis<&sol;a><&sol;span><&sol;strong>&comma; i&period;e&period; three dots &&num;8230&semi;&rpar; instead of broken in the middle to fit it inside the given view&period;<&sol;p> &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Ellipsize property values<&sol;strong><&sol;span><&sol;h3> &NewLine;<p class&equals;"p1">You can use the following values for ellipsize property&colon;<&sol;p> &NewLine;<h4 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Ellipsize end<&sol;b><&sol;span><&sol;h4> &NewLine;<p class&equals;"p1">This will truncate your text from the end and put three dots at the end&period; <&sol;p> &NewLine;<p><img class&equals;"aligncenter wp-image-2631" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;06&sol;Screenshot&lowbar;1624957158-e1624962592749&period;png" alt&equals;"" width&equals;"379" height&equals;"367" &sol;><&sol;p> &NewLine;<h5 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">XML<&sol;span><&sol;strong><&sol;h5> &NewLine;<p class&equals;"p1">Set <strong><span style&equals;"color&colon; &num;0000ff&semi;">maxLines<&sol;span><&sol;strong> to 1 and set <span style&equals;"color&colon; &num;0000ff&semi;"><strong>ellipsize<&sol;strong><&sol;span> to <span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;end&&num;8221&semi;<&sol;strong><&sol;span><&sol;p> &NewLine;<pre>&lt&semi;TextView<br &sol;>android&colon;id&equals;"&commat;&plus;id&sol;textView"<br &sol;>android&colon;layout&lowbar;width&equals;"0dp"<br &sol;>android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;>android&colon;layout&lowbar;margin&equals;"20dp"<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;ellipsize&equals;"end"<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;maxLines&equals;"1"<&sol;strong><&sol;span><br &sol;>android&colon;text&equals;"The quick brown fox jumps over the lazy dog&period;"<br &sol;>android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;dark"<br &sol;>android&colon;textSize&equals;"25sp"<br &sol;>android&colon;textStyle&equals;"bold"<br &sol;>app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&sol;&gt&semi;<&sol;pre> &NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Kotlin Code<&sol;strong><&sol;span><&sol;h5> &NewLine;<p class&equals;"p1">Find the textview&period; Then <span style&equals;"color&colon; &num;0000ff&semi;"><b>setMaxLines<&sol;b><&sol;span> to 1 and <span style&equals;"color&colon; &num;0000ff&semi;"><b>setEllipsize<&sol;b><&sol;span> to <span style&equals;"color&colon; &num;008000&semi;"><b>TextUtils&period;TruncateAt&period;END<&sol;b><&sol;span>&period;<&sol;p> &NewLine;<pre>textView&period;ellipsize &equals; TextUtils&period;TruncateAt&period;END<br &sol;>textView&period;maxLines &equals; 1<&sol;pre> &NewLine;<h4 class&equals;"p2"><strong><span style&equals;"color&colon; &num;000080&semi;">Ellipsize start<&sol;span><&sol;strong><&sol;h4> &NewLine;<p class&equals;"p1">This will keep the ending portion of your text and replace the starting part with three dots&period;<&sol;p> &NewLine;<p><img class&equals;"wp-image-2633 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;06&sol;Screenshot&lowbar;1624957663-e1624965022816&period;png" alt&equals;"" width&equals;"458" height&equals;"409" &sol;><&sol;p> &NewLine;<h5 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">XML<&sol;span><&sol;strong><&sol;h5> &NewLine;<p class&equals;"p1">Set <strong><span style&equals;"color&colon; &num;0000ff&semi;">singleLine<&sol;span><&sol;strong> to &&num;8220&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">true&&num;8221&semi;<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>ellipsize<&sol;strong><&sol;span> to <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;start&&num;8221&semi;<&sol;span><&sol;strong>&period;<&sol;p> &NewLine;<pre>&lt&semi;TextView<br &sol;>android&colon;id&equals;"&commat;&plus;id&sol;textView"<br &sol;>android&colon;layout&lowbar;width&equals;"0dp"<br &sol;>android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;>android&colon;layout&lowbar;margin&equals;"20dp"<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;ellipsize&equals;"start"<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;singleLine&equals;"true"<&sol;strong><&sol;span><br &sol;>android&colon;text&equals;"The quick brown fox jumps over the lazy dog&period;"<br &sol;>android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;dark"<br &sol;>android&colon;textSize&equals;"25sp"<br &sol;>android&colon;textStyle&equals;"bold"<br &sol;>app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&sol;&gt&semi;<&sol;pre> &NewLine;<h5 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Kotlin Code<&sol;span><&sol;strong><&sol;h5> &NewLine;<p class&equals;"p1">Find the textView&comma; set <strong><span style&equals;"color&colon; &num;0000ff&semi;">singleLine&comma;<&sol;span><&sol;strong> and then set the <strong><span style&equals;"color&colon; &num;0000ff&semi;">ellipsize<&sol;span><&sol;strong> to <span style&equals;"color&colon; &num;008000&semi;"><b>TextUtils&period;TruncateAt&period;START<&sol;b><&sol;span><&sol;p> &NewLine;<pre>textView&period;ellipsize &equals; TextUtils&period;TruncateAt&period;START<br &sol;>textView&period;isSingleLine &equals; true<&sol;pre> &NewLine;<h4 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Ellipsize middle<&sol;b><&sol;span><&sol;h4> &NewLine;<p class&equals;"p1">This will keep the starting and ending of the text and place three dots in the middle&period; <&sol;p> &NewLine;<p><img class&equals;"wp-image-2632 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;06&sol;Screenshot&lowbar;1624957454-e1624965364485&period;png" alt&equals;"" width&equals;"518" height&equals;"458" &sol;><&sol;p> &NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>XML<&sol;strong><&sol;span><&sol;h5> &NewLine;<p class&equals;"p1">Set <strong><span style&equals;"color&colon; &num;0000ff&semi;">singleLine<&sol;span><&sol;strong> to <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>ellipsize<&sol;strong><&sol;span> to <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;middle&&num;8221&semi;<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<pre>&lt&semi;TextView<br &sol;>android&colon;id&equals;"&commat;&plus;id&sol;textView"<br &sol;>android&colon;layout&lowbar;width&equals;"0dp"<br &sol;>android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;>android&colon;layout&lowbar;margin&equals;"20dp"<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;ellipsize&equals;"middle"<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;singleLine&equals;"true"<&sol;strong><&sol;span><br &sol;>android&colon;text&equals;"The quick brown fox jumps over the lazy dog&period;"<br &sol;>android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;dark"<br &sol;>android&colon;textSize&equals;"25sp"<br &sol;>android&colon;textStyle&equals;"bold"<br &sol;>app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&sol;&gt&semi;<&sol;pre> &NewLine;<h5 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Kotlin Code<&sol;span><&sol;strong><&sol;h5> &NewLine;<p class&equals;"p1">Find the textView&comma; set <strong><span style&equals;"color&colon; &num;0000ff&semi;">singleLine <&sol;span><&sol;strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;000000&semi;">to<&sol;span><&sol;span><span style&equals;"color&colon; &num;008000&semi;"><strong> &&num;8220&semi;true&&num;8221&semi;<&sol;strong><&sol;span> set the <strong><span style&equals;"color&colon; &num;0000ff&semi;">ellipsize<&sol;span><&sol;strong> to <span style&equals;"color&colon; &num;008000&semi;"><b>TextUtils&period;TruncateAt&period;MIDDLE<&sol;b><&sol;span><&sol;p> &NewLine;<pre>textView&period;ellipsize &equals; TextUtils&period;TruncateAt&period;MIDDLE<br &sol;>textView&period;isSingleLine &equals; true<&sol;pre> &NewLine;<h4 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Ellipsize marquee<&sol;b><&sol;span><&sol;h4> &NewLine;<p class&equals;"p1">It will make your text automatically scroll across the screen horizontally&period; There are no three dots involved in it&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-2669" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1625570806-576x1024&period;png" alt&equals;"" width&equals;"329" height&equals;"586" &sol;>  <img class&equals;"alignnone wp-image-2670" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1625570814-576x1024&period;png" alt&equals;"" width&equals;"332" height&equals;"590" &sol;><&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;<h5 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">XML<&sol;span><&sol;strong><&sol;h5> &NewLine;<p class&equals;"p1">Set <span style&equals;"color&colon; &num;0000ff&semi;"><strong>singleLine<&sol;strong><&sol;span> to <strong><span style&equals;"color&colon; &num;008000&semi;">true <&sol;span><&sol;strong>and <strong><span style&equals;"color&colon; &num;0000ff&semi;">ellipsize<&sol;span><&sol;strong> to <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;marquee&&num;8221&semi;<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;TextView<br &sol;>android&colon;id&equals;"&commat;&plus;id&sol;textView"<br &sol;>android&colon;layout&lowbar;width&equals;"0dp"<br &sol;>android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;>android&colon;layout&lowbar;margin&equals;"20dp"<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;ellipsize&equals;"marquee"<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;singleLine&equals;"true"<&sol;strong><&sol;span><br &sol;>android&colon;text&equals;"The quick brown fox jumps over the lazy dog&period;The quick brown fox jumps over the lazy dog&period;"<br &sol;>android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;dark"<br &sol;>android&colon;textSize&equals;"25sp"<br &sol;>android&colon;textStyle&equals;"bold"<br &sol;>app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"<br &sol;>app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<&sol;pre> &NewLine;<p class&equals;"p1">You will also have to set the textView as selected using kotlin code&period;<&sol;p> &NewLine;<pre>textView&period;isSelected &equals; true<&sol;pre> &NewLine;<h5 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Kotlin Code<&sol;span><&sol;strong><&sol;h5> &NewLine;<p class&equals;"p1">Instead of doing half thing from XML and half from kotlin code&comma; you can make your text scroll automatically across the screen using kotlin code only&period;<&sol;p> &NewLine;<pre>textView&period;ellipsize &equals; TextUtils&period;TruncateAt&period;MARQUEE<br &sol;>textView&period;isSingleLine &equals; true<br &sol;>textView&period;isSelected &equals; true<&sol;pre> &NewLine;<h4 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Make textView manually scrollable across the screen<&sol;b><&sol;span><&sol;h4> &NewLine;<p class&equals;"p1">The ellipsize marquee makes the text automatically scroll across the screen but if you want to give the scrolling control to the user&comma; you can put your TextView inside a HorizontalScrollView and set the textView’s <strong><span style&equals;"color&colon; &num;0000ff&semi;">scrollHorizontally<&sol;span><&sol;strong> property to <span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;true&&num;8221&semi;<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<pre>&lt&semi;HorizontalScrollView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;hs&lowbar;textview"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;margin&equals;"10dp"&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;textView"<br &sol;> android&colon;layout&lowbar;width&equals;"0dp"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;padding&equals;"10dp"<br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;scrollHorizontally&equals;"true"<&sol;strong><&sol;span><br &sol;> android&colon;text&equals;"The quick brown fox jumps over the lazy dog&period;The quick brown fox jumps over the lazy dog&period;"<br &sol;> android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;dark"<br &sol;> android&colon;textSize&equals;"25sp"<br &sol;> android&colon;textStyle&equals;"bold"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;HorizontalScrollView&gt&semi;<&sol;pre> &NewLine;<p><img class&equals;"aligncenter wp-image-2668 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;manually-scrollable&lowbar;textview-e1625570028266-1024x764&period;png" alt&equals;"" width&equals;"458" height&equals;"342" &sol;>&NewLine;

Exit mobile version