Action Bar Drawer Toggle Custom Icon

user3240782 picture user3240782 · Jun 19, 2014 · Viewed 9.7k times · Source

I'm trying to use the action bar drawer toggle, but I would like it to display something more than just the menu icon. I want it to display a notification count in the top right corner of my custom menu icon, but I still want the action bar drawer toggle to display the back arrow when needed.

Therefore, I first tried to see if I could display a custom layout (containing an imageview for my menu icon and a textview for my notification count) on the action bar drawer toggle but whithout success.

I then tried to create a menu item containing my custom menu icon. When this menu item is clicked, I open/close the navigation drawer, but I can't manage to display a textview containing my notification on the menu icon.

Any help/idea is appreciated :)

Answer

Dhaval Parmar picture Dhaval Parmar · Jun 19, 2014

try these way:

ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("hi");
        actionBar.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.actionbar_back_color));
        actionBar.setIcon(getResources().getDrawable(
                R.drawable.image_crush_home_drawar_icon));
        actionBar.setCustomView(R.layout.actionbar_right_two_icon);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(false);

layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/actionbar_back_color"
    android:orientation="horizontal"
    android:weightSum="6" >

    <LinearLayout
        android:id="@+id/line_search"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/image_action_"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_action" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/actionbar_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center|left"
            android:singleLine="true"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/actionbar_text_color" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/line_search"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/image_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image_crush_home_search_icon" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/line_post_Message"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/image_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image_crush_home_message_icon" />
    </LinearLayout>

</LinearLayout>