I would like to display the input of the EditText fields with two decimals at all times. So when the user enters 5 it will show 5.00 or when the user enters 7.5 it will show 7.50.
Besides that I would like to also show zero when the field is empty instead of nothing.
What I've got already is the inputtype set to:
android:inputType="number|numberDecimal"/>
Should I work with inputfilters here?
Sorry I still quite new to android / java...
Thanks for your help!
With the answer of nickfox I was able to solve half of my question.
et.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) {
if(s.toString().matches(""))
{
et.setText("0.00");
Selection.setSelection(et.getText(), 0, 4);
}
}
});
I'm still working on a solution for the other half of my question. If I found the solution I will post it here too.
OnFocusChangeListener FocusChanged = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
String userInput = et.getText().toString();
int dotPos = -1;
for (int i = 0; i < userInput.length(); i++) {
char c = userInput.charAt(i);
if (c == '.') {
dotPos = i;
}
}
if (dotPos == -1){
et.setText(userInput + ".00");
} else {
if ( userInput.length() - dotPos == 1 ) {
et.setText(userInput + "00");
} else if ( userInput.length() - dotPos == 2 ) {
et.setText(userInput + "0");
}
}
}
}
format("%. 2f", d) , your double will be rounded automatically. Save this answer.
Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.
4.732 rounded to 2 decimal places would be 4.73 (because it is the nearest number to 2 decimal places). 4.737 rounded to 2 decimal places would be 4.74 (because it would be closer to 4.74).
setFilters(new InputFilter[]{ new InputFilterMinMax("1", "12")}); This will allow user to enter values from 1 to 12 only.
Update #2
Correct me if I'm wrong, but oficial docs to TextWatcher say that it's legitimate use afterTextChanged
method for make changes to... EditText content for this task.
I have the same task in my multy-language app and as I know it's possible ,
or .
symbols as separator so I modify nickfox answer for 0.00 format with total limit of symbols to 10:
Layout (Updated):
<com.custom.EditTextAlwaysLast
android:id="@+id/et"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:maxLength="10"
android:layout_marginTop="50dp"
android:inputType="numberDecimal"
android:gravity="right"/>
EditTextAlwaysLast class:
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.EditText;
/**
* Created by Drew on 16-01-2015.
*/
public class EditTextAlwaysLast extends EditText {
public EditTextAlwaysLast(Context context) {
super(context);
}
public EditTextAlwaysLast(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextAlwaysLast(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
//if just tap - cursor to the end of row, if long press - selection menu
if (selStart==selEnd)
setSelection(getText().length());
super.onSelectionChanged(selStart, selEnd);
}
}
Code in ocCreate method (Update #2):
EditTextAlwaysLast amountEditText;
Pattern regex;
Pattern regexPaste;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
amountEditText = (EditTextAlwaysLast)findViewById(R.id.et);
DecimalFormatSymbols dfs = new DecimalFormatSymbols(getResources().getConfiguration().locale);
final char separator = dfs.getDecimalSeparator();
//pattern for simple input
regex = Pattern.compile("^(\\d{1,7}["+ separator+"]\\d{2}){1}$");
//pattern for inserted text, like 005 in buffer inserted to 0,05 at position of first zero => 5,05 as a result
regexPaste = Pattern.compile("^([0]+\\d{1,6}["+separator+"]\\d{2})$");
if (amountEditText.getText().toString().equals(""))
amountEditText.setText("0"+ separator + "00");
amountEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (!s.toString().matches(regex.toString())||s.toString().matches(regexPaste.toString())){
//Unformatted string without any not-decimal symbols
String coins = s.toString().replaceAll("[^\\d]","");
StringBuilder builder = new StringBuilder(coins);
//Example: 0006
while (builder.length()>3 && builder.charAt(0)=='0')
//Result: 006
builder.deleteCharAt(0);
//Example: 06
while (builder.length()<3)
//Result: 006
builder.insert(0,'0');
//Final result: 0,06 or 0.06
builder.insert(builder.length()-2,separator);
amountEditText.setText(builder.toString());
}
amountEditText.setSelection(amountEditText.getText().length());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
}
It's look like the best result for me. Now this code support copy-paste actions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With