I have a button like a switch where I am trying to setClickable(false) after I click it so that only the first click will be handled
(additional clicks are ignored in the case of accidental double-clicks/multiple-clicks).
Here is a similar code:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
//do other things
}
});
Then eventually, I have a code somewhere where I will reset the clickable to true, depending on a state variable, so I can switch-off.
The problem is when I click the button very quickly, it seems the succeeding clicks are still handled.
Is there a delay to the effects of setClickable()?
Also, I have read about using setEnabled(false) instead, but I cannot use it in my case. I need the button to still be enabled but not clickable.
Judging from your comment you probably need something like this.
Boolean SWITCH_ON = false;
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!SWITCH_ON ){
SWITCH_ON = true;
}
}
});
Button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
if(SWITCH_ON ){
// do your task for long click here ...SWITCH_ON
}
return true;
}
});
You can use button.setEnabled(false); within your onClick method to disable the button.
Disabled buttons don't trigger the onClick method, and you can easily re-enable it with button.setEnabled(true); when needed.
You could add another variable named buttonEnabled or so and initialize it with true. Then in the onclick do this:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
if(buttonEnabled) {
//do other things
}
buttonEnabled = false;
}
});
Note that you need to change the variable to if you want to reactivate it
Related
I have button with for inside his onclik. i Want stop 'for'or hide button with stopping method but i'm not able to do this.
I have tried with while and variable++ but it don't work with visability gone but it was hiding after 'for' completed.
public void siema(View view) {
for(i=2;i<5; i++){
some if's
}}
and what my problem is : if i click button 5 times it goes 5 times i want to block it or make method siema stop on second click of the button.
use this code that would help you
int count=1;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(count>1)
{
//do action
}else{
count++;
}
}
});
this will help you.
How to disable onClick when I click for LongClick?
It's a code from recyclerView, when I try long click I just see that normal click just spamming like hell.
holder.title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "To clear your recomendations, press for few seconds. ", Toast.LENGTH_SHORT).show();
}
});
holder.title.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
clearPreferences(R.string.preferences_reminder);
Toast.makeText(context, "Recomendations cleared.", Toast.LENGTH_SHORT).show();
return true;
}
});
In case if there is necessity to listen both onLongClick and onClick,
Here is another approach:
Example:
#Override
public boolean onLongClick(View view) {
//return value true to make sure only onLongClick is executed without triggering normal onClick
return true; // or false
}
return true means that the event is consumed. It is handled. No other click events will be notified.
return false means the event is not consumed. Any other click events will continue to receive notifications.
So to make sure both onClick and onLongClick are not triggered at the same time, return true from onLongClick event.
Add this line
holder.title.setOnClickListener(null);
I want to attach two click listener in a button and both onclicklistener should be different. Is there any way to do this.? Can't use function to call from first to second. When I use this then I got output from second one only. I want both output.
I am working on some screening task, so whenever a use click on a button it tell me that user clicked on this in Logcat and also button do its normal task.
First is :
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("First Click" , "Clicked on button 1");
}
});
Second is :
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v2) {
if (v2 instanceof Button) {
Log.i("User Clicked a checkbox with value ", " : " + ((Button) v2).getText().toString());
}
}
});
You can't do that. Setting your second OnClickListener removes the first one.
That's why the function is called setOnClickListener() instead of addOnClickListener()
As you say, you can call two functions from the onClick()
Edit:
On your edit, you say that you want to show a message when your button is clicked.
You can add the loggging functionality that you need before doing anything else on the event, simply doing
#Override
public void onClick(View v) {
// Log something
// Your functionality
}
Or you can create a class implementing View.OnClickListener
public class MyOnClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
// Log something
}
}
And then, on your Activity:
btn.setOnClickListener(new MyOnClickListener() {
#Override
public void onClick(View v) {
super.onClick(v);
// Your functionality
}
});
You can use function in OnClickListener like-
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
run1();
run2();
}
});
private void run1(){
//Your first functionality
}
private void run2(){
//Your second functionality
}
If I understood you correctly, you could do this:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dothings(); //what you are trying to achieve with this button click
showViewInLogCat(v); //to show you the view in the logcat
}
}
where showViewInLogCat() is a function that show you which view was clicked in your logcat:
public void showViewInLogCat(View view) {
if (view instanceof Button) {
Log.i("User Clicked a checkbox with value ", " : " + ((Button) view).getText().toString());
}
//and add the other if
}
You can call this function in every OnClick event on other views
Probably if you want to do something like this..!
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Log.i("First Click" , "Clicked on button 1");
// add a boolean here to check if you want to do the task or not.
doTask = true;
doGeneralTask(doTask); //to show you the view in the logcat
}
}
and in doGeneralTask(doTask) do something like this.
public void doGeneralTask(boolean doTask) {
if (doTask) {
// do whatever generalized tasks you need.
}
}
I don't know whether this question get minus points, but I searched every where and my last resort is stackoverflow.
I need to add five buttons to notification area in horizontally. And each button I need to add even listener. I know it is possible to do with RemoteViews. But I never seen anyone adding event listener to each element.
These are the references if anyone need to refer.
Notifications Documentation
How to create a custom notification on android
SlidingDrawer API
You can add 5 anonymous listeners, or a single named listener.
Anonymous:
Button b1 = new Button(...);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// first listener's code goes here
}
});
Button b2 = new Button(...);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// second listener's code goes here
}
});
...
named is much the same, but contains a switch statement to differentiate what happens:
View.OnClickListener myListener = new View.OnClickListener() {
public void onClick(View v) {
String buttonTitle = ((Button)v).getText();
if ("title1".equals(buttonTitle)) {
// do things for the first button's click
} else if ("title2".equals(buttonTitle)) {
// do things for the second button's click
}
...
}
});
...
I'm creating a dialog box and using the (this) isnt working. Up until now its just been a button calling a dialogbox but now the button within the called dialogbox needs to call another dialog. The Dialog dialogdelcon is the one with problem.
Here is the code:
case R.id.delappt:
//rmvall();
final Dialog dialogdelsel = new Dialog(this);
dialogdelsel.setContentView(R.layout.delsel);
dialogdelsel.setTitle("What would you like to do?");
dialogdelsel.setCancelable(true);
Button btndelsel = (Button) dialogdelsel.findViewById(R.id.btndelsel);
btndelsel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete selected code here.
}
});
Button btndelall = (Button) dialogdelsel.findViewById(R.id.btndelall);
btndelall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete all code here.
final Dialog dialogdelcon = new Dialog();
dialogdelcon.setContentView(R.layout.delcon);
dialogdelcon.setTitle("Deletion Confirmation");
dialogdelcon.setCancelable(true);
Button buttoncnclok = (Button) dialogdelcon.findViewById(R.id.btndelcon);
buttoncnclok.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
dialogdelcon.dismiss();
}
});
dialogdelcon.show();
}
});
dialogdelsel.show();
break;
getApplicationContext() or use YourActictyName.this Because this refers the button click listner ,not your class Object
If this code is in the onCreate() method, or similiar, add getApplicationContext() instead of this and you should be fine. That's because this in a Button-context will refer to the button environment.
To improve the isolation between the two dialogs, it would be best to call showDialog(R.id.delapptcon) from the onClick handler. Then load the new dialog in the onCreateDialog of your activity. In this way, you can create more reusable dialogs and avoid the scoping issue you have now.