How to add Menu to a layout - android

I have a situation in which there are different layouts and each layout has a menu. How do I do it? For reference, you may visit Youtube Mobile App and on the right side of the video, there appears 3 dots, on clicking them, a menu will be opened. I have the screen shot but inadequate credits stop me from uploading it.Please help me out. Thanks in Advance.!

As mentioned by user1632209 you can use android's menu but if you want to create your own pop menu you can do it as follows:
PopupMenu popup = new PopupMenu(context, btnSettings); //you can use image button
// as btnSettings on your GUI after
//clicking this button pop up menu will be shown
popup.getMenuInflater().inflate(R.menu.settings_menu, popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
you can add listener to your menu option like:
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.option1:
//Code for option 1
break;
case R.id.option2:
//Code for option 2
break;
default:
break;
}
return false;
}
Create settings_menu.xml in res->menu directory like:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/option1"
android:icon="#drawable/icon_for_option1"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Option 1"/>
<item
android:id="#+id/option2"
android:icon="#drawable/icon_for_option1"
android:orderInCategory="200"
android:showAsAction="never"
android:title="Option 2"/>
</menu>

Related

ActionBar item does not shown in Option Menu

I can't show items inside the "Menu Item" when those items are already displayed on the Action bar.
This is my onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.opt_menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
This is my opt_menu_main layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="ifRoom"/>
</menu>
I'm not using support library (my target API is 17).
If I click on the button Menu it is always empty because the item is already displayed up on the ActionBar.
I've tried to add more items but when the items goes up on the ActionBar, they are not displayed inside the options menu.
I've also tried with showAsAction="ifRoom|withText"but doesn't work.
I think this behavior is correct, but is it possible to show the same item both in Menu and Action Bar at the same time?
Thanks
Try this:
app:showAsAction="always"
but don't forget to add this line at the begin of definition:
xmlns:app="http://schemas.android.com/apk/res-auto"
The Id's for menu items must be unique. The best option is to create another menu item with another Id and set its showAsAction to "never" to force it always into the overflow menu. But their is a chance if you are also displaying other primary options it might force both into the overflow in which case you will see two menu items with the same title.
For the moment I solved like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always"/>
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:showAsAction="never"/>
</menu>
So in the onOptionsItemSelected method :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add_schedule:
case R.id.menu_act_add_schedule:
//some code
break;
}
return true;
}
I don't think this is a correct solution, because on some device where there is not enough space to add this and other items up on the ActionBar,
since they are defined as showAsAction="always", they will be overlapped on the ActionBar Title, so it can create some layout problems.
The behavior that I want is to show always the items on the option menu, and show it also in the action bar if there is enough space.
This behavior can not be obtained with showAsAction="always" .
It would be great if someone could find a better solution.
Thanks anyway.

How to programatically open the android support library action bar overflow menu?

I'm using the android support compat action bar library to support 2.2 and higher.
Im able to show the menu, and one of my buttons is a overflow button (3 dots).
When it is clicked, I want to show the overflow menu.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
case R.id.action_overflow:
openOptionsMenu();
return true;
}
return super.onOptionsItemSelected(item);
}
I have this, but it is not working.
Does anyone know whats wrong?
Thanks.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sord.ids_connect="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_overflow"
android:icon="#drawable/ic_action_secure"
android:title="#string/action_overflow"
sord.ids_connect:showAsAction="ifRoom" />
</menu>
Well the basic idea behind overflow menu is that, options that are not important are collected and placed into it by default. You do not have to create an overflow menu by yourself. If you put your item's showAsAction="never" it will automatically be placed in the overflow menu.
For e.g.
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_refresh"
android:showAsAction="never"
android:title="#string/action_refresh"/>
<item
android:id="#+id/action_about"
android:showAsAction="never"
android:title="#string/action_about"/>
all of these because they have showAsAction="never" will be placed onto the overflow menu automatically.
And in your switch statement you could simply declare:
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
case R.id.action_settings:
//something
break;
case R.id.action_refresh:
//something
break;
...
}
and so on. But if there is a hardware menu button, the overflow will not appear. For this you can refer to the following.

Showing "More Menu Items" on top right drop down menu with Android ActionBarCompat

