Android Currency Input? - android

I have an editText that displays $0.00. When a user clicks on that, I want a numpad to come up. If they press 5, it should display $0.05, they press 3, it goes $0.53, they press 7 it goes $5.37, etc.
So far I have the editText displaying $0.00 and it brings up a numpad but you need to delete the numbers up to the $ sign and input the decimal yourself. I've had a few really complex ideas but I'm not sure I'm going about it the right way. Any suggestions?

You could use a variable protected int curValue = 0;
And set the editText each time a new number is pressed,
curValue *= 10; curValue += pressedNumber; editText.setText("$"+curValue/100.0f);
(as long as you don't need real big numbers)

Have you by chance taken a look at the android ref for currency?
http://developer.android.com/reference/java/util/Currency.html

Related

EditText soft numeric keyboard sometimes does not allow digits

The following code produces an EditText (target version 23). I've been working on this for about 8 hours, and have received some suggestions, but I don't think anyone has ever seen this before, so I remain stuck.
Click on the field.
The A/N soft keyboard opens up.
Click the 123? button at bottom left. The numeric soft keyboard opens up.
Press any digit. Nothing happens.
Long press 5, "5/8" gets added into the text field.
Press any special character, such as #. It might add to the field.
Clear the field. Type "for", press 123?, now it will take digits.
Clear the field. Type "for?", press 123?, it will not take digits.
I added a TextWatcher. If the digits didn't post, the TextWatcher didn't see them either.
EditText bottomT = new EditText(model);
bottomT.setTextSize(14);
bottomT.setHint("ZIP");
bottomT.setHintTextColor(Color.BLACK);
bottomT.setBackgroundColor(Color.WHITE);
bottomT.setTextColor(Color.BLACK);
// bottomT.setInputType(InputType.TYPE_CLASS_NUMBER) Didn't make any difference.
// bottomT.setRawInputType(InputType.TYPE_CLASS_NUMBER) Didn't make any difference.
// bottomT.setText("", TextView.BufferType.EDITABLE); DIdn't make a difference
bottomT.setText("");
EditText is misbehaving because in my custom ViewGroup I had
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
....
child.layout(child.getLeft(), child.getTop(),
child.getLeft() + child.getMeasuredWidth(),
child.getTop() + child.getMeasuredHeight());
child.setRight(somevalue); // CAUSES EDITTEXT PROBLEMS
child.setBottom(somevalue); // CAUSES EDITTEXT PROBLEMS
It’s clear now that I can't setRight() and setBottom(), but it’s also clear that EditText should not get weird.
Ignore the backspace key.
Randomly ignore numeric keys, but accept the decimal point.
Ignore the newLine(Enter) key
Which keys are ignored, or not, depends on the device. Samsung Tab 4 or the Nexus 5 API 23 X86 emulator are good places to see this.
You have to add this line in your java code.
bottomT.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
Try this line of code .
bottomT.setInputType(InputType.TYPE_CLASS_NUMBER |InputType.TYPE_NUMBER_FLAG_DECIMAL);

Trouble creating a "speed dial" button on Android

I'm making a very simple launcher for Android. I'm having trouble getting some speed dial buttons to work.
The idea is that when the user presses the speed dial buttons for the first time, they'll be prompted to enter a phone number with a popup window (dialog? I think?). That number will then be assigned to that button, which will then call the assigned number when the button is next pressed. It's not great if they want to reassign a number, but this is just something rough that i'd like to get out of the way. I'm thinking of creating an int to reflect the state of the button (0 = no number assigned, 1 = number is assigned), and using if statements to either bring up the window or call the number.
I don't know how to bring up such a window (although I do know I can bring up a dial pad using (android:inputType="phone")), as well as how to pass the number that the user inputted to an int/long. I'm thinking I can assign the value to an int, although that might not be the most optimal data type. I have a rough idea on how to dial a number once it's given.
What should I do? I'm quite new to programming, so i'm having trouble with this.
may be this could help.
have a long click listener to the buttons.
on long click event, show a dialog widow which accepts the number.
store it in a persistent memory, either db, files or pref utils by android.
in on click button, have a logic to retrieve the stored content and update the views accordingly.

Calculator app approach

as getting into android i decided to replace the default calculator with mine. A simple calculator with the 4 operational signs. I've been giving to all buttons the right behaviour, storing every number in a 'num' ArrayList(String) and signs in a 'sign' ArrayList(String).
What i wanted to do, was to then combine numbers and signs into a string, parse it into a float and getting a result. I thought this was one of the easy/simple ways to deal with it, since when you set a float like this:
float f = 6*4-5/2+3
it gives you the right result. but it clearly does not when starting from a String, like this:
String s = "6*4-5/2+3"
Float f = Float.valueOf(s)
Is there a way to getting a result from my 2 ArrayList(String)? In the negative case, what would be a doable approach (in the sense im not an experienced programmer)I?
I Think this approach is incorrect.
I would do the following:
You would have a Textview or Edittext as the calculator "screen" on top.
then you would have all your number and operation signs buttons.
Now, every number you press, it will append to the last one on the screen, using .append()
once you tap on an operator sign - two things will happen:
1) the number in the textView will be stored as a Float (using Float.valueOf(yourTextView); in a varibale, say firstNum.
2) you will save the operator you clicked in a second variable, say String calcOper.
Now, you enter your second number, and then you would press the Equals sign.
What will happen then is simply use a Switch of If expression.
If calcOper is "-" - then do firstNum- Current number shown on screen.
If calcOper is "+" - then do firstNum+ Current number shown on screen.
At last don't forget to set the text on the TextView the result.
Good luck!

