Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning of actionLayout in navigation drawer

I have the following setup:

<android.support.design.widget.NavigationView
    style="@style/NavigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:itemTextAppearance="@style/DrawerTextAppearance"
    app:menu="@menu/drawer"/>

menu/drawer.xml

<menu>    
    <item
        android:id="@+id/messages_item"
        android:icon="@drawable/ic_notifications_neg"
        app:actionLayout="@layout/counter"
        android:title="@string/message_center"/>

    <item
        android:id="@+id/search_item"
        android:icon="@drawable/ic_search_neg"
        android:title="@string/search"/>
</menu>

layout/counter.xml

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="26dp"
    android:layout_height="26dp"
    android:text="55"
    style="@style/Bubble"/>

style:

<style name="Bubble" parent="android:Widget.TextView">
    <item name="android:gravity">center</item>
    <item name="android:layout_gravity">center_vertical</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:background">@drawable/bubble</item>
</style>

this produces the following result:

screenshot

the bubble is shown at the top despite style's gravity setting.

How can I position the actionLayout in the middle of a menu item?

like image 416
injecteer Avatar asked Jun 03 '16 09:06

injecteer


1 Answers

I managed to solve the problem by accident.

I had to put the TextView inside a container:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/counterView"
        style="@style/Bubble"
        android:layout_width="26dp"
        android:layout_height="26dp"
        android:text="55"/>
</LinearLayout>

Now the counter is shown right in the middle of the menu item!


UPDATE

Counter changing code:

TextView view = (TextView) drawer.getMenu().findItem(R.id.counter_item).getActionView().findViewById(R.id.counterView);
view.setText("" + count);
like image 190
injecteer Avatar answered Sep 20 '22 12:09

injecteer