9

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?

Yogi_Bear
  • 512
  • 2
  • 10
  • 24

7 Answers7

10

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
        }
    });
Zied R.
  • 4,964
  • 2
  • 36
  • 67
  • That does't work! Instead ViewTreeObserver work 100% refer to the answer https://stackoverflow.com/a/18000128/11557093 – Harsh0021 Dec 30 '20 at 16:59
2

Do this way to put touch event on Whole Activity

public class MainActivity extends Activity {

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

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_UP) {

            // do your work here
            return true;
        } else {
            return false;
        }
    }

}
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
1

Have your Activity or Fragment implement OnClickListener, and then assign it as your click listener to every view and or layout.

in the first line of the function just run some logic,

@override
public void onClick(view v)
{
   if(isMusicPlaying)
      stopMusic();

   // here run the rest of your logic
   if (v == someButton){}

}
WIllJBD
  • 6,144
  • 3
  • 34
  • 44
1

If you want if touch the layout , set click listener to the layout it self

mohammed momn
  • 3,230
  • 1
  • 20
  • 16
0

You may play with WindowManager and overlaying your layout over everything else(may also overlay status bar and other system UI)

WindowManager instance has addVieW() method. With right layout params it produces described result

Sam
  • 1,652
  • 17
  • 25
0

Try this, Pass your topmost parent as argument for this method

Eg: stopMusicOnTouch(yourParentView);

    public void stopMusicOnTouch(View view) {

    //Set up touch listener for non-text box views to hide keyboard.
    if(!(view instanceof ImageView)) {

        view.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // Stop music here
                return false;
            }

        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {

        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

            View innerView = ((ViewGroup) view).getChildAt(i);

            stopMusicOnTouch(innerView);
        }
    }
}
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
-1

At the end,I made a button for the whole screen and did it transperent (but visible)

it worked perefectly, and I suggest it to others!

Yogi_Bear
  • 512
  • 2
  • 10
  • 24