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;
}
Related
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.
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...
I have a main activity , and a popup.xml file that is included in the activity
the problem is when i press the back button , it closes the app directly , whether the popup is opened or not
i got the idea to override the onClick method, add a boolean that will be true when the popup is opened , and false otherwise , then add this condition in the onClick method
i'm still a noob with Android Studio , would anyone please guide me through ?
Thank you.
override the onBackPressed in your activity and check if popup is showing. if popup is showing then close popup else do general back press action
#Override
public void onBackPressed() {
if(popupWindow.isShowing())
popupWindow.dismiss();
else
super.onBackPressed();
}
Just override the following method in your Activity:
#Override
public void onBackPressed()
{
//Do whatever you want before the back button should trigger
super.onBackPressed(); // call this only if you want to close the app
}
#Override
public void onBackPressed() {
if(!(Activity).isFinishing){
//activity is not yet finished
}else{
//activity finishes
super.onBackPressed();
}
}
When you click button in my app if you are fast enough before the screen/popup loads it loads them multiple times. I know how to disable the click on the button but that's not an option, because when you close the popup or return to the previous screen the button is disabled. I tried with Handler and Runnable to wait for 1s before the button is active again but this solution is not optimal in case if the OS needs more time to open the next screen. So I am searching for the most optimal solution. Any ideas?
Edit: setClickable(false) and then setting it back to true doesn't work because it loads my screen/popup slower than expected the button is enabled again and it opens the screen/popup multiple times again.
You can disable the multiple click at the same time using the following code
private boolean isClicked;
#Override
public void onClick(final View v) {
if(isClicked) {
return;
}
isClicked = true;
v.postDelayed(new Runnable() {
#Override
public void run() {
isClicked = false;
}
}, 1000);
}
Implement logic in your onClick to determine whether you want to ignore the click.
You can disable the button. When you close the popup enable the button and when the popup is visible make it disable. Keep listening the actions for popup and when the user get back to the previous screen.
Maintain one variable on button onClick listener and change the value to determine when you want to click button..
You can stop multiple operations by this way.
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick()
{
performOperation();
}
});
public void performOperation()
{
static boolean working = true;
if(working)
{
return;
}
working = true;
//Do you work here;
working = false;
}
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...