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?
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.
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.
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.
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
}
});
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With