Android - Manage click and longclick in listview - android

I am currently coding a new android Application, and I need to use both OnItemClickListener and OnItemLongClickListener on a listview. Each listener launch a different actionmode on the actionbar.
The problem is that the actionmode associated with the click event is the only that is launched even if I perform a longclick.
After some research, I understand now why : a long click event also create click event, and I guess that this last event is always perform after the long click event, so that explain why I can't manage to display the other actionmode.
The question is : how can I block the click event when i do a longclick ? Or does it exist another mean to perform what I want to do ?

onLongClick():
Returns
true if the callback consumed the long click, false otherwise.
So, if you return true the onclick won't be executed.

Related

#Button new on click listener

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.

Getting continuous input from a button in unity2d

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.

How to keep validation over this cart button?

Kindly check this link , in this on click of cart button ,it shows animation that its added to cart , but i want to keep a validation over this button ,then only it should get added to the cart . Please help me with this code
https://github.com/truizlop/FABRevealLayout
You've got two possibilities:
1) Override the clickListener from the library by registering your own at the FloatingActionButton using fabButton.setOnClickListener(myListener). Make sure you're doing that after the fabRevealLayout has been inflated and initialized. In onCreate() after calling through to super.onCreate() should be fine.
Inside your own clickListener, you perform your validation and based on the result manually trigger the revealing of what the library considers the "secondary view" (fabRevealLayout.revealSecondaryView();) or do not.
2) Register a onTouch listener to the fab button. Inside that touch-listener you check whether it is an ACTION_DOWN event and then do your validation. If it succeeds, return true to notify the system, that the event has not been handled by your functionality (that it has "consumed" the event). Downside: No click sound when pressing among others, so "not the nice way", as we're interfering the android touch handling. On the other hand you do not have to manipulate the third party library.
I would recommend going with the first option.

Using an Android Long-Button press to increment/decrement counter

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.

Cancel onListItemClick() if onCreateContextMenu() Fails

I have a basic ListActivity that supports context menus via onCreateContextMenu() and onContextItemSelected(). If either of these methods fail and the context menu is not generated, the current default functionality seems to be to fire off onListItemClick(). TO me, if a user intentionally holds down their finger to get to the context menu, only to then have the item open, is very confusing and could lead them to simply abandoning this gesture in your app if it happens.
What I'd like to do is on failure fire off a Toast with a helpful message, then cancel the onListItemClick so that the list item doesn't open. I have the Toast working, but trying to figure out if it's possible to cancel the click action from inside on of onCreateContextMenu() or onContextItemSelected() where the error will occur.
Thanks!
So I have a solution to this that I am using and it seems to work fine. I created a boolean field, mIgnoreClick, and set that to false in onCreate(). Then in onCreateContextMenu(), if an error occurs generating the context menu, I set mIgnoreClick to true and send a Toast to the user alerting them that this functionality is currently unavailable. Finally, in onListItemClick(), I check the state of mIgnoreClick; if false, the item is displayed, if true, nothing is done and mIgnoreClick is set back to false so that the user can click on listitems and have them open as needed.
Thus, if the menu wasn't generated properly, I do nothing; they never intended to open the item, so this behavior meets my needs.
Paul

Categories

Resources