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.
Related
I need to put currency at the start of text and this text should be divide thousands by dots. Now I am using MaskedTextChangedListener from this library, so my code is following:
val listener = MaskedTextChangedListener(
"[000].[000].[000]",
true,monthlyInstallment.editText!!, null,null)
listener.rightToLeft = true
monthlyInstallment.editText?.addTextChangedListener(listener)
Here you can see that I added mask and used rightToLeft value in order to divide amount by dots. The problem is that if I will put currency to the start of mask like $[000].[000].[000], the currency appears only after I will fill all the mask, whereas I need to appear it when I will start typing.
Also I tried to put logic for adding currency in other TextChangeListener. My code is following
if(!monthlyInstallment.editText?.text.toString().startsWith("$ ")){
monthlyInstallment.editText?.setText("$ " + monthlyInstallment.editText?.text.toString())
Logger.msg("check " + monthlyInstallment.editText?.text.toString().length)
}
monthlyInstallment.editText?.setSelection(monthlyInstallment.editText?.text.toString().length)
Here problem is that my cursor is set somewhere, but not at the end of text. By the way, this code is placed into afterTextChangedListener method.
So can someone help me with one of the problem?
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 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);
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();