I'm new to Android. I have a list of items and trying to associate single context menus to each list item. I have set setListAdapter and onListItemClick but when I click on any list item I always get the same context menu.
Ideally, list item A should trigger menu A when cliccked and list item B should get menu B. Can't figure out how to do it. Could anybody help me find an example code I could use to learn how to do it?
I don't have any example code that shows the technique -- my best example is something I did for a consulting client.
However, let me point you to this sample project that uses context menus and use it as a basis for this explanation.
You need to return the customized menu in onCreateContextMenu(). If you always return the same menu here, you will always see the same menu. To determine which menu to display, you will need to know which list item was long-tapped. In the case of a context menu for a ListView, you can cast the ContextMenu.ContextMenuInfo supplied to onCreateContextMenu() to be an AdapterView.AdapterContextMenuInfo. That object can tell you the position and _ID of the item in the list that was long-tapped, so you can choose the proper menu.
In the sample code linked to above, I do that cast in onContextItemSelected(), so I can know which item the user is deleting. However, the same cast works in onCreateContextMenu().
The Android team recently released a number of new samples. I believe what you're trying to do has an excellent example here.
Related
I'm trying to make a subscription list in a navigation view where when you subscribe to an item the category of that item will show in the navigation view, and when you unsubscribe from every item in a category it should remove that category from the navigation view's menu.
Problem is to remove the category I need to clear the whole list and then add item by item, but the navigation view doesn't hold just the list of subscription: It also allows the user to navigate through different activities. If I use the menu.clear() method the navigation items will be removed as well.
To add them again, I need as parameters the item's ID, the group's ID, the Order int and the title. I know where to get the IDs and titles since they are coded in the XML, but I know not how to find the order in the list, or what that is.
Also if you have an easier or better way to resolve this, please let me know.
You can use menu
<group...>
Search for that in the Android docs.
Then setGroupVisible/enabled as needed.
Since you know the id of the menu item, you can just call menu.removeItem(id) to remove the item.
I was told the RemoveItem could cause problems, so I tried to avoid that.
What I did was find the item and group ID from the items I didnt want to get removed, clear the list, and then add the items with a made up order (I just made a variable and incremented it for each item I needed), then add the item names I needed for the items without interaction.
I have a ListView in my app. and when user clicks on each item of it, I want a small ListView with 3 items appear to the user.Could any one suggests me How can I implement this small list?
Since you want to always show the same items, you could use a popup activity (a custom dialog) when the user clicks on the list.
The layout of this popup activity will only have a listview, populated in the onCreate() method.
If you want to use ListPopupWindow you can see this example:
Example on ListPopupWindow
To open another activity when an item is selected add something like this in the onItemClick method in ListPopupWindowAppActivity class:
Intent intent = new Intent(getApplicationContext(), AndroidFirstActivity.class);
startActivity(intent);
Keep in mind that ListPopupActivity is available since Api 11, so if you want to target earlier apis you can either use my first option (custom dialog) or the android.widget.PopupWindow. There are several examples for android.widget.PopupWindow, one can be found here:
Example of PopupWindow
I have a listview which contains names of people from my People class. I want to be able to mark multiple people and then delete marked for example.
I have read many questions and seen answers and I've decided I want to do it the following way for simplicity...
I just select the listItem, click on options menu and I select mark. Then a listener will set a private boolean in the person class, isMarked, to true, and then, to tell the user it is marked, a tick appears next to the listItem.
So the question is, is there a way to make an image appear and disappear in android?
EDIT: I have already implemented the options menu and selecting a person and selecting mark then making his/her marked variable true. What I basically need is an indicator to the user that that person is marked.
If what you want to do is have visual feedback for the item's status, then what you probably need is a custom view layout for the items in the ListView. When the list requests a view for the item (getView function) you can show/hide an image to indicate the item's status.
Check the documentation for Adapters, as there are a few other functions related to how this is done. You don't show in the question how you've set up the source of the ListView data.
However, as it seems that what you want to do is to select items and then perform actions on them, you should read on about contextual action bar (see the 'Using the contextual action bar'). Some more info in the menu's page, particularly in the 'Enabling batch contextual actions in a ListView or GridView' section. Selecting items one by one and using the menu to mark them seems like an non-android'y way of doing things.
So the question is, is there a way to make an image appear and disappear in android?
You need to get a reference to the view you want to show/hide and then use the following: http://developer.android.com/reference/android/view/View.html#setVisibility(int)
For example:
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
if (isMarked) {
view.setVisibility(View.VISIBLE);
} else {
view.setVisibility(View.INVISIBLE);
I wish to select multiple ListView items and then share all of them. How can I go about it?
See the Awesome example here android-multiple-selection-listview
Well,
First you need to create OnItemClickListener(View v) in order to get clicks.
Then you can make a HashMap in order to preserve the items which are clicked.
You can also change the Background of the clicked item so the user will know which items are clicked.
Final thing you need is the action (button click - Share) which will pass through the HashMap and collect all clicked items. After that you can do with them what ever you want.
It looks like a huge work, but it's can easily divided into steps and you close them one by one... It actually quite simply.
I want that on the click of the option menu item in action bar, the listview's choice mode becomes multiple, which is easy to achieve. Simultaneously i want to inflate two options menu items namely "ok" and "cancel", so that once the user is done selecting some or all listview items, he can click on "ok" option menu to proceed.
There is this one Android app called Out of Milk that has a menu appear above a selected item in a list view, I was wondering how would I do that for my list view?
Here's a picture of what I'm talking about:
This is called quick action. You can check this link to see how it is done.