Search code examples
javaandroidtouch-event

How to check if the screen hasn't been clicked in Android?


I know that if you want to check if someone clicked on something, you would do

if(event.getAction() == MotionEvent.ACTION_DOWN)

But I want to check if the screen hasn't been clicked at all. What is the code I would produce in order to do this?

Also, as a side note, would it be a good idea to have all the classes in an Android app extend the View classes?


Solution

  • Create a boolean variable called istouched and use onTouchLisener on your top most View actually its a Window that's where your Views are, quick sample

    getWindow().getDecorView().setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                    // i have been touched
                    istouched = true;
                    Toast.makeText(getBaseContext(), "you touched me?!! - i will tell my mom", Toast.LENGTH_SHORT).show();
                    return true;
                }
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    // you can implement this
                    Toast.makeText(getBaseContext(), "shhh; i am touching", Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });
    

    so the logic here is if istouched is false your device is not telling her mom, which means it has not being touched..

    hope it helps