0

I am trying to create a max bench press calculator for a project for school. I am very new to coding and can not figure out why this is not working. The plus and menus weight buttons never work. I did have it working before with an OnClickListener and OnLongClickListener. I just want the OnTouchListeners to keep adding weight as long as the button is held down.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity implements OnClickListener {

private Button plusreps;
private Button menusreps;
private Button Calculate;
private Button plus;
private Button menus;
private TextView textView;
private TextView textView2;
private TextView textView3;
int reps;
int Maxint;
double Max;
double weight;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);
    textView2 = (TextView) findViewById(R.id.textView2);
    textView3 = (TextView) findViewById(R.id.textView3);

    plusreps = (Button) findViewById(R.id.button5);
    menusreps = (Button) findViewById(R.id.button4);
    menus = (Button) findViewById(R.id.button3);
    plus = (Button) findViewById(R.id.button2);
    Calculate = (Button) findViewById(R.id.button);

    Calculate.setOnClickListener(this);

    reps = 0;
    weight = 100;
    textView.setText("" + String.valueOf(weight));
    textView2.setText("" + String.valueOf(reps));

    plus.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent mEvent1) {
            if (mEvent1.getAction() == MotionEvent.ACTION_DOWN)
                startIncreasingWeight();
            else if (mEvent1.getAction() == MotionEvent.ACTION_UP)
                stopIncreasingWeight();
            return false;
        }

        private void stopIncreasingWeight() {
            textView.setText("" + String.valueOf(weight));
        }

        private void startIncreasingWeight() {
            weight = weight++;
            textView.setText("" + String.valueOf(weight));

        }
    });


    menus.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent mEvent) {
            if (mEvent.getAction() == MotionEvent.ACTION_DOWN)
                startDecreasingWeight();
            else if (mEvent.getAction() == MotionEvent.ACTION_UP)
                stopDecreasingWeight();
            return false;
        }

        private void stopDecreasingWeight() {
            textView.setText("" + String.valueOf(weight));
        }

        private void startDecreasingWeight() {
            weight = weight--;
            textView.setText("" + String.valueOf(weight));

        }
    });
}



public void onClick(View v) {
  switch (v.getId()) {
    case R.id.button4:
        if (reps > 1){
            textView2.setText("" + String.valueOf(reps - 1));
            reps = reps - 1;
        }
        else {
            textView2.setText("" + String.valueOf(reps));
        }
        break;
    case R.id.button5:
        textView2.setText("" + String.valueOf(reps + 1));
        reps = reps + 1;
        break;
    case R.id.button:
        Max = reps * weight * 0.0333 + weight;
        int Maxint = (int) Math.round(Max);
        textView3.setText("Your max is: " + String.valueOf(Maxint));
        break;

}





}}
halfer
  • 19,824
  • 17
  • 99
  • 186
LUKER
  • 540
  • 1
  • 5
  • 23
  • onTouch is no different than onClick except that it provides more Action Like ACTION_UP, ACTION_DOWN, ACTION_MOVE. What i want to say is that the code inside MotionEvent.Action_Down still excecutes only once. – Qandeel Abbassi Feb 22 '16 at 18:43
  • 1
    See this [question](http://stackoverflow.com/questions/10511423/android-repeat-action-on-pressing-and-holding-a-button) for your issue – Qandeel Abbassi Feb 22 '16 at 19:14

1 Answers1

1

put this:

plusreps.setOnClickListener(this);
menusreps.setOnClickListener(this);
Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33