Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClickListener for the WHOLE screen

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?

like image 843
Yogi_Bear Avatar asked Feb 10 '14 00:02

Yogi_Bear


People also ask

How do I use OnClickListener?

Creating Anonymous View.OnClickListenerLink the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter.

What is view OnClickListener?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

What is the purpose of setOnClickListener?

One of the most usable methods in android is setOnClickListener method which helps us to link a listener with certain attributes. While invoking this method a callback function will run. One can also create a class for more than one listener, so this can lead you to code reusability.


2 Answers

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
        }
    });
like image 186
Zied R. Avatar answered Sep 27 '22 01:09

Zied R.


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;
        }
    }

}
like image 45
Biraj Zalavadia Avatar answered Sep 23 '22 01:09

Biraj Zalavadia