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...
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 want to make something happen on text selection, which is OnLongClickListener, but inside of that I need to get selected text, which is handled by default OnLongClickListener (at least I think it is).
Actual result, by adding just my listener, is that my method is called, I'm trying to get indices of selection bounds, but these are 0. I can also see in debugger, that no text is selected in that momment.
Code:
textView.setTextIsSelectable(true);
textView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
int start = textView.getSelectionStart();
int end = textView.getSelectionEnd();
// the rest of code
}
}
}
Question: How can I preserve default listener, which will be called first and make selection and then call my function.
I think you misunderstand how listeners work. They don't replace standard behavior, so there is no default listener to invoke to make sure something happens.
In this case, most likely your OnLongClick listener is simply being called before the TextView has actually updated its selection. In which case, you can try having your listener delay its processing until after the selection is set. Look at using Handler.postDelayed() or AsyncTask for that purpose. For example:
textView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
new Handler().post(() -> {/*your code here*/});
}
}
}
But, that being said, OnLongClickListener is not the correct listener to use for text selection changes. You need an ActionMode Callback instead:
How to set up a listener on the selected text in TextView
I want to know how can I stop message sending in a loop.
When I press stop, messages are still sent.
I have provided a stop() method.
for (i = 0; i < copy; i++) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(stop!=1)
sms.sendTextMessage(number,null,message,sentPIn,pdelint);
}
}, 25000);
stop method
int stop=0;
public void st(View view){
Button b=(Button)findViewById(R.id.button2);
b.setText("Stopped");
stop=1;
}
Modify St function to this
public void st() {
Button b = (Button) findViewById(R.id.button);
#Override
b.setOnClickListener(new View.OnClickListener() {
b.setText("Stopped!");
stop=1;
});
}
The problem was u were not using an listener to detect button press. It is not advisable to change the button text use a text view instead!
Most likely he has set onClick attribute in the xml, although only he'll be able to clarify that.
I'm saying that because I see View as a parameter in the method st(View v).
Although if the attribute onClick is not set in xml #Audi 's solution would be good. Though on the contrary if the attribute has been set in the xml then
you should check if view.getId() == R.id.button then apply a button cast to your view and set its text to "Stopped".
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 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