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;
}
Related
I have to build a calculator and i am making some validations, the code is kinda long so i will only put the part that is breaking it.
It is a validation that makes the multiplication, it breaks when trying to multiply a negative number, every other operation is done correctly but the multiplication:
else if(resulttxt.getText().toString().contains("*")){
String resultarray=resulttxt.getText().toString();
String[] split=resultarray.split("\\*");
finalresult = Double.valueOf(split[0]) * Double.valueOf(split[1]);
if (finalresult<0){
negative=true;
}else{
negative=false;
}
resulttxt.setText(String.valueOf(finalresult));
resulttxt is received from a TextView that gets it's data from cliking on the numbers or the signs which are also TextViews (not buttons but they do have the On click listener)the operation is done when ciclking on the = sign.
This throws an error if for example i try to do -6*45:
java.lang.NumberFormatException: Invalid double: "6*45"
Like i said everything works with the addition,substraction and division it is only the multiplication that breaks.
I tried executing your code in the compiler :
String resulttxt = "-6*45";
boolean negative = false;
if(resulttxt.contains("*")){
String resultarray=resulttxt;
String[] split=resultarray.split("\\*");
double finalresult = Double.valueOf(split[0]) * Double.valueOf(split[1]);
if (finalresult<0){
negative=true;
}else{
negative=false;
}
System.out.println(finalresult);
}
Every thing worked fine for, please verify datatype used in your program.
addition, multiplication and division works fine. "-6+45, -6*45 and -6/45"(any other combination. I just used the same)
However for subtraction as the pattern is "-6-45" the above logic will fail and throw number format exception. This is because if you split "\-", the "-" is first character in string and there is nothing before it.
Thus your split will fail. So to avoid this you can always take last index of character to split using substring function.
OMG dudes... this is what was the problem:
I had this validation at the end of the other operations, so BEFORE even going to the multiplication part it entered the "-" validation when it checks
if(resulttxt.contains("-")){
because it is a negative value so it does have a "-"... so it entered as a substraction instead as a multiplication because of that...
To solve it i had to put the substraction validation at the bottom of all of them.
So thank you for the guys who suggested me to check the line where the error was thrown i wouldn't have known logcat actually tells you were the mistake is and to my surprise it was on the substraction validation which to me was a "WTF" moment and then i realized what i just told you.
Try this instead :
String s1 = resultarray.substring(0,resultarray.indexOf("*"));
String s2 = resultarray.substring(resultarray.indexOf("*")+1,resultarray.length());
double d1= Double.valueOf(s1);
double d2= Double.valueOf(s2);
Hope this helps
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 know highlighting in text-view is possible
Highlight Textview Using EditText
and scrolling text-view is also possible
(got the scroll code from here and is successfully scrolling too)
textView scroll at first line
now the question is, i am searching and i want to highlight that text and navigate to it, when someone presses the search button, the highlighting part is perfect, now i can get the index of the word in the string, but not line number of the string in the text-view,
point is if i want to find a position of certain text in text-view, i.e. which line number is that on, how to do it ?
i found an answer for this, but later i realized its for iOS
Search occurrences of a string in a TextView
Correct if I am wrong, you want to know the position where a particular text is in a String? If so then you can do it by using the following
String text = "0123hello9012hello8901hello7890";
String word = "hello";
Log.d("startOfWordPosition",text.indexOf(word)); // prints "4"
Log.d("endOfWordPosition",text.lastIndexOf(word)); // prints "22"
As you see that it will tell you the position as to where the word is located but you have to think about case where a word may come more than once. If you are sure that word will occur only once then above code is perfect for you. If not, then you will have to somehow tackle the problem.
This is working for loading a file into a textview so that the user's last selection or cursor position is at the top of the screen.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
page.setSelection(pos, pos2);
Layout layout = page.getLayout();
scroll.scrollTo(0, layout.getLineTop(layout.getLineForOffset(pos2))); }
}, 500);
page is a TextView, pos and pos2 are ints and the two ends of the last selection by the user (they are the same int if it's just the cursor), scroll is a scrollview containing the textview. It is all in a Handler because of delay issues internal to Android's loading all these objects. Th filename, pos and pos2 are saved as settings on exit.
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'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.