Im working on a simple music player for my intro to Android apps class. I want to be able to add songs to a playlist listed in my context menu. When I click on my addtoplaylist contextmenuitem I want my popup menu to appear. How do I call my popup menu? Also if you have some suggestions on how to populate my popup menu, rather than my for loop, that would be cool too.
I have a contextmenu listener that kind of looks like this.
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
shareIt();
return true;
case R.id.Store:
musicStore();
return true;
case R.id.addtoplaylist:
// open popup menu
return true;
case R.id.snippet:
snippet(tem1);
return true;
default:
return super.onContextItemSelected(item);
}
}
And i have a popup menu that kind of looks like this.
public void showPopup(View v) {
int i = view.getPlaylists().size();
ArrayList<String> playlist = view.getPlaylists();
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
for(int k = 0; k > i;k++){
popup.getMenu().add(playlist.get(k));
}
popup.show();
}
As you can see here PopupMenu
V is just an anchor, so you can pass for instance the ListView which contains your item
Or if you really want the popupmenu to be anchored to the item have a look here
You get your target view like this.
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
View v = info.targetView;
So after call
showPopUp(v);
For your loop, I don't see a far better solution.
the accepted question is fine, but for the for loop part you could use an iterator :)
for (String song : playlist) {
popup.getMenu().add(song);
}
Related
For some reason when the following code runs my phone won't display a popup window with the text "+ create new track", yet the same thing works if I run on an Android Studio emulator.
I can't spot the source of this issue. On my emulator popup.show() displays a window, but when i step through on my device via a debugger this line does nothing. Strangely, popup doesn't seem to be null on the device when this occurs.
How can I get the popup window to display on a real device?
private void showTrackListing(){
int writePermissionCheck = ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (writePermissionCheck != PackageManager.PERMISSION_GRANTED){
Toast.makeText(getActivity(), "Need storage write permissions", Toast.LENGTH_LONG).show();
return;
}
trackListing = getTrackNames();
PopupMenu popup = new PopupMenu(getActivity(), v);
for (int i = 0; i < trackListing.length; i++) { //add a menu item for each existing track
popup.getMenu().add(trackListing[i].getName());
}
//TODO: Add Checkable list on longpress to delete files
//TODO: potentially change popup menu to ListView for better CAB cooperation
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_track:
trackSelectButton.setText("...");
Toast.makeText(getActivity(), "Name your new track.", Toast.LENGTH_SHORT).show();
txtTrackName.setVisibility(txtTrackName.VISIBLE);
return true;
default:
selectedTrackName = (item.getTitle().toString());
trackSelectButton.setText(selectedTrackName);
for (int i = 0; i < trackListing.length; i++) { //add a menu item for each existing track
if (trackListing[i].getName().equals(selectedTrackName)) {
selectedTrack = trackListing[i];
AudioRecorder.setFile(selectedTrack);
}
}
return true;
}
}
});
MenuInflater popupInflater = popup.getMenuInflater();
popupInflater.inflate(R.menu.popup_menu_track_selection, popup.getMenu());
popup.show();
}
popup_menu_track_selection.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<item android:id="#+id/new_track"
android:title="+ create new track"/>
</menu>
I'm new to android development and kinda to Java as well.
I'm learning how to add buttons to actionbar - everything is working, but I don't understand few things.
//Showing small icons at actionbar
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
menuItem = menu.findItem(R.id.recBin); // Finds the button in Actionbar and gets the ID
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.recBin: // Actions for delete button
dbHandler.remove(getID());
displayTaskList();
menuItem.setVisible(false); // Hide the button
break;
case R.id.editBtn: // Actions for editbutton
Toast.makeText(MainActivity.this,
"EDIT", Toast.LENGTH_LONG).show();
break;
case R.id.closeBtn:
Toast.makeText(MainActivity.this,
"CLOSE", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
This line: menuItem = menu.findItem(R.id.recBin); Is for what exactly? I took it off and my app crashed, so I understand that it's mandatory. What I don't understand is what icons ID should I put at bold space? It has to be from "menu" right, but does it matter which id I take? For instance, if I took R.id.closeBtn instead of recBin? As long as it's an id from menu, it works?
P.S. I hope this isn't a terrible question [probably is] and I'm sorry if so.
Solution:
Turns out you can simply get ID's. Stupid and simple.
private MenuItem item1, item2, item3;
//Showing small icons at actionbar
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
item1 = menu.findItem(R.id.recBin); // Rec button
item2 = menu.findItem(R.id.editBtn); // Edit button
item3 = menu.findItem(R.id.closeBtn); // Close button
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.recBin: // Actions for delete button
dbHandler.remove(getID());
displayTaskList();
item1.setVisible(false); item2.setVisible(false); item3.setVisible(false); // Hiding all buttons
break;
case R.id.editBtn: // Actions for editbutton
item1.setVisible(false); item2.setVisible(false); item3.setVisible(false); // Hiding all buttons
break;
case R.id.closeBtn:
item1.setVisible(false); item2.setVisible(false); item3.setVisible(false); // Hiding all buttons
break;
default:
break;
}
return true;
}
It's a reference to MenuItem, but it would be easier if you do:
case R.id.recBin: // Actions for delete button
dbHandler.remove(getID());
displayTaskList();
item.setVisible(false); // Hide the button
break;
You can safely remove the reference afterwards
I want to create a simple drop down list/listview like in below image. It should generate programmatically without using any xml layout.
NOTE : I am not using a spinner in here . Also I want to open it when I click on the ImageView next to the Switch.
I have no idea about this .
Have any ideas ?
not perfect, but it works ;)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
popupMenu.getMenu().add("Edit");
popupMenu.getMenu().add("Delete");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getTitle().toString()) {
case "Edit" :
//execute "edit" action
break;
case "Delete" :
//execute "delete" action
break;
}
return false;
}
});
popupMenu.show();
}
});
Just try to check and implement it
PopupMenu overflowPopupMenu = new PopupMenu(getContext(), finalOverflow);
overflowPopupMenu.getMenuInflater().inflate(R.menu.popup_overflow_options, overflowPopupMenu.getMenu());
overflowPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
break;
case R.id.delete:
break;
}
return true;
}
});
overflowPopupMenu.show();
popup_overflow_options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/edit"
android:title="#string/edit"/>
<item
android:id="#+id/delete"
android:title="#string/delete"/>
</menu>
I use PopupMenu's for this. See also this guide. The guide explains how to use the PopupMenu with an xml menu resource.
In your case you would attach a click listener to the ImageView. That listener would then create a PopupMenu using the ImageView as an anchor. Like this: PopupMenu popup = new PopupMenu(imageView.getContext(), imageView);
At this point since you need dynamic menu items you have the following options:
You can call PopopMenu.getMenu() and manually populate it with MenuItems
You can create an xml menu resource and then adjust/hide ones that need to be changed
I have a custom ListView including a custom row_layout.
After a longclick you get different options from the contextmenu. Depending on what option is chosen by the user I want to add an image/icon to the selected row in order to mark it.
I couldnt find an answer which covers exactly this use-case. I would appreciate a hint or some help or a tutorial for this case.
Thanks in advance.
Never mind I got to a solution by myself. I added a boolean variable to my ListItem - Class and the ImageView to the list_row_layout as well as a ImageView to my ViewHolder Class within the CustomAdapter class. So when a contextItem is clicked the boolean Attribute is set to true for the listItem which was clicked on.
Example Code:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int itemPosition = info.position;
switch (item.getItemId()) {
case R.id.contextItem1:
listViewItems.get(itemPosition).setMarkerAttribute(true);
listView.setAdapter(new MyCustomListAdapter(context, listViewItems));
return true;
case R.id.contextItem2:
//do sth
return true;
case R.id.contextItem3:
//do sth
return true;
default:
return super.onContextItemSelected(item);
}
}
Hope this was useful to somebody, because a lot of examples only cover removing an item ...
I've been working at this all day, and I'm really close but just can't get this to work. I have a button that pulls up an AlertDialog populated with saved entries that include Name and Price. Right now, I can click an item in the Dialog and have it automatically fill in the Name and Price fields in my activity. I want to also be able to long press an item and receive an option to delete it. This is my first try at an Android app, and a lot of this is repurposed from the Notepad Tutorial. Two things I can't figure out:
1) Is my registerForContextMenu sufficient/correct?
2) What am I doing wrong with my onCreateContextMenu?
Thanks.
savedItems.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
cDbHelper.open();
mNotesCursor = cDbHelper.fetchAllSaved();
startManagingCursor(mNotesCursor);
// Create an array of names and corresponding prices from db
String[] from = new String[]{SavedItemsDbAdapter.KEY_NAME, SavedItemsDbAdapter.KEY_PRICE};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.text1, R.id.text2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter saved =
new SimpleCursorAdapter(NewEntry.this, R.layout.saved_row, mNotesCursor, from, to);
// Build an AlertDialog to hold this list
AlertDialog.Builder builder = new AlertDialog.Builder(NewEntry.this);
builder.setTitle("Choose from list");
// IS THIS SUFFICIENT TO REGISTER FOR CONTEXT MENU?
registerForContextMenu(v);
builder.setAdapter(saved, new DialogInterface.OnClickListener() {
// When an item from the list is clicked, it automatically populates the name and price fields in activity
#Override
public void onClick(DialogInterface dialog, int item) {
Cursor c = mNotesCursor;
c.moveToPosition(item);
Intent i = new Intent(NewEntry.this, NewEntry.class);
i.putExtra("name", c.getString(
c.getColumnIndexOrThrow(SavedItemsDbAdapter.KEY_NAME)));
i.putExtra("price", c.getString(
c.getColumnIndexOrThrow(SavedItemsDbAdapter.KEY_PRICE)));
startActivityForResult(i, ACTIVITY_AUTO);
finish();
}
// TRYING AND FAILING TO SET UP A CONTEXT MENU - the goal is to be able to long press,
// have a "Delete?" option pop up, which will delete the entry when clicked
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteItem(info.id);
return true;
}
return false;
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
I just found
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
to be called, but not
#Override
public boolean onContextItemSelected(MenuItem item) {
in my subclassed AlertDialog:
public class MyAlertDialog extends AlertDialog implements
OnCreateContextMenuListener {
Perhaps this is useful to someone else as I'm pretty sure you have meanwhile solved your problem at hand.
You only need implement the following function. It will work.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
Log.e(LOGTAG, "Tao menu");
if(v == expList)
{
super.onCreateContextMenu(menu, v, menuInfo);
//AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
// We know that each row in the adapter is a Map
//HashMap map = (HashMap) simpleAdpt.getItem(aInfo.position);
menu.setHeaderTitle("Options");
menu.add(1, 1, 1, "Reprint");
menu.add(1, 2, 1, "Void");
menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
menu.getItem(1).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
}
}
Is my registerForContextMenu sufficient/correct?
You are registering a context menu for whatever savedItems is. If that is what you want the context menu on, then you are OK.
If your goal is to have a context menu on the items in the list in the dialog, your approach is wrong. You will not be able to use AlertDialog.Builder. You will need to create a custom subclass of AlertDialog, so you can override onCreateContextMenu() there.