The following Android code snippet shows you how to customize the input value format of an EditText
component for accepting currency number. To format the input value we will create a text watcher class called MoneyTextWatcher
, this class implements of the android.text.TextWatcher
interface.
In the MoneyTextWatcher
class we implement the afterTextChanged(EditText s)
method, in this method the currency formatting takes place by adding currency symbol before the digit on numbers.
To apply this text watcher class we call the addTextChangedListener()
method of the EditText
object of the currency input and pass an instance of MoneyTextWatcher
. Below is the working example of the activity class and the text watcher.
package org.kodejava.android;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.math.BigDecimal;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
editText.addTextChangedListener(new MoneyTextWatcher(editText));
editText.setText("0");
textView = findViewById(R.id.textView);
}
public void doGetValue(View view) {
BigDecimal value = MoneyTextWatcher.parseCurrencyValue(editText.getText().toString());
textView.setText(String.valueOf(value));
}
}
When the EditText
input is changed the afterTextChanged()
method of the text watcher will be called. In this method we’ll take the input text and format the input value with currency symbol and a number, this is done by the NumberFormat.getCurrencyInstance()
object.
The static helper method parseCurrencyValue()
will get the number part of the EditText
by removing the currency symbol and return the input number.
package org.kodejava.android;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import java.lang.ref.WeakReference;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Objects;
public class MoneyTextWatcher implements TextWatcher {
private static final Locale locale = new Locale("id", "ID");
private static final DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
private final WeakReference<EditText> editTextWeakReference;
public MoneyTextWatcher(EditText editText) {
editTextWeakReference = new WeakReference<>(editText);
formatter.setMaximumFractionDigits(0);
formatter.setRoundingMode(RoundingMode.FLOOR);
DecimalFormatSymbols symbol = new DecimalFormatSymbols(locale);
symbol.setCurrencySymbol(symbol.getCurrencySymbol() + " ");
formatter.setDecimalFormatSymbols(symbol);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
EditText editText = editTextWeakReference.get();
if (editText == null || editText.getText().toString().isEmpty()) {
return;
}
editText.removeTextChangedListener(this);
BigDecimal parsed = parseCurrencyValue(editText.getText().toString());
String formatted = formatter.format(parsed);
editText.setText(formatted);
editText.setSelection(formatted.length());
editText.addTextChangedListener(this);
}
public static BigDecimal parseCurrencyValue(String value) {
try {
String replaceRegex = String.format("[%s,.\\s]",
Objects.requireNonNull(formatter.getCurrency()).getSymbol(locale));
String currencyValue = value.replaceAll(replaceRegex, "");
currencyValue = "".equals(currencyValue) ? "0" : currencyValue;
return new BigDecimal(currencyValue);
} catch (Exception e) {
Log.e("App", e.getMessage(), e);
}
return BigDecimal.ZERO;
}
}
The following animation is the result of our code snippet above, and you can find to complete source code here:
- How do I split large excel file into multiple smaller files? - April 15, 2023
- How do I get the number of processors available to the JVM? - March 29, 2023
- How do I show Spring transaction in log / console? - March 29, 2023
Cara agar langsung memunculkan Rp.0 tanpa harus mengetikan value dulu itu bagaimana ya? Terima kasih
Halo Enrico,
Cara paling sederhana bisa dengan memberikan nilai “0” ke
edittext
setelah melakukanaddTextChangedListener(...)
, seperti code snippet berikut:Semoga membantu.
Terima kasih, sangat membantu. Apakah bisa menyisipkan spasi sebelum digit angka?
Hi Eto,
The code snippet below might help you if you want to add space after the currency symbol. We need to change the
NumberFormat
toDecimalFormat
so that we can change the currency symbol usingDecimalFormatSymbols
.Terimakasih banyak mas..