Unable to make backspace button function - android

I am adding a backspace button in my calculator app and it is working fine also. The problem is, by default, my calculator is taking digits from decimal part i.e. initially it is 0.00, when I input 1 it becomes 0.01,when I input 2 it becomes 0.12 and so on and so forth. Now, when I press backspace it is deleting 2 and showing 0.01 but if I press 3 instead of showing 0.13 it shows 1.23. How to resolve this?
Code for the backspace button:-
private String addCurrency(String digits) {
String string = ""; // Your currency
enteredNumber = enteredNumber + digits;
// Amount length greater than 2 means we need to add a decimal point
if (enteredNumber.length() > 2) {
String rupee = enteredNumber.substring(0,
enteredNumber.length() - 2); // Pound part
String paise = enteredNumber.substring(enteredNumber.length() - 2); // Pence
// part
if (enteredNumber.contains(".")) {
}
string += rupee + "." + paise;
} else if (enteredNumber.length() == 1) {
string += "0.0" + enteredNumber;
Log.d("TextWatcher", "length 1 " + string);
} else if (enteredNumber.length() == 2) {
string += "0." + enteredNumber;
Log.d("TextWatcher", "length 2 " + string);
}
return string;
}
WHERE:-
enteredNumber is just a String type variable.

If you wish to show your numbers with two decimal points all the time, then why so much trouble? Use this -
public void getMyNumber(String yourInputNumber){
Double result = Integer.ParseInt(yourInputNumber)/100;
private static DecimalFormat REAL_FORMATTER = new DecimalFormat("0.##");
textview1.setText(REAL_FORMATTER.format(result));
}

Related

how to start count upto 1 until press comma(,) it should count tow

its may be silly but am confused on that this i want to start count up to one and if press comma(,)then i want to count comma only, here how i am try.
String conCount;
conCount = "1";
int countComma = conCount.length() - conCount.replace(",", "").length();
String lenVar;
lenVar = conCount;
convert = String.valueOf(countComma);
if (conCount.length() == 0) {
lenVar = "0";
} else {
textViewConCount.setText(convert);
}
String editTextString = "abc,efg,pqr,xyz";
if (editTextString.contains(",")) {
int countStringsSeperatedByComma = 0;
countStringsSeperatedByComma = editTextString.split(",").length;
System.out.println("Count of strings seperated by comma : " + countStringsSeperatedByComma);
int commaCount = countStringsSeperatedByComma - 1;
System.out.println("Count of commas : " + commaCount);
} else {
System.out.println("Count of characters in editText string : " + editTextString.length());
}
Output for above condition will be :
Count of strings seperated by comma : 4
Count of commas : 3
Suppose if your string is "abcefgpqrxyz" i.e. without comma then it will execute else part and print characters count as 12 in this case
Count of characters in editText string : 12
Your question statement is so ambiguous. Elaborate it completely and explain your end result with example. It's regarding string functions, I can give you the answer about it if I understand it :) :D
thanks for that i got that answer like that.
String varStr = editextContact.getText().toString();
//int VarCount = editextContact.getText().length();
int countStringsSeperatedByComma = varStr.split(",").length;
String convet=String.valueOf(countStringsSeperatedByComma);
textViewConCount.setText(varStr);
if (varStr.length() == 0){
textViewConCount.setText("0");
}else {
textViewConCount.setText(convet);
}

Android basic math in textview

I am trying to add two numbers and display them in the textview using this code. The problem here is that it doesn't add the numbers, it just displays the entire string.
CharSequence fnum, snum, symbol;
final TextView CalTextBox = (TextView) findViewById(R.id.MainTextview);
symbol = "+"; // addition selected
fnum = CalTextBox.getText(); // store number into fnum
snum = CalTextBox.getText(); //new number will be added in the code and be stored into snum
CalTextBox.setText(""); // delete whats in the text box
CalTextBox.setText(snum + "" + symbol + "" + fnum); // add two numbers
Well, the '+' operator performs concatenation if used on strings (like in this case). To perform a math operation, you have to convert them to numbers first. I think you can use this:
// Convert the 2 String to integer values
int first = Integer.valueOf(fnum);
int second = Integer.valueOf(snum);
// Compute the sum
int sum = first + second;
// Create the String you can use to display in the TextView
String textToDisplay = String.valueOf(sum);
snum = "2";
fnum = "3";
symbol = "+";
snum + "" + symbol + "" + fnum = "2+3"
Instead you should convert String into integer or double and make appropriate controls such as null or empty, or non-numeric then,
int result = Integer.parseInt(snum) + Integer.parseInt(fnum);
CalTextBox.setText("" + result);
For mathematical operations is best to use int, long or double variable types. Instead of CharSequence use for example int.
to get integer (int) from String (text) use:
int fnum, snum, symbol;
int fnum = Integer.parseInt("10"); or
fnum = Integer.parseInt(CalTextBox.getText());
CalTextBox.setText("" + (snum + symbol + fnum));

