Reputation: 3458
How to set actionbar menu with icon and text both? Like this
Upvotes: 1
Views: 3343
Reputation: 1222
If you are using AppCompatActivity, then this workaround might perfectly suit you:
@SuppressLint("RestrictedApi")
override fun onCreateOptionsMenu(menu: Menu): Boolean {
if (menu is MenuBuilder)
menu.setOptionalIconsVisible(true)
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
In Java:
@SuppressLint("RestrictedApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (menu instanceof MenuBuilder)
((MenuBuilder) menu).setOptionalIconsVisible(true);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Result:
Upvotes: 0
Reputation: 4182
First take menu like that
menu/setting_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:checked="true"
android:id="@+id/first"
android:icon="@drawable/first"
android:title="first"/>
<item
android:id="@+id/second"
android:icon="@drawable/second"
android:title="second"/>
</menu>
demo.java
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class demo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
Toolbar tb = (Toolbar) findViewById(R.id.toolbar1);
ImageView ivSetting=(ImageView)findViewById(R.id.ivSetting);
ivSetting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopup(view);
}
});
}
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(demo.this, v);
// popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
// showPopup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.setting_menu, popup.getMenu());
try {
Class<?> classPopupMenu = Class.forName(popup
.getClass().getName());
Field mPopup = classPopupMenu.getDeclaredField("mPopup");
mPopup.setAccessible(true);
Object menuPopupHelper = mPopup.get(popup);
Class<?> classPopupHelper = Class.forName(menuPopupHelper
.getClass().getName());
Method setForceIcons = classPopupHelper.getMethod(
"setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
} catch (Exception e) {
e.printStackTrace();
}
Menu m = popup.getMenu();
for (int i = 0; i < m.size(); i++) {
MenuItem mi = m.getItem(i);
//for aapplying a font to subMenu ...
SubMenu subMenu = mi.getSubMenu();
if (subMenu != null && subMenu.size() > 0) {
for (int j = 0; j < subMenu.size(); j++) {
MenuItem subMenuItem = subMenu.getItem(j);
}
}
//the method we have create in activity
}
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int itemId = item.getItemId();
String btnName = null;
switch (itemId) {
case R.id.first: {
return true;
}
case R.id.second: {
return true;
}
}
return true;
}
});
popup.show();
}
}
demo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/cardview_shadow_end_color"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark">
<ImageView
android:id="@+id/ivSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_menu_setting"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Upvotes: 1
Reputation: 1641
I believe that is a PopupWindow
.
reference : PopupMenu with icons
P.S. I could post the whole code here, but what's the point if the question in reference link already has it. Just check it out.
Upvotes: 0
Reputation: 466
Since i was not able to comment, but the above solution won't work, your xml can be like as below
<item
android:id="@id/menu_item"
android:title="text"
android:icon="@drawable/drawable_resource_name"
android:showAsAction="always|withText" > </item>
If there are sufficient room in phone 'always|withText' will always work, If there are no room it will only place icon, you can try test with your emulator and let us know if it works.
Upvotes: 0
Reputation: 9225
Simply use code as below in menu.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:checked="true"
android:id="@+id/drawer_how"
android:icon="@drawable/change_lock"
android:title="Forgot"/>
<item
android:id="@+id/drawer_chngP"
android:icon="@drawable/cookies"
android:title="Change Password Type"/>
.....
</menu>
Upvotes: 0