Format currency string using EditText in Android - java

I'm trying to format the input value of an EditText in Android, I want to format the input in currency value, I' ve tried the following:
EditText minimo = (EditText) view.findViewById(R.id.minimo);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
minimo.setText("", TextView.BufferType.valueOf(String.valueOf(fmt.format(TextView.BufferType.EDITABLE))));
Log:
java.lang.IllegalArgumentException: Bad class: class android.widget.TextView$BufferType

try this :
set TextChangedListner as :
minimo.addTextChangedListener(new NumberTextWatcher(minimo));
create custom TextWatcher as:
class NumberTextWatcher implements TextWatcher {
private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;
private EditText et;
public NumberTextWatcher(EditText et)
{
df = new DecimalFormat("#,###.##");
df.setDecimalSeparatorAlwaysShown(true);
dfnd = new DecimalFormat("#,###");
this.et = et;
hasFractionalPart = false;
}
#SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";
public void afterTextChanged(Editable s)
{
et.removeTextChangedListener(this);
try {
int inilen, endlen;
inilen = et.getText().length();
String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
Number n = df.parse(v);
int cp = et.getSelectionStart();
if (hasFractionalPart) {
et.setText(df.format(n));
} else {
et.setText(dfnd.format(n));
}
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel <= et.getText().length()) {
et.setSelection(sel);
} else {
// place cursor at the end?
et.setSelection(et.getText().length() - 1);
}
} catch (NumberFormatException nfe) {
// do nothing?
} catch (ParseException e) {
// do nothing?
}
et.addTextChangedListener(this);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
{
hasFractionalPart = true;
} else {
hasFractionalPart = false;
}
}
}

Related

Replace "," with ". " in TextWatcher

I am using this TextWatcher for .addtextOnChangeListener, the output of the string is ex: "123,456,77" i want it to be "123.456,77" if i use the replace method on "et" in the "afterTextChanged" method, the number isn't even formatting. With the code below the listener works and everything, i just don't know how to replace the "," with "." until the decimals.
If you think to just change the pattern ("###,##0,00") it doesn't work
This is the TextWatcher I have for the EditText
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Locale;
public class NumberTextWatcher implements TextWatcher {
private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;
private EditText et;
public NumberTextWatcher(EditText et)
{
df = new DecimalFormat("###,##0,00");
df.setDecimalSeparatorAlwaysShown(true);
dfnd = new DecimalFormat("###,##0,00");
this.et = et;
hasFractionalPart = false;
}
#SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";
#Override
public void afterTextChanged(Editable s)
{
et.removeTextChangedListener(this);
try {
int inilen, endlen;
inilen = et.getText().length();
String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
Number n = df.parse(v);
int cp = et.getSelectionStart();
if (hasFractionalPart) {
et.setText(df.format(n));
} else {
et.setText(dfnd.format(n));
}
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel <= et.getText().length()) {
et.setSelection(sel);
} else {
// place cursor at the end?
et.setSelection(et.getText().length() - 1);
}
} catch (NumberFormatException nfe) {
// do nothing?
} catch (ParseException e) {
// do nothing?
}
et.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int index = s.toString().indexOf(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()));
int trailingZeroCount = 0;
if (index > -1)
{
for (index++; index < s.length(); index++) {
if (s.charAt(index) == '0')
trailingZeroCount++;
else {
trailingZeroCount = 0;
}
}
hasFractionalPart = true;
} else {
hasFractionalPart = false;
}
{
if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
{
hasFractionalPart = true;
} else {
hasFractionalPart = false;
}
}
}
}
Use this:
DecimalFormat decimalFormat=new DecimalFormat();
DecimalFormatSymbols decimalFormatSymbols=DecimalFormatSymbols.getInstance();
decimalFormatSymbols.setDecimalSeparator(',');
decimalFormatSymbols.setGroupingSeparator('.');
decimalFormat.setDecimalFormatSymbols(decimalFormatSymbols);
String formattedNumber=decimalFormat.format(123456.78);// prints 123.456,78

DecimalFormat issue with EditText Input

