Sagar Garg
Sagar Garg

Reputation: 111

Android EditText - decimal separator

I have two EditTexts. In the first one, the user inputs a number, can be a decimal. In the second EditText, the number is displayed, but formatted, rounded. I made the second EditText not clickable since the user shouldn't be able to change this value. I prefer to use an EditText instead of a TextView because of the style. The problem is with foreign numbers with a comma decimal error. My app is getting this error java.lang.NumberFormatException: Invalid double: "1,60934" by people who use a comma separator.

XML file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:gravity="center"
        android:id="@+id/value1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/value2"
        android:layout_marginTop="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center"/>
</RelativeLayout>

Java file

public class MainActivity extends AppCompatActivity {

    public static EditText value1;
    public static EditText value2;
    public static double value1var;
    public static double value2var;
    public static DecimalFormat df;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        df = new DecimalFormat("0.#####", new DecimalFormatSymbols(Locale.FRANCE));

        value1 = (EditText)findViewById(R.id.value1);
        value2 = (EditText)findViewById(R.id.value2);

        value1.setText(df.format(1.0));
        value2.setText(df.format(1000));

        value1var = Double.parseDouble(value1.getText().toString());
        value2var = Double.parseDouble(value2.getText().toString());

        value1.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                String value = s.toString();
                double valuevar = Double.parseDouble(s.toString());

                if(value.length() > 0){
                    value2.setText(df.format(valuevar));
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
            }
        });
    }

Any ideas how to fix it?

Upvotes: 2

Views: 1039

Answers (1)

piotrek1543
piotrek1543

Reputation: 19351

According to this part of code:

value1.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                String value = s.toString();
                double valuevar = Double.parseDouble(s.toString());

                if(value.length() > 0){
                    value2.setText(df.format(valuevar));
                }
            }

As you noticed, Java throws an exception if there's a comma separator.

To fix this problem, add if given string has comma, I mean write code like this

if (value1.contains(",")) {
//Here implement `for` loop, which would check which char of string has this problematic sign.

//Use string.charAt(i) function to compare

// use string.replace(int begin, int end, CharSequence cs) function to change it to point instead of comma.\
}

You can also prompt a user with AlertDialog if comma char would be detected, but as you may think it won't fix this problem entirely as changing automatically this char.

Play with String and Dialogs to make a validation of these fields.

Hope it help

Upvotes: 1

Related Questions