I want to be able to press a button on my program and hold it down (without releasing) to increment a variable. Problem I am having right now is when I conduct the long button press it only runs once, until I release and press again.
First I want to find out if there is a way to do this without having to use the OnTouchListener, and just using the OnLongClick. Is there a way to check the value of the button? For example.. buttondown=true; Conduct a whileloop to increment until the button is released.
Second, I don't want the updates to be delayed, because the incremented value is being drawn as the user holds down the button.
Basically I am doing something like this:
btn_resume.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
..code..
return true;
}
});
OnLongClick will only be called once per press. It isn't going to work for your purpose.
If I understood your question correct this can be achieved using a OnLongClickListener.
Check http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)
OnTouchListener provides a more granular handling of touch events, e.g. KeyDown, KeyUp
I think you can use OnLongClickListener for increment/decrement. But once the long press is done for the button, the longpress has to be canceled or reset for the next long press of the same button.
Related
I wonder that if I have two buttons, and every time I press the first button I call the method secondButton.setOnClickListener(new OnClickListener). My question is when I set the new listener for the second button every time I press the first button, what happens to the old listener is it still in the memory ?
Activity owns button, button has a listener that is owned by Activity, if both can't be accessed from root, then theoretically they should be GCed.
there is a better way.
secondButton.performClick();
https://developer.android.com/reference/android/view/View.html#performClick()
Hope it helps.
What is the equivalent of GetKey for buttons. In my android game when user press and holds the button , i want player to move right but it only moves for one time when i press the button.But when i get input from a keyboard with GetKey function , it moves until i stop holding that button. I want to use my button like GetKey.I tried to add a event trigger pointer down component into my button but it is not working.What should i do?
public void moveRight(){
rigidbody2D.velocity = new Vector2(moveSpeed,rigidbody2D.velocity.y);
}
void Update () {
if(Input.GetKeyDown (KeyCode.Space)){
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHigh);
}
if (Input.GetKey (KeyCode.A)) {
rigidbody2D.velocity = new Vector2(-moveSpeed,rigidbody2D.velocity.y);
}
if (Input.GetKey (KeyCode.D)) {
rigidbody2D.velocity = new Vector2(moveSpeed,rigidbody2D.velocity.y);
}
}
they are absolutely same but i don't understand why its not working continously.
Update method is the key, i know this but i don't know how to get information about is a button pressed in Update method.Stuck help.
Well, we have two different parts of Unity3D API here.
Let's split it up into pieces:
Input.GetKey
As stated in API docs, Input.GetKey just checks if certain key is pressed:
Returns true while the user holds down the key identified by name. Think auto fire.
If you check it every frame, (Update() is executed every frame) you re-check if button is pressed and if so, proceed with velocity change.
EventTrigger
EventTriggers are a little different. They actually bind a delegate (UnityAction to be precise) you pass to them and trigger it as callbacks on given EventTriggerType fired. So if you bind to OnPointerDown() with your moveRight(), it will fire when pointer goes down over GameObject. If you keep the button pressed, it won't fire again because actual event of pointer going down has already happened and it already fired appropriate trigger you passed to it. Pointer is actually not going down right now. It's pressed.
It all goes down to the fact that one part checks every frame if button is pressed while the other one fires when button press happens.
So I have a button with a onClick method like this:
public void myClickHandler(View v){
}
I have multiple checkboxes on one page, and when that button is pressed it will start a timer on all of the checked boxes. Also, most of the timers have different times. Have any ideas on how I can get this working? Thanks!
i think,you can use one timer,or just one thread.computing and send different broadcasts or the same broadcast with different args,handle and dispatch in the method onReceive;
While developing an android app, I have an activity with some buttons. I would like the onClickListener action to start when the user puts his finger on the screen immediately so he isn't able to click two buttons at once.
I do that in iOS by using the touchDown event rather than the default TouchUpInside.
I have absolutely no idea of how this can be done in Android...
Android code :
button.setOnClickListener(clicked);
[...]
View.OnClickListener clicked = new View.OnClickListener() {
public void onClick(View v) {
//Cool stuff there
}
}
thanks for helping !
What you are looking for is using an OnTouchListener instead of the OnClickListener. This will allow you to react upon the first touch of the button and stop reacting on other button touches until the first touch is stopped again.
You can disable the other buttons when you start the click and re-enable them when you are finished.
By default the buttons clicks will be handled on the same thread so their actions would not complete in parallel even if you do not disable them.
Good Morning,
I have a list of items with a prev. & next button. When the user is at the start of the list the prev button is disabled. Clicking next takes them to the next record and my click handler sets the prev button enabled true. However in the emulator it doesn't show the button enabled. Clicking next moves me to the third record and again the handler sets the prev button enabled but this time it does become enabled in the emulator. I'm grasping at straw here but do I need to invalidate and redraw or something?? I don't understand why such an elementary task is not working.
In XML:
<Button
android:id="#+id/btn_PrevLift"
...
android:enabled="false"
android:onClick="btn_PrevLiftClick" />
In the handler code:
private void UpdateNavButtonStatus(int z)
{
...
btn_Next.setEnabled(true);
btn_Prev.setEnabled(true);
....
}
No just to show you how little I know about what I'm doing how come when I look at the variable values in Eclipse debug I can't see the enabled property in any state???
More Info
Very odd to me at least. If I move from using XML defined event handlers to programatically defined as below it works great!!!???
btn_Nxt.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//Call helper methods etc...
}
});
I think I have it but not sure exactly why
So when I was trying to get a handle on my Button objects I was using the View.findViewById(etc). When I changed from XML to the programatically declared event handler I used ViewGroup.findViewById. Reverting back to xml if I use the ViewGroup I get a "different" handle that seems to work...????
Ok,so what you can do is declare a variable count and on the click f button next increment the value of count..and on the click of Back button decrement the value of count and give the condition that while count<0 the task of the back button is done else nothing
I have identified the problem and of course the problem is me. I was enabling the button and then the ListView was updating to the next record. Of course my enabled button was one behind so it appeared that when I clicked a second time the button suddenly enable but that is not the case. When I clicked a second time we moved to record three and showed button for record two which was enabled.
See my buttons were part of my ListView and new buttons were being drawn for each record. OOHHHH it makes so much sense now how everything was behaving.
Anyway I moved the buttons off the ListView layout so they remain constant as the user navigates through the records.