Delete characters in string until /r/n

I have long string that at some part has
some text + "PHOTO;ENCODING=BASE64;TYPE=JPEG:" + some characters that generate randomly + /r/n...
I am wondering how can I delete part from
"PHOTO;ENCODING=BASE64;TYPE=JPEG:" untill /r/n
so I will be left only with
some text + /r/n ?
my code so far:
if (string.contains("PHOTO;ENCODING=BASE64;TYPE=JPEG:") {
string = string.replace("PHOTO;ENCODING=BASE64;TYPE=JPEG:", "");
}
but this obviously would not replace my random generated chars, only "PHOTO;ENCODING=BASE64;TYPE=JPEG:".
How do I "loop through" string from "PHOTO;ENCODING=BASE64;TYPE=JPEG:" untill /r/n ?
final String input = "some text + PHOTO;ENCODING=BASE64;TYPE=JPEG: + some characters that generate randomly + /r/n"
final int index = input.indexOf("PHOTO;ENCODING=BASE64;TYPE=JPEG:");
if (index != -1)
{
final String result = input.subString(0, index) + System.getProperty("line.separator")
}
why dont you try following
1) Get the index of "PHOTO;ENCODING=BASE64;TYPE=JPEG:". and call it idx
2)If idx != -1 then take substring of original string using str.subString(0,idx) and call it newStr
3)return newStr+(str.endsWith("\r\n")?"\r\n":"")

Substring removes forward slash?

text = Daily 10 am - 5 pm.\\nClosed Thanksgiving and Christmas.
private String activateNewlines( String text ) {
String temp = text;
if ( text.contains( "\\n") ) {
while ( temp.contains( "\\n" ) ) {
int index = temp.indexOf( "\\n" );
temp = temp.substring( 0, index ) + temp.substring( index + 1 );
}
return temp;
}
return text;
}
I'm trying to get rid of an extra slash for a special character but for some reason substring ends up removing the forward slash. Does substring not like slashes at the beginning of strings? The final string ends up becoming
Daily 10 am - 5 pm.nClosed Thanksgiving and Christmas.
What I need is
Daily 10 am - 5 pm.\nClosed Thanksgiving and Christmas.
EDIT: What ended up working for me:
String temp = text;
if ( text.contains( "\\n") ) {
temp = temp.replaceAll( "\\\\n", "\\\n" );
int x = 5;
return temp;
}
return text;
This actually allows the TextView to recognize the newlines.
I think you should simply do this,
string.replaceAll("\\n", "\n")
Detailed code,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String text = "Daily 10 am - 5 pm.\\nClosed Thanksgiving and Christmas.";
Log.d("TEMP", "*********************************" + activateNewlines(text));
}
private String activateNewlines( String text ) {
String temp = text;
return temp.replaceAll("\\n", "\n");
}
Logcat output is,
08-28 19:16:00.944: D/TEMP(9739): *********************************Daily 10 am - 5 pm.\nClosed Thanksgiving and Christmas.
I'm a bit confused, but here goes. So, "\n" is a new line. "\\n" is a backslash and an n, \n. You can just use replaceAll to get rid of it: string.replaceAll("\n", ""). This is where I get confused, I'm not sure what you exactly want. If you want to keep the new line it then you have to properly get it from wherever you're getting it from (e.g. you should get a \n character instead of the escaped version.).

Dialog shows alpha Characters?

Am using Custom Dialog to shows the list of number based on user input,in Edittext box. I added Textwatcher everything is wrking fine until user try to give input more fast its showing one more Dialog with Black stripes with some alpha characters How to rectify this one?
My Screenshot is 1
Custom Dialog code here
this is my code which am using in textwatcher
#Override
public void afterTextChanged(Editable s) {
String str = s.toString();
str_length = str.length();
Log.v("length_before", "" + count + "" + str_length);
if (str_length == count + 1) {
return;
}
if (str_length >= 3) {
return;
}
if (str_length > count) {
count = str.length();
AmountDialog.amount_dialog(TicketIssueActivity.this, str,
amount);
} else if (str_length < count) {
count = str_length - 1;
Log.v("length slese", "" + count + "" + str_length);
}
}
If all you want is number in amount field. you can set:
android:inputType="number"
in the EditText field of your layout xml file.
That's all I got from the question. Please post more details or code if this doesn't solve the problem.

Categories

Resources