Question

So I have a view that has two edit texts that are using textwatchers as text change listeners. when the value from first edit text is changed a new value is calculated and set to the 2nd edit text and vice versa. I am removing the textwatchers before setting the text. the problem I am facing is when I type something in the first or the second edit text box it takes only one or two numbers and stops taking any input. the cursor goes from the end to the start. here is the class

public class CurrencyView extends RelativeLayout implements ICustomView{

    private class CustomOnClick implements OnClickListener{
        @Override
        public void onClick(View v) {
            final TextView view = (TextView)v;
            final String options[] = ConversionTypes.getCurrencyTypes();

            AlertDialog.Builder builder = new AlertDialog.Builder(rContext);

            builder.setItems(options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    view.setText(options[which]);
                    convert(view.getId());      
                }


            });
            builder.show();

        }
    }

    private class CustomTextWatcher implements TextWatcher{

        int ID;
        @Override
        public void afterTextChanged(Editable s) {
            convert(ID);

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            if(s.toString().equals(type1Value.getText().toString())){
                ID=11;
            }
            else{
                ID=22;
            }

        }

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

    }

    private TextView currencyType1;
    private LayoutParams currencyType1LP;
    private TextView currencyType2;
    private LayoutParams currencyType2LP;
    private EditText type1Value;
    private LayoutParams type1ValueLP;
    private EditText type2Value;
    private LayoutParams type2ValueLP;
    private CustomTextWatcher fCustomTextWatcher = new CustomTextWatcher();
    private ArrayList<Double> rates = new ArrayList<Double>();
    private Context rContext;
    private int screenWidth;

    private ListView rPopularConversions;
    private long lastRefreshed;
    private TextView lastRefeshedView;

    private void addListeners() {
        currencyType1.setOnClickListener(new CustomOnClick());
        currencyType2.setOnClickListener(new CustomOnClick());
        addEditTextListeners();

    }

    private void addEditTextListeners() {
        type1Value.addTextChangedListener(fCustomTextWatcher);
        type2Value.addTextChangedListener(fCustomTextWatcher);
    }

    private void convert(int i) {
        removeAllListeners();

            if(i == 11 || i == 1 ){
                if(!Formatting.isEmptyOrNull(type2Value)){
                    double from = rates.get(getLocation(currencyType2));
                    double to = rates.get(getLocation(currencyType1));
                    Currency c = new Currency(from,to, Double.valueOf(type2Value.getText().toString()));
                    type1Value.setText(String.valueOf(Formatting.roundOff(c.getResult())));
                }

            }

            if(i == 22 || i==2 ){
                if(!Formatting.isEmptyOrNull(type1Value)){
                    double from = rates.get(getLocation(currencyType1));
                    double to = rates.get(getLocation(currencyType2));
                    Currency c = new Currency(from, to, Double.valueOf(type1Value.getText().toString()));
                    type2Value.setText(String.valueOf(Formatting.roundOff(c.getResult())));
                }
            }

            addEditTextListeners();
    }




    private int getLocation(TextView tv) {
        for (int i = 0; i < ConversionTypes.getCurrencyTypes().length; i++) {
            if(tv.getText().toString().equals(ConversionTypes.getCurrencyTypes()[i])){
                return i;
            }
        }
        return 0;
    }


    private void initialize() {



        currencyType1 = CustomObject.getCustomTextView(rContext, 1, Color.parseColor("#63879F"), Color.WHITE, 16, ConversionTypes.getCurrencyTypes()[121]);
        currencyType2 = CustomObject.getCustomTextView(rContext, 2, Color.parseColor("#63879F"), Color.WHITE, 16, ConversionTypes.getCurrencyTypes()[121]);
        type1Value = CustomObject.getCustomInputBox(rContext, 35, "1", "Enter value", new int[]{}, 11);
        type1Value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        type2Value  = CustomObject.getCustomInputBox(rContext, 35, "1", "Enter value", new int[]{}, 22);
        type2Value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

        int orientation = ((WindowManager) rContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
        if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180) {
            loadPotraitView();
        }
        else {
            loadLandscapeView();
        }





    }

    @Override
    public void loadLandscapeView() {
        lastRefreshed = rContext.getSharedPreferences("com.rokzin.converto_preferences", 0).getLong("LastRefreshed", 0);
        screenWidth = rContext.getResources().getDisplayMetrics().widthPixels;

        removeAllViews();

        rPopularConversions = new ListView(rContext);
        RelativeLayout.LayoutParams listLP = CustomObject.getCustomParams(screenWidth, LayoutParams.WRAP_CONTENT, new int[]{});
        listLP.addRule(RelativeLayout.BELOW, type2Value.getId());

        currencyType1LP = CustomObject.getCustomParams(screenWidth * 9/20, 120, new int[]{RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.ALIGN_PARENT_TOP});
        currencyType2LP = CustomObject.getCustomParams(screenWidth * 9/20, 120, new int[]{RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.ALIGN_PARENT_TOP});

        type1ValueLP = CustomObject.getCustomParams(screenWidth * 9/20, 120, new int[]{RelativeLayout.ALIGN_PARENT_LEFT});
        type2ValueLP = CustomObject.getCustomParams(screenWidth * 9/20, 120, new int[]{RelativeLayout.ALIGN_PARENT_RIGHT});

        type1ValueLP.addRule(RelativeLayout.BELOW, currencyType1.getId());
        type2ValueLP.addRule(RelativeLayout.BELOW, currencyType2.getId());

        this.addView(currencyType1, currencyType1LP);

            TextView equalTo = CustomObject.getCustomTextView(rContext, 111, Color.TRANSPARENT, Color.parseColor("#63879F"), 24, "=");
            RelativeLayout.LayoutParams params = CustomObject.getCustomParams(screenWidth*2/20, 240, new int[]{RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.CENTER_HORIZONTAL});
            equalTo.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
            this.addView(equalTo, params);

        this.addView(currencyType2, currencyType2LP);
        this.addView(type1Value,type1ValueLP);
        this.addView(type2Value,type2ValueLP);

        this.addView(rPopularConversions,listLP);
        removeAllListeners();
        addListeners();

    }


    @Override
    public void loadPotraitView() {
        lastRefreshed = rContext.getSharedPreferences("com.rokzin.converto_preferences", 0).getLong("LastRefreshed", 0);
        screenWidth = rContext.getResources().getDisplayMetrics().widthPixels;

        removeAllViews();

        rPopularConversions = new ListView(rContext);
        RelativeLayout.LayoutParams listLP = CustomObject.getCustomParams(screenWidth, LayoutParams.WRAP_CONTENT, new int[]{});
        listLP.addRule(RelativeLayout.BELOW, currencyType2.getId());

        currencyType1LP = CustomObject.getCustomParams(screenWidth * 1/3, 120, new int[]{RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.ALIGN_PARENT_TOP});
        currencyType2LP = CustomObject.getCustomParams(screenWidth * 1/3, 120, new int[]{});

        type1ValueLP = CustomObject.getCustomParams(screenWidth * 2/3, 120, new int[]{RelativeLayout.ALIGN_PARENT_TOP});
        type2ValueLP = CustomObject.getCustomParams(screenWidth * 2/3, 120, new int[]{});

        type1ValueLP.addRule(RelativeLayout.RIGHT_OF, currencyType1.getId());
        type2ValueLP.addRule(RelativeLayout.BELOW, currencyType1.getId());
        currencyType2LP.addRule(RelativeLayout.RIGHT_OF, type2Value.getId());
        currencyType2LP.addRule(RelativeLayout.BELOW, type1Value.getId());


        this.addView(currencyType1, currencyType1LP);
        this.addView(type1Value,type1ValueLP);
        this.addView(type2Value,type2ValueLP);
        this.addView(currencyType2, currencyType2LP);
        this.addView(rPopularConversions,listLP);
        removeAllListeners();
        addListeners();



    }


    public void removeAllListeners(){
        type1Value.removeTextChangedListener(fCustomTextWatcher);
        type2Value.removeTextChangedListener(fCustomTextWatcher);

    }
}
Was it helpful?

Solution

I think the problem is here:

if(s.toString().equals(type1Value.getText().toString())){
    ID=11;
}
else{
    ID=22;
}

Take in mind that both EditTexts are going through that method, so when type1Value is being edited, s.toString().equals(type1Value.getText().toString()) will be true.

I recommend you to use a TextWatcher for each EditText so you won't have to differentiate each one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top