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.
Related
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
I have a view (button) and am listening to a click event, but in my while loop it won't call the onClick method:
public class Activity_Main extends Activity implements OnClickListener {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.btn_1);
button1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_1:
//do something make boolean false for example
break;
}
}
while(boolean == true) {
//it's not reacting on the click
}
Is there something I am missing?
You are blocking the main thread with that infinite while loop:
while(boolean == true) {
//it's not reacting on the click
}
and it cant execute any other code. You should be more worried about the ANR coming next rather then the click not responding. Put the while loop in a separate thread as suggested and dont overload the main thread's work with excessive tasks.
If you use Handlers and Messages you can start and stop the recording when the user clicks the button. And to check if the button has been clicked, just use onClick method.
Any experts here are able to help me, i wanted to change the number in edittext, add or minus the number inside directly once i click on the buttons. But there are error once i clicked on the buttons, how do i change the current UI directly without refreshing the page by clicking on the button?
for(int x=0; x<itemAmt; x++){
final int f=x;
btn[0][x].setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
int num=Integer.parseInt(et[f].getText().toString());
et[f].setText(num+1);
}
});
btn[1][x].setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
int num=Integer.parseInt(et[f].getText().toString());
et[f].setText(num-1);
}
});
}
Try to use these methods:
int num=Integer.valueOf(et[f].getText().toString());
et[f].setText(Integer.toString(num+1));
and
int num=Integer.valueOf(et[f].getText().toString());
et[f].setText(Integer.toString(num-1));
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
}
...
}
});
...
hi i am a new developer. i am trying to design an app. In my app i want to calculate the no of touches in a particular button. Is this can be calculated by onTouch process if yes can anyone give me an example or idea.
Try below code
First Create an Global variable
int numberOfClick = 0;
Now for your button try following code
clickButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
numberOfClick++;
}
}
now you can get the number of clicks by this variable
A click on a button is sent to the app via the onClick event. So if you have a Button:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(myClickListener);
You can set up your onClickListener to do whatever you want when the button is clicked.
// Create an anonymous implementation of OnClickListener
private OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
// increment the counter on click
numberOfClicks++;
}
};