I'm struggling to use ImageView as a button that I need when I click it should display items, like when we click a spinner the same procedure.
final View imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
// display a list of suggestions !
}
});
I will be thankful if there is anyone who gonna help me to solve this problem.
First you create your menu of items you want to show like this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/first"
android:title="First Menu Item"/>
<item
android:id="#+id/second"
android:title="Second Menu Item"/>
<item
android:id="#+id/third"
android:title="Third Menu Item"/>
</menu>
Then in your Activity you create a PopupMenu
PopupMenu pm = new PopupMenu(MainActivity.this, pBtn);
pm.getMenuInflater().inflate(R.menu.popup_menu, pm.getMenu());
pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.first:
Toast.makeText(MainActivity.this, "Clicked First Menu Item", Toast.LENGTH_SHORT).show();
return true;
case R.id.second:
Toast.makeText(MainActivity.this, "Clicked Second Menu Item", Toast.LENGTH_SHORT).show();
return true;
case R.id.third:
Toast.makeText(MainActivity.this, "Clicked Third Menu Item", Toast.LENGTH_SHORT).show();
return true;
}
return true;
}
});
pm.show();
Finally you call this popup menu in your click listener
EDIT:
Create an ArrayList of String for example
ArrayList<String> popupItems = new ArrayList<String>();
Fill your arraylist with your data
Then you initialize your popupmenu with this array list
popupMenu = new PopupMenu(this, imageButton);
Loop through your array add values to the popupmenu menu
for (int i =0; i < popupItems.size(); i++)
popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, popupItems.get(i))
You can show the items in dialog, when you click on image view show the dialog
Related
I am trying to hide a few of menu item located in android toolbar.
I have implement this code :
MenuItem beaconIconMenuItem =
toolbar.getMenu().findItem(R.id.booklist_mylibrary_menu_beacon);
beaconIconMenuItem.setVisible(false);
When I try to compile the code, there is no error, but instead of removing the toolbar it is hown twice.
This is my source code:
private void setupToolbar() {
toolbar = (Toolbar) getView().findViewById(R.id.toolbar);
if (getResources().getString(R.string.app_name).equalsIgnoreCase("PNM e-Reader")){
toolbar.setTitle("My Books");
}else {
toolbar.setTitle(getString(R.string.booklist_drawer_mylibrary));
}
toolbar.inflateMenu(R.menu.booklist_mylibrary_menu);
// if kpm app , hide beacon icon on toolbar
if (getResources().getString(R.string.app_name).equalsIgnoreCase("KPM")){
toolbar.inflateMenu(R.menu.booklist_mylibrary_menu);
MenuItem beaconIconMenuItem = toolbar.getMenu().findItem(R.id.booklist_mylibrary_menu_beacon);
beaconIconMenuItem.setVisible(false);
//if kpm app , hide filter icon on toolbar
}else if
(getResources().getString(R.string.app_name).equalsIgnoreCase("KPM")){
toolbar.inflateMenu(R.menu.booklist_mylibrary_menu);
MenuItem filterMenuItem = toolbar.getMenu().findItem(R.id.common_menu_filter);
filterMenuItem.setVisible(false);
//if kpm app , hide new collection selection under settings icon on toolbar
}else if
(getResources().getString(R.string.app_name).equalsIgnoreCase("KPM")){
toolbar.inflateMenu(R.menu.booklist_mylibrary_menu);
MenuItem newcollectionMenuItem = toolbar.getMenu().findItem(R.id.booklist_mylibrary_menu_addtab);
newcollectionMenuItem.setVisible(false);
}
And this is the xml file to related java class:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/booklist_mylibrary_menu_beacon"
android:title="#string/beacon_switch"
android:id="#+id/common_menu_filter"
android:title="#string/common_filter"
android:id="#+id/booklist_mylibrary_menu_addtab"
android:title="#string/booklist_tab_add"
app:showAsAction="never" />
<item
</menu>
You are inflating the menu twice. Once before the if statement and once inside the if statement
toolbar.inflateMenu(R.menu.booklist_mylibrary_menu);
You are calling this before the if statements and inside the if statements.
Remove it from the if statement.
Resources resources = getResources();
if (resources.getString(R.string.app_name).equalsIgnoreCase("KPM")) {
MenuItem beaconIconMenuItem = toolbar.getMenu().findItem(R.id.booklist_mylibrary_menu_beacon);
beaconIconMenuItem.setVisible(false);
//if kpm app , hide filter icon on toolbar
} else if (resources.getString(R.string.app_name).equalsIgnoreCase("KPM")) {
MenuItem filterMenuItem = toolbar.getMenu().findItem(R.id.common_menu_filter);
filterMenuItem.setVisible(false);
//if kpm app , hide new collection selection under settings icon on toolbar
} else if (resources.getString(R.string.app_name).equalsIgnoreCase("KPM")) {
MenuItem newcollectionMenuItem = toolbar.getMenu().findItem(R.id.booklist_mylibrary_menu_addtab);
newcollectionMenuItem.setVisible(false);
}
Look's like your only doing else-if on kpm so I will just convert it to a switch and clean up your code. now it should look like
private void setupToolbar() {
toolbar = (Toolbar) getView().findViewById(R.id.toolbar);
String appName = getResources().getString(R.string.app_name).toLowerCase();
toolbar.setTitle(appName.equals("pnm e-reader") ? "My Books" : getString(R.string.booklist_drawer_mylibrary));
toolbar.inflateMenu(R.menu.booklist_mylibrary_menu);
switch (appName) {
case "kpm":
Menu menu = toolbar.getMenu();
MenuItem beaconIconMenuItem = menu.findItem(R.id.booklist_mylibrary_menu_beacon);
MenuItem filterMenuItem = menu.findItem(R.id.common_menu_filter);
MenuItem newcollectionMenuItem = menu.findItem(R.id.booklist_mylibrary_menu_addtab);
beaconIconMenuItem.setVisible(false);
newcollectionMenuItem.setVisible(false);
filterMenuItem.setVisible(false);
break;
}
}
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 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
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);
}
Hello im trying to use onclicklistener along with onmenuselectlistener in my mainactivity class and the program runs but selecting the popup menu opions dont work they wont set the text to want it want anyone have any idea's? I know i did not implement onMenuSelectListener and maybe thats my problem if it is is there any other way to makes this work?
Here is my code:
public class MainActivity extends Activity implements OnClickListener {
// init variables
Handler uiHandler;
EditText cl;
TextView info;
Button enter;
Button line;
Button arc;
DrawingUtils callDU = new DrawingUtils();
DrawingTools callDT = new DrawingTools();
EditTools callET = new EditTools();
Conversion callConversion = new Conversion();
GLSurfaceView mGLSurface;
String Tag = "Debug";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLSurface = new GLSurfaceView(this);
mGLSurface.setRenderer(new BasicRenderer());
setContentView(R.layout.canvas);
FrameLayout v = (FrameLayout) findViewById(R.id.canvas);
v.addView(mGLSurface);
// init views and buttons
info = (TextView) findViewById(R.id.info);
enter = (Button) findViewById(R.id.enter);
line = (Button) findViewById(R.id.line);
arc = (Button) findViewById(R.id.arc);
cl = (EditText) findViewById(R.id.cl);
/*
* Handler for Main Thread uiHandler = new Handler() { public void
* handleMessage(Message msg) { switch (msg.what) {
*
* } Bundle bundle = msg.getData(); String string1 =
* bundle.getString("P1Key"); String string2 =
* bundle.getString("P2Key"); info.setText(string1);
* info.setText(string2); } };
*/
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.enter:
break;
case R.id.line:
break;
case R.id.arc:
break;
}
};
public void CreatePopupMenu(View v) {
PopupMenu mypopupmenu = new PopupMenu(this, v);
MenuInflater inflater = mypopupmenu.getMenuInflater();
inflater.inflate(R.menu.filemenu, mypopupmenu.getMenu());
mypopupmenu.show();
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.newCanas:
info.setText("New");
Log.d(Tag, "New was clicked");
break;
case R.id.open:
break;
case R.id.save:
break;
}
return super.onMenuItemSelected(featureId, item);
}
}
It doesn't look like you're attaching onClickListeners to anything. What that means is you're saying "whenever the onClick event is fired with me as the target, perform this action". But then you're never making yourself a target. Try adding the following code to your onCreate.
arc.setOnClickListener(this);
line.setOnClickListener(this);
enter.setOnClickListener(this);
The same thing happens with the PopupMenu it appears. Try adding mypopupmenu.addOnMenuItemSelectListener(this) right after you inflate the layout for your menu.
Jonathan is right. You should add .setOnClickListener(this); to each of the buttons added after you create them.
For the menu items though, you have to do the following:
1) Create a layout with the items on your menu and store it in your res/menu/ directory.
Example:
main.xml
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
2) Override the method called: onCreateOptionsMenu() to populate the items in the menu.
Example:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
3) Override the method onOptionsItemSelected() to do whatever you want with a switch like you were doing with the actionListeners.
4) Additionally, you could also override the method onPrepareOptionsMenu() which is the same as the previous one, but it is called every time the menu opens.
Good luck