0

I have a Button that whenever the user clicks it, it increases the number in my EditText, but i want to have an onTouch method that when the user keeps their finger on the button the number increases with more speed(continuously), something like the backspace, that when you click it, it deleted just one letter and when you keep it down, it deletes the words faster, so i have both ontouch and onclick in my code, but whenever i click the button only onTouch triggers, what should i do? can anybody help, please? I have searched a lot but couldn't find anything that helps me

Here is part of my code

     btnFineUp.setOnTouchListener (this);   
            btnFineUp.setOnClickListener (this);

    @override
            public void onClick (View view)
        {
            switch (view.getId ())
            {
                case R.id.btn_fine_up:

                {
                    View     v = findViewById (getCurrentFocus ().getId ());
                    EditText editText;

                    if (v instanceof EditText)
                    {
                        editText = (EditText) v;
                    }
                    else
                    {
                        return;
                    }
                    int calcStateAmp   = +1;

                     if (view.getId () == R.id.btn_fine_up)
                    {
                        calcState_2 = upAndDown.getFineUpAmp () * calcStateAmp;

                        calcStateAmp = calcState_2;

                    }
                    double currentValue;
                    if (StringUtils.isNullOrEmpty (editText.getText ().toString ()))
                    {
                        currentValue = 0;
                    }
                    else
                    {
                        currentValue = Double.parseDouble (editText.getText ().toString ());
                    }
                    switch (getCurrentFocus ().getId ())
                    {
                        case R.id.txt_va1_a:
                        case R.id.txt_va2_a:
                        case R.id.txt_va3_a:
                        case R.id.txt_vb1_a:

                            editText.setText (String.format (Locale.ENGLISH, "%.2f", currentValue + (0.01 * calcStateAmp)));
                            break;
    }


        @Override
        public boolean onTouch (View view, MotionEvent motionEvent)
        {
                switch (view.getId ())
                {
                    case ir.vebko.R.id.btn_fine_up:

                    {
                        View     v = findViewById (getCurrentFocus ().getId ());
                        EditText editText;

                        if (v instanceof EditText)
                        {
                            editText = (EditText) v;
                        }
                        else
                        {
                            return false;
                        }
                        int calcStateAmp   = +1;



             if (view.getId () == R.id.btn_fine_up)
                        {
                            calcStateAmp = calcState_2;

                        }
                        double currentValue;
                        if (StringUtils.isNullOrEmpty (editText.getText ().toString ()))
                        {
                            currentValue = 0;
                        }
                        else
                        {
                            currentValue = Double.parseDouble (editText.getText ().toString ());
                        }

                        switch (getCurrentFocus ().getId ())
                        {
                            case ir.vebko.R.id.txt_va1_a:
                            case ir.vebko.R.id.txt_va2_a:
                            case ir.vebko.R.id.txt_va3_a:
                            case ir.vebko.R.id.txt_vb1_a:

                                editText.setText (String.format (Locale.ENGLISH, "%.2f", currentValue + (0.01 * calcStateAmp)));
                                break;
    }
return true;
}
Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
  • Return `false` may be it will help you [Use both onTouch and onClick](https://stackoverflow.com/questions/19538747/how-to-use-both-ontouch-and-onclick-for-an-imagebutton) – Mohammad Arman Jul 08 '17 at 05:50

1 Answers1

0

OnTouch itself handles the click user action for OnClick as well. Thus, a click user action raises an OnTouch event first; only when there is no listener listening to OnTouch does it invoke the OnClick event. Since you have an OnTouchListener associated with the OnTouch event, there is no way your OnClickListener will be invoked. For more details, look at this question.

From your question, it seems that you can skip View.OnTouchListener altogether and instead implement both View.OnLongClickListener and View.OnClickListener.

Hope that helps.

Rajan Prasad
  • 1,582
  • 1
  • 16
  • 33