How can i detect a long press on the whole activity? since onLongClickListener is only for individual views.I want to run a method everytime the user longpress the screen
You can override your activity's dispatchTouchEvent() method. You also need a gesture detector in order to determine which motion events are 'long presses'. Put this into your activity:
final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
// The code for when a long-press happens
}
});
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.dispatchTouchEvent(event);
}
Please note that I did not test the above code.
A long press is actually multiple registers of the key. So you could do a while loop on the input and as long as input is not NULL run the method you want. If I understood you correctly this should do the trick...
Related
I was thinking of creating a button that when tapped can disable the phone back button. I wanted to be able to enable it back again by pressing another button. However, the way that I found to disable the back button was with an override. Could somebody lend me a hand on how I could do that? Thanks!
What I tried was to put the override inside the button listener and onClick method, but it highland the override in red. I then tried putting the override in a different class and then calling the class when the the button is tapped.
I figured it out, but for anyone wondering the same thing, I'll post what I did. I implemented the following code inside MainActivity.class with the buttons:
private boolean backButtonEnabled = true;
#Override
public void onBackPressed() {
if (backButtonEnabled) {
super.onBackPressed();
}
}
public void disableBackButton() {
backButtonEnabled = false;
}
public void enableBackButton() {
backButtonEnabled = true;
}
I have a Button and when I click it it plays a Sound. How to use longpress to turn sound ( on and off ), so basically first tap should play a sound, second tap should stop it.
MainActivity
You can use onLongClickListener:
Button button;
button = findViewById(R.id.<your_button_id>);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
//your code goes here
return false;
}
});
You have to add onLongClickListener to your button and implement the method onLongClick in your main activity.
for example:
public class MainActivity implements View.OnLongClickListener
after you implement the onLongClickListener you override the function onLongCLick
#Override
public boolean onLongClick(View view) {
return false;
}
And finally you need to set onLongClickListener to your button
btn.setOnLongClickListener(this);
In order to make the sound on and of just hold a global boolean variable which is called
private boolean isPlaying;
When it is long pressed once you set it to true, and when it is called again set it to false.
and stop your sound.
When returning to an activity using the back button and
startActivity(new Intent(this, ActivityMainMenu.class));
is called, are there any methods that automatically go to a custom view?
I've noticed that when going back to the view, it's no longer invalidated.
Basically without using the activity's onResume I want to be able to resume my custom view.
For anyone else who wants to know you can use:
protected void onAttachedToWindow()
It's called every time a view is attached to the Window.
In the Android Source for TextView, it posts a Runnable.
if (ss.error != null) {
final CharSequence error = ss.error;
// Display the error later, after the first layout pass
post(new Runnable() {
public void run() {
setError(error);
}
});
}
I have a click handler method defined in a class. I am trying to call a particular method if a cancel button on the screen is clicked.
here's the code snippet -
boolean tempCheck;
#Override
protected void onBind() {
super.onBind();
...
...
getViewName().getVar().addClickHandler(new ClickHandler() {
#Override
public void onClick(final ClickEvent event) {
//Doing some operation and making tempCheck true
tempCheck = true;
}
});
If(tempCheck){
this.box.getButtonName().addClickHandler(new ClickHandler() {
#Override
public void onClick(final ClickEvent event) {
this.box.hide();
this.getViewName().hide();
}
});
} else {
this.callToMethodA();
}
}
When the button gets clicked, tempCheck boolean variable becomes true.
tempCheck = true;
but if condition is getting failed, it always go into else part.
If(tempCheck)
why this is happening? is this because of how java managed closures?
Note : If condition has to be outside the block as this.callToMethodA() is used by other click handlers too.
When you call addClickHandler(), it creates the handler and immediately continues with the next statement (which is If(tempCheck) in this case). It does not wait for the button to be clicked, as you seem to assume. So tempCheck will always be false at this point.
Any code you want executed after the button is clicked has to go inside the onClick() method, or inside a method that you call from onClick().
Your way of thinking about Event Handling is wrong.....You have to write the functionality "in side the onClick() itself,whichever you want to execute when you click the close button.
But here what you are doing is just changing one variable value...you are not doing the functionality you want to do inside the onClick()
I hope u understand....Even if u didn't understand this....think once...you can come to know your silly mistake...
I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. I've been doing research for hours, and I cannot find any methods to accomplish this. I've tried running threads, which often crash or seemingly don't execute. I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. I'm currently using a while loop, but this just freezes the program. I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. Am I on the right track or am I way off in left field? Here is a snippet of code:
rightButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
while(arg0.isPressed())
{
mover.updateCoordinates(1, 0);
}
}
});
Would you try this another method?
Firstly declare your button as class variable, declare a Handler and a Runnable:
private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
if(rightButton.isPressed())
{
// If press state is pressed, move your item and recall the runnable in 100 milliseconds.
mover.updateCoordinates(1, 0);
mHandler.postDelayed(mRunnable, 100);
}
}
};
Then your button's onClickListener will looks like this:
rightButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
// Instead of performing a loop here, just call a runnable, do simple press state checking there.
mHandler.postDelayed(mRunnable, 100);
}
});
Create a loop that updates the views, waits, and calls itself after it finishes waiting. Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. Have the onClick method toggle the boolean field's value.
Try:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// Start your animation here
} else if (e.getAction() == MotionEvent.ACTION_UP) {
// Stop your animation here
}
return false;
}
});
References:
MotionEvent
OnTouchListener
Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent