onClickListener for the WHOLE screen

Yogi_Bear picture Yogi_Bear · Feb 10, 2014 · Viewed 12.1k times · Source

I am making an alarm clock. I want to make an activity which on the layout part is empty (exept a photo on the background) I want to do, that if i touch anywhere on the screen, the music will stop.

I thought about making the img as a imageview... but it dosent strach on the screen when I do it (even if the parameters are on the whole screen)

help?

Answer

Zied R. picture Zied R. · Feb 10, 2014

in your layout verify that :

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

then try to use onTouchListener

then try :

yourActivityLayout.setOnTouchListener(new View.OnTouchListener() {  
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
           // action to do
            return true;//always return true to consume event
        }
    });