androidandroid-edittextfocuslistener

Edittext convert input value to float in android


I have one Edittext.My edittext inputType is numberDecimal

 <EditText
                            android:layout_width="wrap_content"
                            android:layout_height="@dimen/u_widget_height"
                            android:id="@+id/transfer_maney"
                            android:layout_alignParentTop="true"

                            android:singleLine="true"
                            android:inputType="numberDecimal"
                            android:hint="Amount"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_toLeftOf="@+id/centerline"
                            android:layout_toStartOf="@+id/centerline"
                            android:layout_marginRight="@dimen/u_common_margin_left"
                            />

i want to change input value when focus finished in my Editytext.i wrote some java code but i have NumberFormatException

  transfer_maney.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            float inputFloatValue;
            if(!hasFocus)
            {
                if (TextUtils.isEmpty(contact_search.getText().toString())) {
                    inputFloatValue = 0F;

                } else {
                    inputFloatValue = Float.parseFloat(contact_search.getText().toString().trim());
                    Log.e("inputFloatValue",inputFloatValue+"");
                }
            }

        }
    });

simple i want to change input value integer to float when i finish focus in my Edittext if anyone knows solution please help me thanks


Solution

  • Change

    float inputFloaValue= Float.parseFloat(transfer_maney.getText().toString().trim());
    

    to:

    float inputFloatValue;
    if (TextUtils.isEmpty(transfer_maney.getText().toString())) {
      inputFloatValue = 0F;
    } else {
      inputFloatValue = Float.parseFloat(transfer_maney.getText().toString().trim());
    }