Action bar box Toggle custom icon

I am trying to use the action bar drawer switch, but I would like it to display something more than just a menu icon. I want it to display the number of notifications in the upper right corner of my custom menu icon, but I still want the action bar drawer to display the back arrow when needed.

So I first tried to see if I could display my own layout (containing an image for my menu icon and a text box for my notification counting) in the action bar switch, but without success.

Then I tried to create a menu item containing my custom menu icon. When this menu item is clicked, I open / close the navigation box, but I cannot display the text box containing my notification on the menu icon.

Any help / idea appreciated :)

+4
source share
1 answer

try the following:

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>
+4
source

Source: https://habr.com/ru/post/1545244/


All Articles