Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button android while pressed

I want to print something while my button is pressed (not after it is released). At moment I have this, but it only works once...

button.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            System.out.println("pressed");
            return true;
        }
        return false;
    }
});
like image 436
user1477414 Avatar asked Nov 30 '25 04:11

user1477414


1 Answers

System.out doesn't work in an Android Device (won't show you anything on the device) and if you have a text view you can set the text on your MotionEvent.ACTION_DOWN as you are already doing and on the MotionEvent.ACTION_UP you set your text of your text view to empty. Something like this:

public class TestActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        final TextView textView = (TextView) findViewById(R.id.textview);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    textView.setText("Button Pressed");
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    textView.setText(""); //finger was lifted
                }
                return true;
            }

        });
    }

}
like image 71
Raykud Avatar answered Dec 01 '25 17:12

Raykud