Android Lollipop, Add Popup Menu from Title in Toolbar

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



Leave a reply



Submit