How to disable multiple clicks on button in Android - java

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

Related

How to override a method when a button is tapped?

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

Can't re-enable a view after disabling it with CheckBox

I'm trying to enable/disable a seekbar when a checkbox is checked(or not).
I'm using this to create and refer:
CheckBox checkBoxProva = (CheckBox) findViewById(R.id.checkbox_prova);
boolean varCheckBoxProva = checkBoxProva.isChecked();
And this simple if to disable/enable the view:
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}
All this is inside the onCreate.
When the app starts the seekbar is disabled (so the if works), but if I check the CheckBox it doesn't change to enabled.
EDIT: I've succed thank to the reply of #rohan bhatia.
You are doing all of this in your onCreate method that only gets called when the activity is first created. You need to setup a listener that will be fired when the checkbox is clicked. See: https://developer.android.com/guide/topics/ui/controls/checkbox.html
You need a Listener that notifies when a checkbox is selected or deselected..
checkBoxProva.setOnCheckedChangeListener(new new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
seekBarProva.setEnabled(isChecked);
}
}
);
Accordingly, whenever you click the checkbox the method onCheckedChanged() will be executed.. You will still need to specify this in onCreate -
seekBarProva.setEnabled(checkBoxProva.isChecked());
As onCheckedChanged() only listens for changes in checked state, so to specify initial stage you will need that above line too.
Simply insert an else block
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}else{
seekBarProva.setEnabled(true);
}
Alternative solution:
Personally, I'd recommend adding a listener to the checkbox inside your onCreate method.
checkBoxProva.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(varCheckBoxProva){
seekBarProva.setEnabled(true);
}else{
seekBarProva.setEnabled(false);
}
}
});

How can reuse onClickListener in Android

I'm having one activity containing many buttons and each button performs different task. Since, I'm using push notifications to perform separate task every time for different notification, What is the best way to simulate the onClick Event.
One way I thought is using function. Writing separate function and calling it in onClickListener. Is there any other way to achieve the objective?
btnengg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (netconnect(null)==1)
{
w.loadUrl("myurl1");
wtshare.setVisibility(View.VISIBLE);
}
else
{
mInterstitialAd.show();
pbb.setVisibility(View.VISIBLE);
fadView.setVisibility(View.INVISIBLE);
w.loadUrl("myurl2");
requestNewInterstitial();
}}
});
Assign OnClickListener to a field and set it as click listener and based on button Id perform relative action
OnClickListener buttonClickListener = button -> {
switch (button.getId()) {
case R.id.button1:
break;
}
};
button.setOnClickListener(buttonClickListener);
In case you are asking how to initiate click action programmatically, you can
button.performClick();

How do I delay a button performing an action by a certain timeframe, if the button wasn't pressed again in that timeframe?

Sorry for the terrible title, I am bad at describing these things.
I am building a metronome and have a (-) UI button that decreases the tempo by 1, and a (+) UI button that increases the tempo by 1.
My problem currently is that whenever I press either buttons, the metronome restarts itself since there's a new tempo, and plays immediately. So if you press the (-) button 10 times in a row, each time you press it you hear the initial metronome "beep".
I would like my app to do the following:
When the user clicks either (-) or (+) buttons, wait for 200 milliseconds
IF the user didn't click the buttons again in that timeframe, play the metronome
If the user DID click the button again, don't play the metronome, repeat the process: wait 200 milliseconds, if no click was made play the metronome, etc
The end result would be that if I'm at 100 bpm and I repeatedly press the (+) button 20 times until I am at 120 bpm, the metronome wouldn't start playing until I am done tapping.
How do I go about implementing this? Thank you!
Declare and instantiate the below in your activity:
private Handler timeoutHandler = new Handler();
private Runnable delayStartThread = new Runnable() {
public void run() {
startMetronome();
}
};
Then insert the below code block in your onClickListener for both + and - buttons:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timeoutHandler.removeCallbacks(delayStartThread);
tempoOfMetronome++; //tempoOfMetronome--; for decrease button
stopMetronome();
timeoutHandler.postDelayed(delayStartThread, 200);
}
});
For more details on how the code works, refer the below links for examples (I used these examples to formulate the answer):
Android: clicking TWICE the back button to exit activity - How to use handler.postDelayed()
How to cancel handler.postDelayed? - How to cancel handler.postDelayed()
You should also look at the Android documentation for those methods.
If you want a delay between the action and the effect, there are several ways you can achieve it. This is one.
private boolean pressedAction = false;
#override
public void onClick(View v) {
if (pressedAction) return;
pressedAction = true;
new Thread(new Runnable(
#override
public void run() {
try {
Thread.sleep(200); // 200 miliseconds
} catch (Exception e) {}
// Update views or do work (program logic)
pressedAction = false;
}
}
}
Then, the metronome logic is your bussiness.

GWT clickHandler - Condition failing

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...

Categories