I'm using the support NavigationView in my navigation drawer to display menu of items.
<android.support.design.widget.NavigationView
android:id="@+id/drawer_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/drawable_background"
app:headerLayout="@layout/drawer_header"
app:itemBackground="@drawable/drawer_item_background"
app:itemIconTint="@color/drawer_item"
app:itemTextColor="@color/drawer_item"
app:menu="@menu/drawer"
app:theme="@style/Text.DrawerItem"/>
I want to display a counter of connection requests next to a menu item.
I've tried to set the actionLayout
in XML menu definition like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
...
<item
android:id="@+id/navigation_requests"
android:icon="@drawable/ic_swap_horiz_24dp"
android:title="@string/connect_menu_connection_requests"
app:actionLayout="@layout/connection_request_counter"
app:showAsAction="always"/>
...
</group>
</menu>
Layout of the ActionView connection_request_counter.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="@color/brand_blue"
android:text="5"/>
But at this point I can't see number 5 next to the item.
I've tried to set ActionLayout programatically as well:
MenuItemCompat.setActionView(mMenu.findItem(R.id.navigation_requests), R.layout.connection_request_counter);
But still no luck.
I can get reference to the TextView like
final TextView connectionRequestCounterView
= (TextView) MenuItemCompat.getActionView(mMenu.findItem(R.id.navigation_requests));
Which actually returns a TextView instance, but even if I set a text to it, I can't see anything in the layout. Could that be related to the fact that I'm styling the menu items? (I set text color, tint and background of the menu items)
Here are my styles:
<style name="Text.DrawerItem">
<item name="android:textColor">@color/text_color_dark_grey</item>
<item name="android:textSize">@dimen/text_size_default</item>
<item name="android:clickable">false</item>
<item name="android:textColorLink">@color/text_color_dark_grey</item>
<item name="fontPath">@string/font_aller_light</item>
<!--<item name="android:background">@color/background_activity_default</item>-->
<!--<item name="background">@color/background_activity_default</item>-->
</style>
drawer_item_background.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--To override the default grey background for selected item-->
<item android:drawable="@android:color/transparent" android:state_checked="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
and drawer_drawablbe.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/background_activity_default"/>
</shape>
</item>
<item
android:bottom="24dp"
android:left="16dp">
<bitmap
android:gravity="left|bottom"
android:src="@drawable/my_drawer_logo"/>
</item>
</layer-list>
Where @drawable/my_drawer_logo
is a PNG in different densities.
Any other ideas why I can't see the action view?
See Question&Answers more detail:
os