Android: Calculator on showing 0 immediately after the dot

I am now working on a calculator, and everything works fine except for decimal places.
The calculator contains 2 displays actually, one is called fakedisplay for actual operations, and one is called Display, for presenting the desired format, ie adding commas.
When pressing 12345.678, Display will follow fakedisplay and present as 12,345.678, but if i press 12345.009, the fakedisplay will work normally as 12345.009, but the Display stuck as 12,345 until 9 is pressed, and at that time it will show 12,345.009 normally.
However, it is strange that when the user presses 0, there is no response, and until pressing 9, 009 will then immediately append.
I know this arise from the parsing code, but based on this, how could I amend the following code? I really cannot think of any solution... Many thanks for all your advice!
one.setOnClickListener(new View.OnClickListener() {
if (str.length()<15) {
Fakedisplay.append("1");
}
DecimalFormat myFormatter1 = new DecimalFormat("###,###,###,###.#################");
String str1=Fakedisplay.getText().toString();
String stripped1 = Double.valueOf(str1).toString();
stripped1 = myFormatter1.format(Double.valueOf(stripped1));
if (stripped1.endsWith(".0"))
stripped1 = stripped1.substring(0, stripped1.length() - 2);
Display.setText(stripped1);
}
Probably the easiest solution is to not strip off the .0 in the code for every keystroke..
Instead, only strip off trailing zeros (assuming there's a decimal point in there of course) when the user calls for a result. Entering keys such as the digit keys 0 through 9, the decimal point ., or the sign-change key +/- (what I'll call the entry keys) are not generating a result so should not strip trailing zeros.
However, non-entry keys, such as when you press + or - or = on your calculator can freely modify the number.
That will give you a display of the digits being entered as the user enters them but will still strip off trailing zeros when necessary.
You can do that with a modification to your statement (and, as mentioned, only doing this when the user presses a non-entry key):
stripped1 = stripped1.replaceAll("(\\.[0-9]*[1-9])0+$","$1");
stripped1 = stripped1.replaceAll("\\.0$","");
The first statement removes all trailing zeros at the end of a decimal number (other than on if it's really an integer). The second takes care of that case.
No doubt I could make a single substitution if I gave it some more thought but that should be enough to get it functional.

IndexOutOfBoundsException in Edit Text for android

i have an edit text in my activity.i am entering numbers in it manually but
int mystart = destinationNumber.getSelectionStart();
int myend = destinationNumber.getSelectionEnd();
numberText.getText().replace(Math.min(mystart, myend), Math.max(mystart, myend),
"1", 0, 1);
its entering fine according to the cursor position.
i have a delete button in my acitivity which deletes single character according to cursor postion.
numberText.getText().delete(myend - 1, mystart);
But this logic is not working properly when i select whole text and call delete method it gives me IndexOutOfBoundsException OR i select 4-5 digits and call this delete.
I want the same functionality as android contact dialpad number enter field.Can someone help me figure out what is the correct logic to delete single digit from edittext and multiple selected digits as well.
Thanks
delete receives the start as first parameter and end as second, not the other way around.
Probably the error its taht mystart or myend(probably this) are bigger or smoller than numberText.lenght().
Try to put a Log.d("","" ) with the lenght of the text, mystart and myend and check if you need a myend -1 or something like that.

Categories

Resources