How to limit lines on a TextView - android

I'm trying to limit a TextView to 1 line, the length of the screen. I'm having trouble keeping it from expanding.
I've tried adding the
android:minLines="1"
android:maxLines="1"
attributes to the TextView xml. This does keep the view from expanding, but this does not limit the characters of the text. For example, if I have entered the maximum number of characters in a line and proceed to add a '(' to the string, I can see the top of it printed below the first line.
Basically, I need a way to limit the amount of characters to the width of the screen. I've tried this:
while(txt.getLineCount() > 1) {
txt.setText(txt.getText().subSequence(0,txt.getText().length()-1));
}
which doesn't work at all. I've also tried that same method with comparing the txt.getHeight() to the txt.getTextSize().
I'm out of ideas. What can I do?

If I use a textview this way it will ocupy only 1 line:
<TextView
android:id="#+id/textView"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Try this. This will limit textview to single line. , if the text was more than one line it will display ... at end.
<TextView
android:id="#+id/textView"
android:singleline="true"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Related

Text view losing characters that is to be displayed [Android]

I have a text to be displayed in a text view. The text contains 200 lines of data, each line is having 10 characters. So total 2000 characters. The text view is inside a vertical scrollable view. The first 50 lines are not getting displayed. If the text view had a limitation to the number of characters, then its fair enough if it loses the last few lines, but why is it taking off the head??.
And also what is the limit on the number of characters for a text view??
My code:
<ScrollView
android:id="#+id/scroll_layout_ECG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_ECG"
android:layout_alignLeft="#+id/btn_ECG"
android:layout_alignStart="#+id/btn_ECG"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp" >
<TextView
android:id="#+id/tvECG_samples"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="ECG\nsamples"
android:textSize="25sp" />
</ScrollView>
In the 1st screen shot, the data is being shown from 51st line. The first 50 lines are clipped off. Why??
no need of external scrollbar rather than u can use textview's property
android:scrollbars="vertical"
I think It will help u

Android - TextView ellipsize doesn't work properly

Android ellipsize doesn't work as expected in some cases.
What I'm expecting is three dots on the last line, but in cases it adds the 3 lines plus a few characters related to the rest of the text afterward. Is it possible to change this behavior?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:singleLine="false"
android:ellipsize="end"
android:maxLines="2"
android:layout_marginBottom="16dp"/>
According to me android:ellipsize="end" depends on the words in the TextView.
If following word is lengthy then it omits the word and shows ...
If the following word is small then it will fit correctly and last fill ...
Hope it helps.

text if going long then the text field coming to next line how to stop it

I am very new to android development.I am trying to make a text field in which if I type long text the next line should not come.
currently If the text is too long line break is coming and its going to the next line making the height of text field grow.
<EditText android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message"
android:inputType="textMultiLine"
android:gravity="top"/>
suggest please.
Delete the android:inputType="textMultiLine" parameter. It makes the EditText to break into a new line if the text is becoming big enough
dear use android:singleLine="true"
You have lot of option to restrict this.
android:singleLine="true" Using this textView always display text in only single line.
android:lines="1" Using this you can hard code the number of line space.
android:maxLines="1" Using this your textview size will grow up to specified number of lines.
Hope this will help you to better understanding....:)
Remove this line
android:inputType="textMultiLine"
And add this line
android:singleLine="true"

Android TextView with multiple lines

I want a TextView that should be broken into 4 lines. For e.g.
Vishal Vyas
Having
342
Reputation
Note, the gravity should be center_horizontal
I tried following :
<TextView
android:gravity="center_horizontal"
android:id="#+id/lblUserRep"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:lines="4"
android:maxLines="4"
android:text="VishalVyas Having 342 Reputation" >
</TextView>
This works! but produces following output:
VishalVyas
Having
342
Reputation
Problems:
It doesn't work with the space between words Vishal and Vyas.
android:layout_width="70dp" is harcoded and there can be any name with n number of characters instead of VishalVyas.
Please advice.
Added:
It would be fine if I need to write a custom TextView for achieving this but I'll require some guidance.
Thanks in advance.
I think it's wrapping because "Vishal Vyas" is going beyond 70dp. Instead, do wrap_content on the width and use newline characters for lines instead of wrapping (i.e. "Vishal Vyas\n342\nReputation")
You should be able to insert the newline character \n to control where the splits go. Once you do that, you can expand your TextView wider so that it can accommodate a longer user name but still break in the right place.
android:lines="2"
android:minLines="2"
android:singleLine="false"
Even if Android Studio warns that android:singleLine=false is deprecated, keep it and one can have the number of lines they want for their text box depending on the length of their text
I did so:
The Container of TextView:
android:layout_width="match_parent"
android:layout_height="match_parent"
The TextView:
android:layout_width="match_parent"
android:layout_height="wrap_content"
Then,
The Text of the TextView was showed in two lines or more...

Can I limit TextView's number of characters?

What I have now is a ListView of TextView elements. each TextView element displays a text (the text length varies from 12 words to 100+). What I want is to make these TextViews display a portion of the text (let's say 20 word or roughly 170 chars).
How to limit the TextView to a fixed number of characters?
Here is an example. I limit the sizewith the maxLength attribute, limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.
<TextView
android:id="#+id/secondLineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxLength="10"
android:ellipsize="end"/>
If your not interested in xml solutions maybe you can do this:
String s="Hello world";
Textview someTextView;
someTextView.setText(getSafeSubstring(s, 5));
//the text of someTextView will be Hello
...
public String getSafeSubstring(String s, int maxLength){
if(!TextUtils.isEmpty(s)){
if(s.length() >= maxLength){
return s.substring(0, maxLength);
}
}
return s;
}
Use below code in TextView
android:maxLength="65"
Enjoy...
I did this using the maxEms attribute.
<TextView
android:ellipsize="end"
android:maxEms="10"/>
I am sharing an example where I have set maxLength=1 i.e. limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.
Please Note: layout_width which is 120dp i.e. after 120dp any text
exceeding will triggrer "ellipsize=end" property
paste the below code directly to check.
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:maxLength="40"
android:text="Can I limit TextView's number of characters?"
android:textColor="#color/black"
android:textSize="12sp"
android:textStyle="bold" />
.
You can use setEllipsize method of TextView class
http://developer.android.com/reference/android/widget/TextView.html#setEllipsize(android.text.TextUtils.TruncateAt)
With the constants of TextUtil class for added the suspension points
http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html
Programmatic Kotlin.
Cut off the start of the text:
val maxChars = 10000
if (helloWorldTextView.text.length > maxChars) {
helloWorldTextView.text = helloWorldTextView.text.takeLast(maxChars)
}
Cut off the end of the text:
val maxChars = 10000
if (helloWorldTextView.text.length > maxChars) {
helloWorldTextView.text = helloWorldTextView.text.take(maxChars)
}
Here is the correct Solution. Keep remember, to set width to WRAP CONTENT. and ems is not based on Max Charater. EMS based on width. If ems value width will exceed the text length, then automatically (...) will be added at last of textview.
android:ems="8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This is a demo text"
As mentioned in https://stackoverflow.com/a/6165470/1818089 & https://stackoverflow.com/a/6239007/1818089, using
android:minEms="2"
should be enough to achieve the goal stated above.
you can extend the TextView class and overwrite the setText() function.
In this function you check for text length or word cound.

Categories

Resources