1

I've TextView and EditText in my layout. TextView represents price and EditText represents quantity. When user enters some number in EditText i want to multiply price by that entered number and display it on my TextView. But i'm not able to achieve this.Below is my source code.

        mEditTextQuantity = (EditText) rootView.findViewById(R.id.quantity_edit_text);
        mEditTextQuantity.setFilters(new InputFilter[]
        { new Ipfilters("1", "999") });
        mEditTextQuantity.setText("1");
        int position = mEditTextQuantity.length();
        Editable etext = mEditTextQuantity.getText();
        Selection.setSelection(etext, position);
        Utility.showKeypad(mEditTextQuantity, getActivity());
    mEditTextQuantity.setOnTouchListener(new OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                mEditTextQuantity.requestFocus();
                Utility.showKeypad(mEditTextQuantity, getActivity());
                int mtemp;
                mtemp=Integer.parseInt(mEditTextQuantity.getText().toString().trim());  
                NumberFormat nf = NumberFormat.getInstance();
                nf.setMinimumFractionDigits(2);
                double price = Double.parseDouble(mProductPriceTextView.getTag().toString());
                price = price * mtemp;
                mProductPriceTextView.setText(mCurrency + nf.format(price));
                return true; 
            }
        }); 

I also understand that we can achieve it through onKeyPreIme method of edit text but not able to do so. Thanks in advance.

Amrut
  • 2,655
  • 3
  • 24
  • 44

3 Answers3

2

Why not try using a textChangedListener as described here? You can see what the text was before and after it was changed and modify your TextView based on the result.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46
1

You may be looking for that...

edtText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,  int count) {
          int   newPrice = price*Integer.parseInt(edtText.getText().toString());

            mProductPriceTextView.setText(""+newPrice);

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    });
mdDroid
  • 3,135
  • 2
  • 22
  • 34
0

First suggestion: Don't use the OnTouchListener; it will respond to any and all touch events that interact with the edit text.

I'm not immediately seeing anything in your code that would fail, but maybe see if it will work using a different trigger. This code should respond to the user hitting the "done" or "enter" key.

mEditTextQuantity.setOnEditorActionListener(new OnEditorActionListener(){
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
        if(actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
            int mtemp;
            mtemp=Integer.parseInt(mEditTextQuantity.getText().toString().trim());  
            NumberFormat nf = NumberFormat.getInstance();
            nf.setMinimumFractionDigits(2);
            double price = Double.parseDouble(mProductPriceTextView.getTag().toString());
            price = price * mtemp;
            mProductPriceTextView.setText(mCurrency + nf.format(price));
            return true; 
        }
        else
            return false;
    }//onEditorAction()
}

To get a better idea of what you're looking for, can you provide any error logs or anything that might tell us what's going wrong?

Stspurg
  • 155
  • 1
  • 13