Hello i'm writing this program to get user input and format to 0,000.00 for example
the program works fine when i try 5,768.80 BUT it doesn't when i try 5,768.08
as you can see the problem is that i can't put a 0 in the cents place before any other number... here is my code:
package com.calculadorabss.gorydev.calculadorabolivaressoberanos;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import java.text.DecimalFormat;
import java.text.ParseException;
class NumberTextWatcher implements TextWatcher {
private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;
private EditText et;
//Dar formato al texto de entrada separando por comas
public NumberTextWatcher(EditText et)
{
df = new DecimalFormat("#,###,##");
df.setDecimalSeparatorAlwaysShown(true);
dfnd = new DecimalFormat("#,###,##");
this.et = et;
hasFractionalPart = false;
}
#SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";
public void afterTextChanged(Editable s)
{
et.removeTextChangedListener(this);
try {
int inilen, endlen;
inilen = et.getText().length();
String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
Number n = df.parse(v);
int cp = et.getSelectionStart();
if (hasFractionalPart) {
et.setText(df.format(n));
} else {
et.setText(dfnd.format(n));
}
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel <= et.getText().length()) {
et.setSelection(sel);
} else {
// place cursor at the end?
et.setSelection(et.getText().length() - 1);
}
} catch (NumberFormatException nfe) {
// do nothing?
} catch (ParseException e) {
// do nothing?
}
et.addTextChangedListener(this);
}
}
i want the program to be able to receive 0 as cents for example 67,789.05
Try this i think it work
public void afterTextChanged(Editable s)
{
et.removeTextChangedListener(this);
try {
int inilen, endlen;
inilen = et.getText().length();
String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
hasFractionalPart = v.contains(".");
Number n = df.parse(v);
int cp = et.getSelectionStart();
if (!hasFractionalPart) {
et.setText(dfnd.format(n));
}
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel <= et.getText().length()) {
et.setSelection(sel);
} else {
// place cursor at the end?
et.setSelection(et.getText().length() - 1);
}
} catch (NumberFormatException nfe) {
// do nothing?
} catch (ParseException e) {
// do nothing?
}
et.addTextChangedListener(this);
}

DecimalFormat java allow to type 0 after dot

I have a DecimalFormat with the following pattern "#,###.###"
But when I want to type for example 50.05 it does not allow to, as I parse the string to number every time a text changes and when I call
df.parse("50.0");
it returns 50. and cuts the 0
Any ideas what can I do to be able to type numbers like 50.05 ?
The full code of my class is presented below. Sorry if it is too verbose:
private class NumberTextWatcher implements TextWatcher {
#SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";
private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;
private EditText et;
NumberTextWatcher(EditText et) {
df = (DecimalFormat) DecimalFormat.getInstance(Locale.ENGLISH);
df.applyLocalizedPattern(hasThousandSeparator ? "#,###.###" : "####.###");
df.setDecimalSeparatorAlwaysShown(true);
df.setMaximumFractionDigits(maxDecimal);
dfnd = (DecimalFormat) DecimalFormat.getInstance(Locale.ENGLISH);
dfnd.applyLocalizedPattern(hasThousandSeparator ? "#,###" : "####");
dfnd.setMaximumFractionDigits(maxDecimal);
this.et = et;
hasFractionalPart = false;
}
#Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
try {
int inilen, endlen;
inilen = et.getText().length();
String sign = hasSign ? s.toString().startsWith("-") ? "-" : "+" : null;
String sStr = hasSignAttached(s) ? s.toString().substring(1) : s.toString();
String v = sStr.replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
if(v.length() == 0){
return;
}
if(hasFractionalPart){
String[] vSplit = v.split("\\.");
String intStr = vSplit[0];
String decStr = vSplit.length > 1 ? vSplit[1] : "";
if(intStr.length() > maxNumbers)
intStr = intStr.substring(0, maxNumbers);
if(decStr.length() > maxDecimal)
decStr = decStr.substring(0, maxDecimal);
v = String.format("%s.%s", intStr, decStr);
} else {
if(v.length() > maxNumbers)
v = v.substring(0, maxNumbers);
}
Number n = df.parse(v);
int cp = et.getSelectionStart();
String formatted;
if (hasFractionalPart) {
formatted = df.format(n);
} else {
formatted = dfnd.format(n);
}
et.setText(hasSign ? String.format("%s%s", sign, formatted) : formatted);
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel <= et.getText().length()) {
et.setSelection(sel);
} else {
// place cursor at the end?
et.setSelection(et.getText().length() - 1);
}
} catch (Exception e) {
ExceptionTracker.trackException(e);
} finally {
et.addTextChangedListener(this);
}
}
private boolean hasSignAttached(Editable s){
return s.toString().startsWith("+") || s.toString().startsWith("-");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(hasSign && start == 1 && count == 1){
et.setText("");
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
hasFractionalPart = s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()));
}
}
this code solves the problem:
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat dformat = new DecimalFormat("#,###.###");
dformat.setMinimumFractionDigits(1);
String someNumber = "50.05";
Double someNumberDouble = Double.valueOf(someNumber);
System.out.println(dformat.format(someNumberDouble)); // 50.05
someNumber = "50.0";
someNumberDouble = Double.valueOf(someNumber);
System.out.println(dformat.format(someNumberDouble)); // 50.0
}
}
Ok, I have done a small hack to fix this. May be it will help someone in the future, or someone will suggest a better solution.
So I added the following checks:
boolean decHasZeroAtEnd = false;
.......
if(decStr.endsWith("0")){
decHasZeroAtEnd = true;
}
.......
if(decHasZeroAtEnd){
decHasZeroAtEnd = false;
formatted = String.format("%s0", formatted);
}
And the whole code is the following:
private class NumberTextWatcher implements TextWatcher {
#SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";
private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;
private EditText et;
NumberTextWatcher(EditText et) {
df = (DecimalFormat) DecimalFormat.getInstance(Locale.ENGLISH);
df.applyLocalizedPattern(hasThousandSeparator ? "#,###.###" : "####.###");
df.setDecimalSeparatorAlwaysShown(true);
df.setMaximumFractionDigits(maxDecimal);
dfnd = (DecimalFormat) DecimalFormat.getInstance(Locale.ENGLISH);
dfnd.applyLocalizedPattern(hasThousandSeparator ? "#,###" : "####");
dfnd.setMaximumFractionDigits(maxDecimal);
this.et = et;
hasFractionalPart = false;
}
#Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
try {
int inilen, endlen;
inilen = et.getText().length();
String sign = hasSign ? s.toString().startsWith("-") ? "-" : "+" : null;
String sStr = hasSignAttached(s) ? s.toString().substring(1) : s.toString();
String v = sStr.replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
if(v.length() == 0){
return;
}
boolean decHasZeroAtEnd = false;
if(hasFractionalPart){
String[] vSplit = v.split("\\.");
String intStr = vSplit[0];
String decStr = vSplit.length > 1 ? vSplit[1] : "";
if(intStr.length() > maxNumbers)
intStr = intStr.substring(0, maxNumbers);
if(decStr.length() > maxDecimal)
decStr = decStr.substring(0, maxDecimal);
if(decStr.endsWith("0")){
decHasZeroAtEnd = true;
}
v = String.format("%s.%s", intStr, decStr);
} else {
if(v.length() > maxNumbers)
v = v.substring(0, maxNumbers);
}
Number n = df.parse(v);
int cp = et.getSelectionStart();
String formatted;
if (hasFractionalPart) {
formatted = df.format(n);
} else {
formatted = dfnd.format(n);
}
if(decHasZeroAtEnd){
decHasZeroAtEnd = false;
formatted = String.format("%s0", formatted);
}
Log.d("testt", "formatted " + formatted);
et.setText(hasSign ? String.format("%s%s", sign, formatted) : formatted);
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel <= et.getText().length()) {
et.setSelection(sel);
} else {
// place cursor at the end?
et.setSelection(et.getText().length() - 1);
}
} catch (Exception e) {
ExceptionTracker.trackException(e);
} finally {
et.addTextChangedListener(this);
}
}
private boolean hasSignAttached(Editable s){
return s.toString().startsWith("+") || s.toString().startsWith("-");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(hasSign && start == 1 && count == 1){
et.setText("");
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
hasFractionalPart = s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()));
}
}

