Overflow menu icon not visible... - android

I am adding the toolbar to linear layout I have created a toolbar without using xml.Everything is working fine but i am not able to add overflow menu .Overflow icon is not showing
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_lnrLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.smartify.customizetoolbardemo.MainActivity"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lnrlayout_toolbar"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>

Add a menu to your toolbar
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Settings"
android:id="#+id/action_settings"
/>
</menu>
inflate you menu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
You will get icon of overflow menu then.

Add one button or any view in yourlnrlayout_toolbar
Button btn=(Button)findViewById(R.id.btn_menu);
showPopupMenu(btn);
and call below method for showing overlay menu
public void showPopupMenu(View v){
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sub_menu, popup.getMenu());
popup.show();
}
is sub_menu is your menu file that you want to show..

Try this
XML code for menu items
create menu resource in res>>menu>>menu_optipn.xml
<item android:id="#+id/new_game"
android:icon="#mipmap/ic_launcher"
android:title="item1"
/>
<item android:id="#+id/help"
android:icon="#mipmap/ic_launcher"
android:title="item2"
android:orderInCategory="0"
/>
MainActivity.Java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_option, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
Toast.makeText(this, "Item 1 is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.help:
Toast.makeText(this, "Item 2 is selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Related

Menu items are not showing on my android phone

I am running this code on my android phone and i am unable to see menu items on my android phone but when i run this on emulator, i can see menu items.
Please help . Here is the screenShot of.
<item android:title="About Us"
android:id="#+id/aboutUs_ID"/>
<item android:title="Preferences"
android:id="#+id/preferences_ID"/>
java code in activity
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater myMenu = getMenuInflater();
myMenu.inflate(R.menu.menu_file, menu);
return true;
}
Try to use below solution;
res/menu/main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Color Theam"
app:showAsAction="never" />
<item
android:id="#+id/action_appMahiti"
android:orderInCategory="100"
android:title="#string/tab_refrence"
app:showAsAction="never" />
</menu>
Here is the code snippiest you can add in your activity class
Activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// perform operation or display message
Toast.makeText(this, "CLick me", Toast.LENGTH_SHORT).show();
break;
case R.id.action_appMahiti:
// perform operation
startActivity(new Intent(this, XYZ.class));
break;
}
return super.onOptionsItemSelected(item);
}
I will suggest to try to use above solution.

ActionBar menu with text ,icon,text

Hello guys i want to display action with text and images on action bar, but i only get text in overflow menu
my menu.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_show_ir_list"
android:showAsAction="ifRoom"
android:title="Cancel"/>
<item
android:id="#+id/phone"
android:title="Wowio"
android:icon="#drawable/logo"
android:showAsAction="ifRoom"
/>
<!--android:icon="#drawable/info"-->
<item
android:id="#+id/computer"
android:title="Done"
android:showAsAction="ifRoom"
/>
</menu>
in my main class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected About App", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected About Developer", Toast.LENGTH_SHORT).show();
break;
case R.id.action_show_ir_list:
Toast.makeText(getBaseContext(), "Volume Set To Max", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
Here every thing is coming in over flow menu nothing is coming on action bar
I tried every thing on internet and nothing is working
What should i do to show them on ActionBar?
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:[yourapp]="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:orderInCategory="10"
[yourapp]:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
instead of [yourapp] type your app name

How to add toggle button in menu item in android

I have options menu item in my application. Requirement was to add a toggle button to a menu item. Is this possible?
As of this writing there are 3 options.
1) Use app:actionViewClass. Example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Switch!"
app:actionViewClass="android.widget.Switch"
app:showAsAction="always" />
</menu>
2) You can use a custom layout in a menu item to add toggle button. Example:
Create a layout with Switch (alternatively, you may also use ToggleButton), res/layout/menu_switch.xml:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="64dp" />
And use that layout in menu item:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="#string/switch_button_title"
app:actionLayout="#layout/menu_switch"
app:showAsAction="always" />
</menu>
3) You need to set android:checkable property of the menu to true and control its checked state in runtime. Example:
Menu:
<item
android:id="#+id/checkable_menu"
android:checkable="true"
android:title="#string/checkable" />
Activity:
private boolean isChecked = false;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.checkable_menu);
checkable.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.checkable_menu:
isChecked = !item.isChecked();
item.setChecked(isChecked);
return true;
default:
return false;
}
}
Hope this helps.
I had a couple of issues when using a actionViewClass="android.widget.Switch in a menu item. It does actually show a switch on the ToolBar, although for me:
The onOptionsItemSelected() doesn't actually get called when I toggle
the switch.
Using setChecked() on the MenuItem doesn't toggle its state.
Upon debugging and removing the actionViewClass="android.widget.Switch, the onOptionsItemSelected() gets called again.
Not sure what was going on; Maybe that I am using a custom ActionBar that I set using setSupportActionBar(), and using OptionsMenu callbacks within a fragment by enabling it with setHasOptionsMenu(true).
I get this solved by inflating the switch itself, and set OnCheckedChangeListener on it
<item
android:id="#+id/my_switch"
android:title=""
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
app:showAsAction="always" />
And in Fragment
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
MenuItem menuItem = menu.findItem(R.id.my_switch);
SwitchCompat mySwitch = (SwitchCompat) menuItem.getActionView();
mySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
// Do something when `isChecked` is true or false
});
}
And to toggle the switch programmatically, call setChecked() on the Switch, not on the MenuItem.
use app:actionViewClass
<item android:id="#+id/id"
android:title="#string/string"
app:actionViewClass="android.widget.ToggleButton"
android:orderInCategory="80"
app:showAsAction="always" />
public boolean onPrepareOptionsMenu(final Menu menu) {
if(super.mMapView.isTraffic())
menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_off_48);
else
menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_on_48);
return super.onPrepareOptionsMenu(menu);
}
Do you mean you want to add a toggle button as one of the elements/items appearing in the options menu or add a button to a list item from the menu?
Then you can do it with a custom layout(use a ListView within if you want) and inflating it in the
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
and you can save the values each time the button is toggles.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.btnToggleValue:
// save it here
return true;
case R.id.btnSecond:
...
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Popup menu on click of a button in action Bar