I am developing an Android app with ActionBarCompat (released by google). In tablets,it shows more menu items as a dropdown menu at top right (Image 1) but in small devices (handsets) you should hit menu button to see more items (Image 2).
Is there any way to have top right dropdown menu like tablets in handset devices? If yes, how?
Thank you.
Sure you can !
First of all, make a menu.xml in res > menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:technicachat="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<item
android:id="#+id/dropdown_menu"
android:showAsAction="ifRoom"
technicachat:showAsAction="ifRoom"
android:title="Options"
android:icon="#drawable/ic_menu_moreoverflow_normal_holo_light">
<menu>
<item
android:id="#+id/item1"
android:showAsAction="ifRoom"
technicachat:showAsAction="ifRoom"
android:title="Refresh"
android:icon="#drawable/ic_menu_refresh"/>
</menu>
</item>
</menu>
All items under the second menu will appear when you click on the dropdown menu button.
You don't even need to use onOptionsItemSelected to drop it.
Just proceed as usual to handle item selection.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.item1:
/*DO STUFF*/
}
return super.onOptionsItemSelected(item);
}
In case you want pngs for your apk, they are under /your sdk path/platforms/android-xx/data/res/ and you got drawable-hdpi, mdpi etc. For example the one you need is ic_menu_moreoverflow_normal_holo_light.
Here you are ! ;)

Unable to show the 3 dotted menu in overflow - SherlockActionBar

I've searched alot around the internet. I've done what I had to, but still I'm unable to show the 3 dotted menu as I want.
Here's the relevant code:
First, my manifest min sdk is set to 9 and my targetSdk is set to 18.
The menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/itemMainAlbum"
android:showAsAction="ifRoom"
android:title="Set as Main Album">
</item>
<item
android:id="#+id/itemImport"
android:showAsAction="ifRoom"
android:title="Import">
</item>
<item
android:id="#+id/itemSettings"
android:showAsAction="ifRoom"
android:title="Settings">
</item>
The Activity's relevent code (extends SherlockActivity):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.gallery_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.itemImport:
startImportActivity();
return true;
case R.id.itemMainAlbum:
setMainAlbum();
return true;
case R.id.itemSettings:
// do s.th.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here's how my activity looks like:
The menu item in the bottom is shown only when I click on my device's menu button. The 3 dotted menu is just never shown, no matter how many menu items I add.
If you use ActionBarSherlock, you can use this little hack. It works flawlessly for me and shows the menuoverflow on every device I have tested it on.
The "3 dotted menu" will never be shown, as your device has a MENU button. The "3 dotted menu" will only appear on devices that lack a MENU button, to allow such users the ability to access the overflow. You can see what the "3 dotted menu" looks like by setting up an emulator that emulates a device with no MENU button.
You can read more about this in the "Say Goodbye to the MENU Button" Android Developers Blog post.

How can I create an android menu item using android setting icon

Can you please tell me how can I create an android menu item using android setting icon?
Here is a list of the standard icons. I don't see a "settings" icon. Perhaps you mean "Preferences" (ic_menu_preferences)?
You can set the icon programmatically like this:
menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon);
You can also set it in your xml layout like this:
<item android:id="#+id/save_button"
android:icon="#android:drawable/ic_menu_save"
android:title="Save Image"/>
Creating Menus in Android
You can see all the icons in the android SDK forder:
_your_install_path_\android-sdk\platforms\android-10\data\res\drawable-hdpi\
and then get a reference to them with:
android.R.drawable.ic_menu_preferences
just like it was your drawable.
Add a new Vector Asset.
File -> New -> Vector Asset.
Click on the icon to change it.
Select the icon you want (e.g. search for "setting").
Adjust other settings.
Use that new Vector Asset in your xml.
android:logo="#drawable/ic_settings_white_24dp"
Party!
If you want to handle the event, just try this on your activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case android.R.drawable.ic_popup_sync:
Toast.makeText(this, "ic_popup_sync selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
And in your menu folder use something like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.test.app.MainActivity"
>
<item android:id="#+id/action_settings1"
android:icon="#drawable/abc_ic_search"
android:title="Find Location"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
<item android:id="#+id/save_button"
android:icon="#android:drawable/ic_menu_save"
android:title="Save Image"/>
<item android:id="#+id/refresh"
android:icon="#android:drawable/ic_popup_sync"
android:title="Refresh"/>
</menu>

Categories

Resources