I have an editText that is suppose to be representative of dollar amounts. As I am trying to get the functionality of the editText working to how I want it, I am running into a lot of issues. Let me first show you what I have set up, and then I will explain my issues. Here is what I did in my xml for the editText
<EditText
android:id="@+id/edittxtbill"
android:layout_width="fill_parent"
android:layout_height="38dp"
android:textColor="@color/black"
android:textSize="@dimen/font_mediumlarge"
android:hint="@string/txt_hint"
android:textColorHint="@color/black"
android:gravity="right"
android:maxLines="1"
android:maxLength="10"
android:singleLine="true"
android:inputType="numberDecimal"
android:digits="0123456789." >
There are a couple of things to point out. I am only allowing {0-9 and . } input. I also max the length to 10. And (although probably redundant) I limit lines to 1.
Next, my code. This is where issues occur:
1) If you input a period, without any other input this causes a crash. I fixed this by handling this specific case like
if (etBill.getText().toString().contentEquals(".")) {
// handle special case
etBill.setText("0.00"); }
2) If you input 8 values, so lets say the string in the editText is "12345678" I would receive a StackOverflowError because I am updating the value in editText in dollar amount form. So, in this scenario the editText attempts to update to 12345678.00. This is an issue because I only allow maxLength = 10. I had to handle this specific case like
// handle special case
if (integerPlaces == 7 &&
etBill.getText().toString().contains(".") == false) {
etBill.setText(strFormatted);
}
This case was not my best, but basically before you can even input 8 integers without a decimal, it will edit the editText in the proper dollar form (strFormatted is the proper dollar format "0.00").
3) If you input more than two decimals places, this also becomes an issue. In otherwords, 1.234 is not a valid dollar format, so I had to handle that case like
// handle special case
if (decimalPlaces >= 3) {
etBill.setText(strFormatted);
}
There are more cases, but I think I have provided enough to ask my question. Is this how I am suppose to program this? Case by case? This does not seem practical, because there are many exception cases. For example, what if the user tries to input two periods. I had to handle that case by changing the inputType of the editText if it already contained a period, otherwise this would cause an exception error as well.
Even more, if the user inputs 1234567.0 and then attempts to add another value to the left of the decimal, once updated to dollar form this would cause an issue too.
What is a better method? I read some information on Regex and I thought that maybe this would help prevent me from having to handle as many special cases because then I could do checks on patterns. But I do not really know, I have never used it before (and it is a bit confusing!).
My application works fine. But because I am still fairly new to programming, this approach feels sloppy. Sorry for long post, and thanks in advance!