EditText mask with 3 decimal places in java [android]

i'm newbie in Android and i want to do a mask for an EditText with a decimal number with 3 decimal places(ex: 0.658), i need a mask that user doesn't need write the ".", only the numbers, like a conventional mask for currency.
I'm trying create a TextWatcher based in this:
public static TextWatcher currency(final EditText editText) {
return new TextWatcher() {
String current = "";
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
editText.removeTextChangedListener(this);
String cleanString = s.toString();
if(count != 0) {
String substr = cleanString.substring(cleanString.length() - 2);
if (substr.contains(".") || substr.contains(",")) {
cleanString += "0";
}
}
cleanString = cleanString.replaceAll("[R$,.]", "");
double parsed = Double.parseDouble(cleanString);
Locale locale = new Locale("pt", "BR");
String formatted = NumberFormat.getCurrencyInstance(locale).format((parsed / 100));
formatted = formatted.replaceAll("[R$]", "");
current = formatted;
editText.setText(formatted);
editText.setSelection(formatted.length());
editText.addTextChangedListener(this);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {}
};
}
But without success.
There are a better way to do this?
Thanks.
Try to use MaskedEditText from github.
I had a similar problem. So I started looking for any implementation of TextWatcher. I finally decided to build my own:
public class BrRealMoneyTextWatcher implements TextWatcher {
private static final Locale DEFAULT_LOCALE = new Locale("pt", "BR");
private static DecimalFormat NUMBER_FORMAT = (DecimalFormat) DecimalFormat.getCurrencyInstance(DEFAULT_LOCALE);
private static final int FRACTION_DIGITS = 2;
private static final String DECIMAL_SEPARATOR;
private static final String CURRENCY_SIMBOL;
static {
NUMBER_FORMAT.setMaximumFractionDigits(FRACTION_DIGITS);
NUMBER_FORMAT.setMaximumFractionDigits(FRACTION_DIGITS);
NUMBER_FORMAT.setParseBigDecimal(true);
DECIMAL_SEPARATOR = String.valueOf(NUMBER_FORMAT.getDecimalFormatSymbols().getDecimalSeparator());
CURRENCY_SIMBOL = NUMBER_FORMAT.getCurrency().getSymbol(DEFAULT_LOCALE);
}
final EditText target;
public BrRealMoneyTextWatcher(EditText target) {
this.target = target;
}
private boolean updating = false;
#Override
public void onTextChanged(CharSequence s, int start, int before, int after) {
if (updating) {
updating = false;
return;
}
updating = true;
try {
String valueStr = formatNumber(fixDecimal(s.toString()));
BigDecimal parsedValue = ((BigDecimal) NUMBER_FORMAT.parse(valueStr));
String value = NUMBER_FORMAT.format(parsedValue);
target.setText(value);
target.setSelection(value.length());
} catch (ParseException | NumberFormatException ex) {
throw new IllegalArgumentException("Erro ao aplicar a máscara", ex);
}
}
private String formatNumber(String originalNumber) {
String number = originalNumber.replaceAll("[^\\d]", "");
switch(number.length()) {
case 0 :
number = "0" + DECIMAL_SEPARATOR + "00";
break;
case 1 :
number = "0" + DECIMAL_SEPARATOR + "0" + number;
break;
case 2 :
number = "0" + DECIMAL_SEPARATOR + number;
break;
default:
number = number.substring(0, number.length() - 2) + DECIMAL_SEPARATOR + number.substring(number.length() - 2);
break;
}
return CURRENCY_SIMBOL + number;
}
private String fixDecimal(String number) {
int dotPos = number.indexOf('.') + 1;
int length = number.length();
return (length - dotPos < FRACTION_DIGITS) ? fixDecimal(number + "0") : number;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
}
I hope it helps somebody else.

TextWatcher and SharedPreferences don't recognize Edittext after first value is entered

I am encountering a problem in my Android application. The problem is that the Edittext is not being changed with a TextWatcher and a SharedPreferences. IT WORKS FINE WITHOUT A SHAREDPREFERENCES. Here's my code:
public interface CurrencyConverter {
public double convert(String currencyFrom, String currencyTo)
throws Exception;
}
public class YahooCurrencyConverter implements CurrencyConverter {
public double convert(String currencyFrom, String currencyTo)
throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom
+ currencyTo + "=X&f=l1&e=.csv");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpGet, responseHandler);
httpclient.getConnectionManager().shutdown();
return Double.parseDouble(responseBody);
}
}
public String convertvalues(String convertfrom, String convertto) {
double current;
double val = Double.parseDouble(edittextdollars.getText()
.toString());
DecimalFormat df = new DecimalFormat(".##");
YahooCurrencyConverter ycc = new YahooCurrencyConverter();
try {
current = ycc.convert(convertfrom, convertto);
edittexteuros.setText(df.format(val * current));
return "passed";
} catch (Exception e) {
return "passed";
}
}
private void addListenerOnButton2() {
edittextdollars.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
String text1 = spinner1.getSelectedItem().toString().trim();
String text2 = spinner2.getSelectedItem().toString().trim();
//TextView myOutputBox = (TextView) findViewById(R.id.myOutputBox);
//myOutputBox.setText(s);
String hello = edittextdollars.getText().toString();
if (hello.matches("")) {
img1.setImageDrawable(null);
edittexteuros.setText("");
}
if (edittextdollars.getText().toString().length() != 0) {
if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR")) {
try {
convertvalues("USD", "EUR");
getGraph("USD", "EUR");
String hello1 = edittextdollars.getText().toString();
double hello2 = Double.parseDouble(hello1);
String hello3 = edittexteuros.getText().toString();
double hello4 = Double.parseDouble(hello3);
String h = Double.toString(hello4 / hello2);
SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("rohit", 0);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(getString(R.string.data1), h);
editor.commit();
} catch(Exception e) {
SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("rohit", 0);
String name2 = sharedPrefs.getString(getString(R.string.data1), "not found");
double name3 = Double.parseDouble(name2);
String hello1 = edittextdollars.getText().toString();
double hello2 = Double.parseDouble(hello1);
double ee = hello2 * name3;
//String h = Double.toString(ee);
//edittexteuros.setText(h);
DecimalFormat df = new DecimalFormat(".##");
edittexteuros.setText(df.format(hello2 * name3));
}
}
It works here:
But it doesn't here:
By the way, edittextdollars is the top edittext, edittexteuros is the bottom edittext, spinner1 is the top spinner, and spinner2 is the bottom spinner.
What is wrong here? What problem have I encountered? The reason I use sharedpreferences is when the internet connection is down. As I said, it works flawlessly with internet connection. So what did I add in the SharedPreferences that was wrong?
Any help would be appreciated regarding this problem.

Categories

Resources