I am trying to implement an action bar in which one of the buttons on click shows a popup menu.
Here's the menu. XML (menu items in the action bar)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search"
android:icon="#drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="#string/menu_search"/>
<item
android:id="#+id/refresh"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_refresh"/>
<Item
android:id="#+id/popup"
android:icon="#drawable/ic_action_search"
android:onClick="showPopup"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_search" />
I wish to show a popup menu on the click of the item having id "#+id/popup".
here is the XML for the popup menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item1"
android:icon="#drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="#string/menu_search"/>
<item
android:id="#+id/item2"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_search"/>
here is the onClick method for the button
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.overflow, popup.getMenu());
popup.show();
}
And the problem is that no popup shows up on click of that button. Need help folks.
I found this here: http://developer.android.com/guide/topics/ui/menus.html
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/selectImg"
android:icon="#android:drawable/ic_dialog_dialer"
android:showAsAction="always">
<menu>
<item android:id="#+id/top"
android:title="#string/topimg"/>
<item android:id="#+id/bottom"
android:title="#string/botimg" />
</menu>
</item>
</menu>
You can put a menu within a menu to present sub-menus when the item is clicked. Then, in Java, you can use the same methods as usual.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
// View v = findViewById(R.id.f);
switch (item.getItemId()) {
case R.id.top:
//action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The id of 'top' in the xml is still recognized even though it is a sub menu. This worked for me and it looks just like the popup menu.
Hi every one, that's my own solution : i created the showPopup method, and then i called it in the onOptionsItemSelected like this :
public void showPopup(){
View menuItemView = findViewById(R.id.menu_save);
PopupMenu popup = new PopupMenu(getActivity(), menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.popup, popup.getMenu());
popup.show();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
showPopup();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
popup.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/decon"
android:showAsAction="ifRoom"
android:title="#string/decon"/>
<item
android:id="#+id/mRes"
android:showAsAction="ifRoom"
android:title="#string/mesRes"/>
</menu>
main.xml => it's called onCreateOptionsMenu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_save"
android:enabled="true"
android:icon="#drawable/action_save"
android:showAsAction="ifRoom|withText"
android:title="#string/action_save"
android:visible="true"/>
</menu>
Finally i
implements PopupMenu.OnMenuItemClickListener to #Override onMenuItemClick method.
As the popup menu is a MENU, you have to handle this by implementing the "onOptionsItemSelected". You'll be able to say what to do for each menu option. It will replace the "onClick" option you defined and will be called automatically.
Try to change 'this' to getActivity().
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(getActivity(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.overflow, popup.getMenu());
popup.show();
}
Hope it helps..!!
I found a solution to this. Instead to using the menu XML to inflate the popup menu, I made a XML layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8b8989"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/menuItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/menu1" />
<TextView
android:id="#+id/menuItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/menu2" />
<TextView
android:id="#+id/menuItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/menu3" />
</LinearLayout>
and i changed the onClick method
public void showPopup(View v) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(
R.layout.overflow_layout, null, false), 300, 400, true);
pw.showAtLocation(findViewById(R.id.container), Gravity.CENTER, 0,
0);
}
This solved the issue
android:onClick="popup"
may be you should change it to android:onClick="showPopup"?

Android Hardware Menu button isn't working for me

I'm using this code to display a menu, but it isn't doing anything when I press the menu button.
This is in a view flipper, I don't know if that has anything to do with the problem.
I used this before and with no problems at all:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.new_game:
return true;
case R.id.help:
finish();
return true;
}
return false;
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:title="new_game" />
<item android:id="#+id/help"
android:title="clear" />
</menu>
You are not calling the superclass methods.
Dont you have to have some icon attached to it?
android:icon="#drawable/ic_menu_add"
for example

Categories

Resources