How to set ellipses to the end of TextView? - android

In my application I want use TextView and I want when this TextView if get l line show 3dos (...)
I write this lines in TextView :
android:ellipsize="end"
android:maxLines="1"
But always set ... end of textView.
But I want if line after 1 lines then show ...
how can I it?

Try this code :
if (holder.newsTitle.getLineCount() > 1) {
holder.newsTitle.setEllipsize(TextUtils.TruncateAt.END);
}
I hope help you dear

If you want to get the number of lines of a TextView, you can do this...
textView.setText("Here is my text");
int numOfLines = textView.getLineCount();
if (numOfLines > 1) {
//code here
}
The BEST way to do this is by character count.
theString = "This is my string";
if (theString.length() > 5) {
textView.setText(theString.substring(0, 5) + "...");
}
The above code will say, if the character count is greater than 5, cut off the characters after 5 and add ...
The above code prints out:
This ...

Related

How can I print while loop results in textView in Kotlin

I have a "for-loop" in Kotlin which is going to run my code 6 times.
I also have a textView on the app and want to see these 6 results shown there.
I can easily println() the results.
However, If I set the text of textView to these results, it only gets the last result.
What I like to do printing out all 5 results in textView (suggestedNums ) as each result is a separate line.
Is it even possible?
Any help appreciated.
Thanks.
for (i in 1..6) {
val s: MutableSet<Int> = mutableSetOf()
//create 5 numbers from numbers
while (s.size < 5) {
val rnd = (numbers).random()
s.add(rnd)
}
// remove all 5 random numbers from numbers list.
numbers.removeAll(s)
// sort 5 random numbers and println
println(s.sorted())
// set suggestedNums text to "s"
suggestedNums.text = s.sorted().toString()
}
You can do it in 2 ways
replace
suggestedNums.text = s.sorted().toString()
with
suggestedNums.text = suggestedNums.text.toString() + "\n" + s.sorted().toString()
Create a string and append the results with "\n" and set the text outside the for loop

singleLine true for Textview,But multiple lines comes in one line..,Need to show 1st line

Given singleLine="true" for Textview
msg = messageInbox.getMESSAGE();
msg = msg.replaceAll("\n", "<br/>");
descView.setText(Html.fromHtml(msg));
Text does not displaying with Only 1st line .
example i have test \n\n\n and more \n
i need to display 1st line test
try this textview attributes
android:inputType="textPersonName"
OR
android:maxLines="1"
if (msg.contains("\n"))
msg = msg.substring(0, msg.indexOf("\n"));
else
msg = msg.replaceAll("\n", "<br/>");

How to get length of first line in Edittext android?

I have a multi lines Edittext with a text (don't content "\n"), a font size (sp)
and the length of text > Edittext.width().
I want to get length of the first line in EditText.
How can I do it?
You can see the photo
One option could be to read the text and then get the index of the newline character, which is essentially the length of the string prior to it:
int firstLineLength = myEditText.getText().toString().indexOf("\n");
As an alternative, if you ever need to do this with other lines you can simply split the whole string based on the newline character:
String[] lines = myEditText.getText().toString().split("\n");
EDIT
Keep in mind that indexOf() will return -1 if an occurrence is not found. So if your EditText has one and only one string, you'll get a -1 line length so be prepared to check against that:
int lineEndIndex = myEditText.getText().toString().indexOf("\n");
int firstLineLength;
if(lineEndIndex == -1) {
firstLineLength = myEditText.getText().toString().length();
} else {
firstLineLength = lineEndIndex;
}

How do I get the text at a specific line in a TextView

I have a TextView that is X lines long. How do I get the text that is at, say, line 3?
Example: I have this TextView
This is line 1
And this is line 2
And this is line 3
I want to be able to get the String at any one of these lines by itself, such as getting the String at line 3 would return "And this is line 3". Searching for line breaks is not the solution I need, as it doesn't take into account text wrapping.
You can use textView.getLayout().getLineStart(int line) and getLineEnd to find the character offsets in the text.
Then you can just use textView.getText().substring(start, end) -- or subsequence if you are using Spannables for formatting/etc.
Here is some code to supplement the accepted answer:
List<CharSequence> lines = new ArrayList<>();
int count = textView.getLineCount();
for (int line = 0; line < count; line++) {
int start = textView.getLayout().getLineStart(line);
int end = textView.getLayout().getLineEnd(line);
CharSequence substring = textView.getText().subSequence(start, end);
lines.add(substring);
}

Android Development: Count EditText Lines on textChanged?

How can I count the number of lines in an EditText?
Basically in my app I have line numbers and I wanted to make them update on textchange (I already have the textchangelistener set up).
Is this possible? :(
Thanks,
Alex!
Lines can be differents:
Visible lines: Wrapped text count as a new line...
List item: Only lines with \r, \n, \r\n
First case (the easiest):
int nbLines = editText.getLineCount();
Second case:
int nbLines = 0;
StringReader sr = new StringReader(editText.getText().toString());
LineNumberReader lnr = new LineNumberReader(sr);
try {
while (lnr.readLine() != null){}
nbLines = lnr.getLineNumber();
lnr.close();
} catch (IOException e) {
nbLines = editText.getLineCount();
} finally {
sr.close();
}
Depends on what you define as "line number".
A line in your edittext in "GUI way", which includes the linebreaks your editview does? Or a line in a "coding way" of describing it (having \n at the end)?
First one will be quite hard to get, if even impossible. Second one: just count the numbers of \n in the text, plus add another 1 if there is something after the last \n.

Categories

Resources