Android Lollipop, add popup menu from title in toolbar
You're going to need to add a Spinner to the Toolbar:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary">
<Spinner
android:id="@+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
You will then need to disable the default title:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
You can then retrieve and setup the Spinner as needed in your Activity/Fragment.
Android Toolbar Popup menu not showing icons
I found this solution: https://stackoverflow.com/a/30337653/197127. Basically, overriding a method and it does not break the device menu button or the overflow.
Thanks to all.
How to show menu popup exact below actionbar?
Set the theme at android manifest to
android:theme="@android:style/Theme.Holo.Light"
Show popup menu on `ActionBar` item click
So finally I found solution. When you want to anchor popupmenu to ActionItem in ActionBar you need to find view that renders ActionItem.
Simple find view with findViewById() where id is same as id of your menu item in xml.
DISPLAYING POPUP:
public boolean onOptionsItemSelected(MenuItem item) {
// ...
View menuItemView = findViewById(R.id.menu_overflow); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.counters_overflow);
// ...
popupMenu.show();
// ...
return true;
}
MENU:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
....
<item
android:id="@+id/menu_overflow"
android:icon="@drawable/ic_overflow"
android:showAsAction="ifRoom"
android:title="@string/menu_overflow"/>
....
</menu>
If menu item is not visible (is in overflow) it does not work. findViewById returns null so you have to check for this situation and anchor to another view.
Popup menu on click of action bar Icon
don't pass MenuItem as View cause it s not.
so it s better to cast it as view like this
View menuItemView = findViewById(R.id.menu_overflow); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.counters_overflow);
// And define click listener here.
popupMenu.show();
Source
Toolbar not showing menu when minsdk set to 21 or lollipop 5.0
You got to pass your custom toolbar to setActionBar(toolbar);
method.
Try adding the above code statement at the end of OnCreate
function. If that doesn't work then make the following changes.
styles.xml
:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And in AndroidManifest.xml
:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
I've made a sample code in github look at it -> Link
popup menu submenu title color
I've done this programmatically with below code:
Put this before you call popup.show()
.
// Change PopupMenu subMenu title color
PopupMenu popup = new PopupMenu(activity, view);
Menu itemSetAs = popup.getMenu();
SubMenu s = itemSetAs.findItem(R.id.SetAs).getSubMenu();
SpannableString headerTitle = new SpannableString(itemSetAs.findItem(R.id.SetAs).getTitle());
// Change the color:
headerTitle.setSpan(new ForegroundColorSpan(Color.YOUR_COLOR), 0, headerTitle.length(), 0);
// You can even change the size:
int textSize = getResources().getDimensionPixelSize(R.dimen.your_size);
headerTitle.setSpan(new AbsoluteSizeSpan(textSize), 0, headerTitle.length(), SPAN_INCLUSIVE_INCLUSIVE);
s.setHeaderTitle(headerTitle);
popup.show().
Related Topics
Custom Translucent Android Actionbar
Progress Bar While Loading Image Using Glide
Android: Sample Code to Connect to Vpn in Android 4.0 Using Vpnservice API
Oniabpurchasefinished Never Called
How to Prevent a Scrollview from Scrolling to a Webview After Data Is Loaded
Add 'Tools:Replace="Android:Value"' to <Meta-Data> Element at Androidmanifest
Am I Getting the Steps Right for Verifying a User's Android In-App Subscription
Cannot Find Zip-Align When Publishing App
How to Display Both Icon and Title of Action Inside Actionbar
Android 4.0, Text on the Action Bar Never Shows
How to Allocate These Folders in Another Place
How to Read Properties Defined in Local.Properties in Build.Gradle
Android: How Handle Message Error from the Server Using Volley
Android Access to Remote SQL Database
Android: Get Facebook Friends List