Samir Bhatt
Samir Bhatt

Reputation: 3261

Edittext text select-all and then type first letter will not displayed

There is an awkward scenario happening in my application.

My application has functionality like EditText for the price, where you tap on it will select all text, then you can enter a price. When user clicking on EditText, it is selecting all text but, when the user enters any text, the first number will not be displayed. Then another number will be displayed properly. I have added TextChangeListner and try to figure out but I am getting 0 CharSequence while users enter the first character.

I have inputfilter for EditText. Check below code for editfilter.

public static class InputFilterForDoubleMinMax implements InputFilter {

    private double min, max;

    public InputFilterForDoubleMinMax(double min, double max) {
        this.min = min;
        this.max = max;
    }

    public InputFilterForDoubleMinMax(String min, String max) {
        this.min = Double.parseDouble(min);
        this.max = Double.parseDouble(max);
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            String temp = dest.toString() + source.toString();
            if (source.toString().equalsIgnoreCase(".")) {
                return "";
            } else if (temp.toString().indexOf(",") != -1) {
                temp = temp.toString().substring(temp.toString().indexOf(",") + 1);
                if (temp.length() > 2) {
                    return "";
                }
            }


            double input = Double.parseDouble(dest.toString().replace(",", ".").replace("€", "") + source.toString().replace(",", ".").replace("€", ""));
            if (isInRange(min, max, input))
                return null;
        } catch (NumberFormatException nfe) {
        }
        return "";
    }

    private boolean isInRange(double a, double b, double c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }

}

Any help appreciated.

Upvotes: 0

Views: 894

Answers (2)

Samir Bhatt
Samir Bhatt

Reputation: 3261

The issue is resolved. the issue was due to logic implemented inside input filter.

Following is my updated code input filter.

  public static class InputFilterForDoubleMinMax implements InputFilter {

        private double min, max;

        public InputFilterForDoubleMinMax(double min, double max) {
            this.min = min;
            this.max = max;
        }

        public InputFilterForDoubleMinMax(String min, String max) {
            this.min = Double.parseDouble(min);
            this.max = Double.parseDouble(max);
        }

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            try {
                String temp;
                if (dstart == 0) {
                    temp = source.toString();
                } else {
                    temp = dest.toString() + source.toString();
                }
                if (source.toString().equalsIgnoreCase(".")) {
                    return "";
                } else if (temp.toString().indexOf(",") != -1) {
                    temp = temp.toString().substring(temp.toString().indexOf(",") + 1);
                    if (temp.length() > 2) {
                        return "";
                    }
                }


                double input;
                if (dstart == 0) {
                    input = Double.parseDouble(source.toString().replace(",", ".").replace("€", ""));

                } else {
                    input = Double.parseDouble(dest.toString().replace(",", ".").replace("€", "") + source.toString().replace(",", ".").replace("€", ""));
                }

                if (isInRange(min, max, input))
                    return null;
            } catch (NumberFormatException nfe) {
            }
            return "";
        }

        private boolean isInRange(double a, double b, double c) {
            return b > a ? c >= a && c <= b : c >= b && c <= a;
        }

    }

Upvotes: 1

Rahul
Rahul

Reputation: 5049

This is working fine.

package com.imrhk.testapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText editText = (EditText) findViewById(R.id.edit_text);

        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(editText.getText().toString())) {
                    editText.selectAll();
                }
            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if(editText.getSelectionStart() != -1) {
                    //editText.setText("");
                    editText.setSelected(false);
                }
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"/>
</LinearLayout>

edit:

Add this as filter

editText.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if(String.valueOf(source).equals(".") && String.valueOf(dest).equals("100"))    //replace it with maxed value
                    return "";
                try {
                    Float f = Float.parseFloat(String.valueOf(dest) + String.valueOf(source.toString()));
                    if (f < 0.0f || f > 100.0f) //replace it with max/min value
                        return "";
                    return null;
                } catch (NumberFormatException e) {
                    return null;
                }
            }
        }});

Upvotes: 0

Related Questions