I have a multiline edit text in which the number of lines can grow up to any number.
Does android provide any functionality to get content of any particular line?
Suppose total number of lines are three and i want to read only second line.
A EditView in Android does wrap its text so it fits in the view. That means that you have to determine a lines start and end position to read it and then you can extract it from the full contentstring. This post already explained this issue.
So your code would be:
// change to your needs
int linenumber = 1;
int startPos = myTextView.getLayout().getLineStart(linenumber);
int endPos = myTextView.getLayout().getLineEnd(linenumber);
String theLine = myTextView.getText().toString().substring(startPos, endPos);
Related
If I have a string like this
String myString = "This is some text.";
Will I get the same lengths if I measure the entire string as when I take the sum of measuring each character?
At first thought, I assumed the answer to be yes, but then I was reading this answer, which said measureText adds an AdvanceX value before and after each measurement. If that is true, then it would add a subtle error into measuring summed lengths (because there would be double padding between each character).
I also know that there is kerning with some fonts that changes how close letters are placed together depending on the surrounding text. This is making me think more and more that a sum of the parts will be different than the whole.
The image comes from Wikipedia.
This is important because I am making my own custom text view that needs to handle line wrapping. If a single word is too long for the line then I measure each character until I discover where I need to break. However, I assumed that summing the measure of the individual character widths would be the same as measuring the whole string.
I have set up this question so that I can answer it below after I am finished with my tests.
The whole is not always equal to the sum of the parts.
This appears to be a result of kerning and not related to any AdvanceX double padding that you mentioned.
Test code
Here is the test code:
String myString = "This is some text.";
Paint myPaint = new Paint();
myPaint.setAntiAlias(true);
myPaint.setTextSize(100);
float theWhole = myPaint.measureText(myString);
float sumOfTheParts = 0;
for (int i = 0; i < myString.length(); i++) {
sumOfTheParts += myPaint.measureText(myString, i, i + 1);
}
Results
In my first test, the measures are the same:
String myString = "This is some text.";
theWhole // 787.0
sumOfTheParts // 787.0
However, when there are letters that need kerning, the measures are different. This is the test again with a different string:
String myString = "AV Wa";
theWhole // 291.0
sumOfTheParts // 297.0
Implications
You cannot assume that measuring all the characters of a string and then summing the total will give you the actual length of the string. Thus, when doing text wrapping you will need to measure entire string ranges.
I'm working on a calculator app in android studio, and I would the calculator to append the answer to the existing equation, more like a graphing calculator, where it shows the equation then the answer beneath it. My current solution works for the first equation only. I've tried
int start = display.getLayout().getLineStart(display.getLineCount());
int end = display.getLayout().getLineEnd(display.getLineCount());
then
double result = calc(display.getText().toString().substring(start,end));
The result is that I get an IndexOutOfBoundsException from getLineEnd, and I don't know how to go about it?
Ticked answer is wrong. I have multiline TextView but the text has no '\n' or '\r' characters. It is multiline because width isn't enough for whole text. Your problem is you sending getLineCount() as parameter you must send getLineCount() -1 for last line. I am calculating last line width as below:
float lastLineWidth = textViewMessage.getLayout().getLineRight(textViewMessage.getLineCount() - 1) - textViewMessage.getLayout().getLineLeft(textViewMessage.getLineCount() - 1);
But I have one problem with using it. I cant call textView.getLayout() method in ListView.getView() mehtod. It always returns null. I can only call it in TextView.Post() method but I don't want to call it in Post() method because I do some adding and removing rules to LayoutParams and it causes problem scroll problems in ListView.
Is there a way to get last line string or last line width without calling getLayout() method of TextView.
Sounds like you have a multiline TextView so you could try splitting by new line chars and getting the last element from resulting array like this:
public static String getLastLine(TextView display){
String lines[] = display.getText().toString().split("\\r?\\n");
String lastOne = lines[lines.length-1];
return lastOne;
}
At the outset, I would like to apologize for my English :).
I have a String with long text. I display it in a TextView. If the text is automatically wrapped to new line, I want to add "\t" at the beginning of the new line. I don't know how do it. Any ideas?
E.g.
String text = "1. abcdefghij\n\tklmnopqrstuvwxyz";
Display:
1. abcdefghij
klmnopqrstuvwxyz
But if:
String text = "1. abcdefghij\n\tklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Display:
1. abcdefghij
klmnopqrstuvwxyzABCDEF
GHIJKLMNOPQRSTUVWXYZ
I want:
1. abcdefghij
klmnopqrstuvwxyzABCDEF
GHIJKLMNOPQRSTUVWXYZ
You can count the lines of your TextView and if there are more than 1 insert a "\t".
In this post it's shown how to count the lines although is not an obvious question.
You also need to take into account if the tab is already inserted, because could be inserted more than one \b.
I'm creating a code editor in android. I want to print line number on the screen when each time user takes a new line. I'm using EditText for editor. Can any one help me out.Thank you in advance.
You can use getLineCount() from here and auto add(as example in loop) line number for every new line.
Pseudo code:
for (a = 1; a < getLineCount();a++)
{
line[a] = a.toString()+": " + line[a];
}
Edit:
For auto adding line number you should create your own implemenation of EditText class.
I need to configure a button with a code that takes the first two digits of a number (when clicked) (from a textbox) and adds a number to it; and then takes the 3rd and 4th digits and does somewhat the same to it and displays the result value in a toast message. The user inputs the original number to which the additions and subtractions must be done.
So far I have only done: Toast.makeText(TimerCodeActivity.this, String.valueOf(
After that I am just lost
Try something like the following:
EditText theTextBox = (EditText)findViewById(R.id.idOfTheEditText);
int digits = Integer.parseInt(theTextBox.getText().toString().subString(0,2)); // Take the first two digits
int newnumber = digits + 4; // Add a number? Or are you trying to add like a string?
Toast.makeText(TimerCodeActivity.this, String.valueOf(newnumber), Toast.LENGTH_LONG).show();