Cancel onListItemClick() if onCreateContextMenu() Fails - android

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

Related

InputMethodManager.showInputMethodPicker not showing when custom keyboard in my app is not currently selected

I am implementing a custom keyboard with InputMethodService. Calling showInputMethodPicker from a service that is a chat head (a floating widget like in facebook messenger) when the selected keyboard is my custom one works but if current selected keyboard is something else e.g. Gboard, the input method picker UI doesn't show up, no error message on Run console in android studio. By selected keyboard, i mean the default keyboard in settings.
// inside main activity, start 'chat head' service when a button in app is clicked
startService(Intent(this#MainActivity, FloatingViewService::class.java))
// inside 'chat head' service, show picker UI when a button in expanded 'chat head' menu is clicked
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.showInputMethodPicker()
How to make showInputMethodPicker work even though my custom keyboard is not selected or is it not possible? Not familiar with Android, so alternative approach is welcome (if the method above is wrong from the start)
Followed this blog post for creating the 'chat head' and this stackoverflow post for how to use showInputMethodPicker
related post, question mentions about using a postDelay, so my current workaround is: when button clicked in chat head service, create an intent to start activity with a boolean data using putExtra, activity onCreate gets called, if the boolean is true, call showInputMethodPicker after some delay (500ms for me works), set boolean to false
start activity from service details
delay using postDelayed details

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.

Android app - delete item from list following an action in another activity

I'm creating an app where I display a list of pending challenges. When the user clicks on a challenge, he can accept it or ignore it.
Here's what I want to do and I don't know how :
if the user accepts or ignore the challenge, call this.finished and remove the challenge from the list
if the back button is pressed, do nothing, the challenge is still visible
In short, if the user really responds to the challenge I don't want it to be displayed in the list, but if he doesn't choose any option and press the back button, he didn't choses one of the two actions so I want that challenge to still be visible in the list.
I don't think it's possible to detect what button I've pressed when i go back to my main activity. I've thought about using global variables, but I don't want to misuse them either.
Just to be clear, I'm not asking how deleting a list item. But when to know deleting one depending of the actions of another activity.
Give your second activity the index you want to remove as a parameter inside the intent and let it finish by returning the index again as an intent extra (by using setresult(Intent i) and then calling finish) inside your first activity catch the result from your second activity by overwriting onActivityResult (http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))
see 3.3. Retrieving result data from a sub-activity in http://www.vogella.com/tutorials/AndroidIntent/article.html for a detailed howTo

close a slider after an intent is launched

I have an activity, DogActivity, with a slider. When the user slides view PawsView to a certain degree, I start another activity, CatActivity, using startActivity(intent). If the user clicks the back button, normally the user returns to DogActivity. Here is my problem: if in Developer options I set Do not keep activities then when the user clicks the back button and thus returns to DogActivity, the slider is not asserted and so PawsView is back to its original position; however, if I don't have that option selected, upon returning to DogActivity the slider is still asserted (the sliding already occurred).
Since I don't want to depend on the user selecting or deselecting Do not keep activities, I need to do this programmatically. So does anyone know how to do this? I have tried putting the appropriate code inside onResume but that has no effect. It's as if finishing CatActivity has no effect on DogActivity. BTW, the view I am using to display PawsView is a custom view.
I already tried using a handler with postDelayed to pull PawsView back to normal, but the handler always executes before the startActivity is executed. If on the other hand I start a proper Thread to run the call to close the slider, I get an error about the wrong thread trying to change the layout.
Another way of asking the question may be: How do I force onResume to be called even when Do not keep activities is NOT selected on a user's device.
You could try to launch CatActivity using startActivityForResult and then handle the result in onActivityResult and do the necessary setup from there. It's sort of like forcing onResume.

Android - Manage click and longclick in listview

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.

Categories

Resources