Reputation: 12385
I have an EditText
view with a bounded size of 300dp * 300dp.
This EditText is used to show a preview of some descriptions received from a server, so if the text is too long appears cut in the last line. I want that in case of too long text, in the end of the last fully visible line are showed suspension points to avoid bad truncation
example
if for the text
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.
If the max visible text is only
Lorem ipsum dolor sit amet, consectetur adipisici elit,
The EditText should show
Lorem ipsum dolor sit amet, consectetur adipisici...
Any solutions?
Upvotes: 0
Views: 2534
Reputation: 2007
You can measure the width of a text using measureText from the Paint class. If the text is too long for the view, just search for the substring that fit that width.
For example, if view
is the EditText
:
String text = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.";
String subtext = text;
Paint paint = view.getPaint();
while( paint.measureText(subtext) > view.getWidth() ) {
subtext = subtext.substring(0, subtext.lastIndexOf(' '));
}
view.setText(subtext + "...");
Upvotes: 0
Reputation: 13247
If your message is a description obtained from a server and it is rather useless to edit it by the user, why not using TextView
instead?
<TextView
...
android:ellipsize="end"
android:singleLine="true" />
Same you can achieve with EditText
, but you have to set also android:editable="false"
.
Upvotes: 6