Android Development: Count EditText Lines on textChanged? - android

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.

Related

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;
}

Delete spaces from Editable object

I have deleted characters in edittext objects with this code
edit = etcalle.getEditableText();
if (edit.length() > 0)
edit.delete(edit.length() - 1, edit.length());
It has been working but when my string looks like this +81 901 it doesn't delete the space. It gets to it and stops deleting characters.
How can I remove the space in my text?
EDIT:
Just to be clear, I don't want to remove everything at once. Just one character at every time I hit my delete button
String original = etcalle.getText().toString();
then
etcalle.setText(original.substring(0,original.length-1));
of course be sure to check that the original is not null or length < 1
use this it will remove all the spaces
String str = "99 85263 9633";
str.replace(" ", ""); // Output is 99852639633
Then Its magic you can do this with this
String str = "99 85263 9633";
str.replaceFirst(" ",""); // Output is 9985263 9633

How to read word by word from file?

Could anybody post here some code how can I read word by word from file? I only know how to read line by line from file using BufferedReader. I'd like if anybody posted it with BufferedReader.
I solved it with this code:
StringBuilder word = new StringBuilder();
int i=0;
Scanner input = new Scanner(new InputStreamReader(a.getInputStream()));
while(input.hasNext()) {
i++;
if(i==prefNamePosition){
word.append(prefName);
word.append(" ");
input.next();
}
else{
word.append(input.hasNext());
word.append(" ");
}
}
There's no good way other than to read() and get a character at a time until you get a space or whatever criteria you want for determining what a "word" is.
If you're trying to replace the nth token with a special value, try this:
while (input.hasNext()) {
String currentWord = input.next();
if(++i == prefNamePosition) {
currentWord = prefName;
}
word.append(currentWord);
word.append(" ");
}
Another way is to employ a tokenizer (e.g. in Java) and using the delimiter space character (i.e. ' '). Then just iterate through the tokens to read each word from your file.
You can read lines and then use splits. There is no clear definition of word but if you want the ones separated by blank spaces you can do it.
You could also use regular expressions to do this.

Removing newline from string

Here's my issue:
I have a database and it is full of episodes of a tv show. One column denotes the episode number. I want to display the episodes in a list like this:
Episode 1
Episode 2
Episode 3
etc.
I'm using my own adapter class that extends SimpleCursorAdapter to do this...
Since I had formatting errors I am using Android.R.layout.simple_list_item_1 and Android.R.id.text1
Basically the only reason I have a custom adapter is so I can do something like this:
textView.setText("Episode " + cursor.getString("column_for_episode_number");
The problem is, I get a list that looks like this:
Episode
1
Episode
2
Episode
3
When I try something like this(which worked in a different portion of my code):
String text = "Episode " + cursor.getString("blah");
text = text.replaceAll("\\n","");
I get the exact same list output :(
Why don't I use create a custom view with two textboxes next to each other? It is hard for me to get that to look pretty :/
text.replaceAll(System.getProperty("line.separator"), "");
There is a mistake in your code. Use "\n" instead of "\\n"
String myString = "a string\n with new line"
myString = myString.replaceAll("\n","");
Log.d("myString",myString);
Check if there is new line at the beginning before you replace and do the same test again:
for(int i=0; cursor.getString("blah").length()-1; i++)
{
if(cursor.getString("blah").charAt(i)=='\\n') <-- use the constant for the line separator
{
Log.i("NEW LINE?", "YES, WE HAVE");
}
}
Or use the .contains("\n"); method:
Check the xml for the width of the textview as well.
Why are you using getString() when you are fetching an integer? Use getInt() and then use Integer.toString(theint) when you are setting the values in a textview.
This could help you:
response = response.replaceAll("\\s+","");
It sounds like you are hitting wrapping issues rather than newline issues. Change this:
String text = "Episode " + cursor.getString("blah");
To this:
String text = "Episode" + cursor.getString("blah");
And see if that changes the output. Post your layout xml please?
this worked for my (on android 4.4):
(where body is a string with a newline entered from an EditText view on handset)
for (int i=0; i<body.length(); i++) {
if (body.charAt(i) == '\n' || body.charAt(i) == '\t') {
body = body.substring(0, i) + " " + body.substring(i+1, body.length());
}
}
have you tried
cursor.getString("blah").trim()

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);
}

Categories

Resources