EditText
, for example, a number which has a lot of followed 0 numbers, user may be confused or mistyped the number value. So, in some cases, you - as a developer - should provide a format for the number input type. Most popular, you can insert the comma (,) or dot (.) as thousand separators for input data and it's will automatic adding when user typing.In this tip, I would like to provide a solution through set
TextWatcher
for EditText
(handling text input changed to format the number).DEMO VIDEO:
TextWatcher
to handling EditText
input changed. You will format the input value through using a DecimalFormat
instance with a pattern String
argument. Whole of this process will be written in afterTextChanged(Editable s)
method. You must call removeTextChangedListener()
first and at the last of this, addTextChangedListener()
back:
private TextWatcher onTextChangedListener() {
return new TextWatcher() {
@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.removeTextChangedListener(this);
try {
String originalString = s.toString();
Long longval;
if (originalString.contains(",")) {
originalString = originalString.replaceAll(",", "");
}
longval = Long.parseLong(originalString);
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
formatter.applyPattern("#,###,###,###");
String formattedString = formatter.format(longval);
//setting text after format to EditText
editText.setText(formattedString);
editText.setSelection(editText.getText().length());
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
editText.addTextChangedListener(this);
}
};
}
And you can set TextWatcher
for the EditText
by this code:
editText.addTextChangedListener(onTextChangedListener());
NOTE
: Any EditText
that using this TextWatcher
must have android:inputType="number"
property. For example, this is my main Activity
layout:
activity_main.xml
When click at the <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<EditText
android:id="@+id/txt_number"
android:hint="Input number"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_number"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:text="Get Input Text" />
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:textStyle="bold" />
</RelativeLayout>
Button
, I will get both formatted and original input values to TextView
. So, this is full code for the main Activity
:
MainActivity.java
Running application, we have this output:package info.devexchanges.numberformat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.txt_number);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.text_view);
editText.addTextChangedListener(onTextChangedListener());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//showing formatted text and original text of EditText to TextView
textView.setText(String.format("Formatted number value: %s\nOriginal input: %s",
editText.getText().toString(),
editText.getText().toString().replaceAll(",", "")));
}
});
}
private TextWatcher onTextChangedListener() {
return new TextWatcher() {
@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.removeTextChangedListener(this);
try {
String originalString = s.toString();
Long longval;
if (originalString.contains(",")) {
originalString = originalString.replaceAll(",", "");
}
longval = Long.parseLong(originalString);
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
formatter.applyPattern("#,###,###,###");
String formattedString = formatter.format(longval);
//setting text after format to EditText
editText.setText(formattedString);
editText.setSelection(editText.getText().length());
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
editText.addTextChangedListener(this);
}
};
}
}
Now, the "formatting number in real time" work has been finished. Hope this helpful with your developing work. Moreover, visit this tag link to read all blog posts about
EditText
in Android.