Umair Iftikhar
Umair Iftikhar

Reputation: 3

I want to keep increasing/decreasing Counters on Long Click on "+" or "-" Button.... Android setOnLongClickListener

I was Able to increment/decrement on respective "-" or "-" Button Click with help of setOnClickListener. But I want to KEEP INCREASING/DECREASING respect counter if i keep Preasing(Long Click) a "+" or "-" Button. Just Like in Maps, When we touch a Zoom in Button and held our finder on it, the map keeps Zooming in until we remove our finger.

I tried doing it with setOnLongClickListener and setOnTouchListener, Searched stackoverflow forums but couldn't find help.

Here is Complete Java Code of my Android Activity.

public class MainActivity extends Activity {

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

    final TextView tv_1= (TextView) findViewById(R.id.tv_1);
    Button btn_1= (Button) findViewById(R.id.btn_1);
    Button btn_2= (Button) findViewById(R.id.btn_2);

    final TextView tv_2= (TextView) findViewById(R.id.tv_2);
    Button btn_3= (Button) findViewById(R.id.btn_3);
    Button btn_4= (Button) findViewById(R.id.btn_4);

    final TextView tv_3= (TextView) findViewById(R.id.tv_3);
    Button btn_5= (Button) findViewById(R.id.btn_5);
    Button btn_6= (Button) findViewById(R.id.btn_6);

    Button btn_7= (Button) findViewById(R.id.btn_7);


    btn_1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_1.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt++;
        tv_1.setText(""+cnt);
        }
    });
    btn_2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_1.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt--;
        tv_1.setText(""+cnt);
        }
    });



    btn_3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_2.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt++;
        tv_2.setText(""+cnt);
        }
    });
    btn_4.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_2.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt--;
        tv_2.setText(""+cnt);
        }
    });


    btn_5.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_3.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt++;
        tv_3.setText(""+cnt);
        }
    });
    btn_6.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_3.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt--;
        tv_3.setText(""+cnt);
        }
    });




    btn_7.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv_1.setText(""+0);
            tv_2.setText(""+0);
            tv_3.setText(""+0); 
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Upvotes: 0

Views: 2723

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

I suggest you to use OnTouchListener. We invoke new Thread that will run till you press on the button in while loop:

public class MainActivity extends Activity implements OnTouchListener {

...
btn_1.setOnTouchListener(this);
...

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
    if (arg1.getAction()==MotionEvent.ACTION_DOWN){
        
        doPressDown();          
    }
    else if (arg1.getAction()==MotionEvent.ACTION_UP){
        
        doPressRelease();
    }

    return true;
}  

private void doPressDown() {
    isPressed = true;
    new Thread(() -> {
        while (isPressed == true) {
            // here we increment till user release the button
        }
    }).start();
}

private void doPressRelease(){
    isPressed = false;
}

}

Upvotes: 3

Related Questions