Search code examples
screenonclicklistener

Click on the whole screen


I have a project where I need to set a click listener on the whole screen (when screen is clicked on I collect the values of the accelerometer).

I only see everywhere that I need to set a button (isn't it possible without a button?) which can be transparent and with the same dimension as the screen. Is there any other possibility or do the OnClickListener only works when a button is used?

I hope I've been understandable enough.


Solution

  • Here is what I did:

    1) In my xml view code, I checked the name of the Layout wich includes all the others "ConstraintLayout"

    <android.support.constraint.ConstraintLayout
    

    xmlns:android="http://schemas.android.com/apk/res/android"

    //Other layouts

    >

    2) And then I add this in my activity after implementing View.OnTouchListener :

    ((ConstraintLayout) findViewById(R.id.layout_main)).setOnTouchListener(this);

    @Override

    public boolean onTouch(View view, MotionEvent motionEvent) {

    //DO THINGS
    
    
    return false;
    

    }