How to show border around text in android action bar menu item?

Varun Bhatia

I intend to add Chrome like functionality to my android application (see arrow in the screenshot below) where a number would be displayed in the action bar menu with a border around it. How can I do this?

enter image description here

kris larson

Let's start with the box frame:

/res/drawable/count_frame.xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:inset="2dp">
    <shape
        android:shape="rectangle">

        <corners android:radius="2dp"/>
        <solid android:color="@android:color/transparent"/>
        <stroke
            android:width="2dp"
            android:color="#FF404040"/>
    </shape>

</inset>

That count_frame box is going to go around a TextView:

/res/layout/menu_action_count_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text"
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="24dp"
          android:layout_height="24dp"
          android:layout_margin="12dp"
          android:background="@drawable/count_frame"
          android:gravity="center"
          android:textColor="#FF000000"
          android:textSize="13sp"
          android:textStyle="bold"
          tools:text="4"/>

This TextView is going to be an action view for your menu item. (Using the app: namespace since I am assuming you are using AppCompatActivity):

/res/menu/menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_result_view"
        android:title="@string/count"
        app:actionLayout="@layout/menu_action_count_view"
        app:showAsAction="always"/>

</menu>

Now in your onCreateOptionsMenu override, you get the action view and set it up. Let's assume your count is in private int mCount;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    TextView count = (TextView) menu.findItem(R.id.action_result_view).getActionView();
    count.setText(Integer.toString(mCount));  // so the int isn't mistaken for a resource id!
    count.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // do your action here
        }
    });
    return true;
}

When the count changes, call supportInvalidateOptionsMenu().

In case you wanted to show the overflow menu on the tap of this bordered textview, use the following code in onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        final Menu m = menu;
        final MenuItem item = menu.findItem((R.id.action_result_view));
        TextView count = (TextView) menu.findItem(R.id.action_result_view).getActionView();
        count.setText(Integer.toString(mCount));  // so the int isn't mistaken for a resource id!
        count.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                m.performIdentifierAction(item.getItemId(), 0);
            }
        });
        return true;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to show border around text in android action bar menu item?

From Dev

menu item won't show in action bar

From Dev

Show Menu item always in support action bar

From Dev

Action Bar menu item text color

From Dev

Programatically fire action bar menu item in Android

From Dev

Programatically fire action bar menu item in Android

From Dev

android - menu item not showing in action bar

From Dev

How to show list of icon with text in android action menu like image

From Dev

How to show Text at top right corner of Action bar in Android?

From Dev

Hide/Show Action Bar Option Menu Item for different fragments

From Dev

Android - How to hide the Menu Button in Action Bar

From Dev

Menu item moved in action bar

From Dev

Menu item moved in action bar

From Dev

Action Bar - Drag Menu Item

From Java

Android action bar options menu item custom selectable background

From Dev

Android action bar options menu item custom selectable background

From Dev

Changing Action Bar Menu Item Icon When Pressed in Android Studio

From Dev

Android - How to show App logo in action bar

From Dev

How to remove an item from the action bar drop down navigation menu

From Dev

How to don't show menu item in Action Overflow?

From Dev

Show search bar with action (bar item)

From Dev

How to Show/Hide Action Bar item programmatically via Click Event

From Dev

How to Show/Hide Action Bar item programmatically via Click Event

From Dev

How to change the text color in Android Action bar

From Dev

How to align the Action bar Tab text in android

From Dev

How to centre text in Sherlock Action Bar with icon and sliding menu

From Dev

Android - How to add text to a menu item?

From Dev

android fragment action bar menu

From Dev

How to draw border around a bar in MPAndroidChart BarCart?

Related Related

  1. 1

    How to show border around text in android action bar menu item?

  2. 2

    menu item won't show in action bar

  3. 3

    Show Menu item always in support action bar

  4. 4

    Action Bar menu item text color

  5. 5

    Programatically fire action bar menu item in Android

  6. 6

    Programatically fire action bar menu item in Android

  7. 7

    android - menu item not showing in action bar

  8. 8

    How to show list of icon with text in android action menu like image

  9. 9

    How to show Text at top right corner of Action bar in Android?

  10. 10

    Hide/Show Action Bar Option Menu Item for different fragments

  11. 11

    Android - How to hide the Menu Button in Action Bar

  12. 12

    Menu item moved in action bar

  13. 13

    Menu item moved in action bar

  14. 14

    Action Bar - Drag Menu Item

  15. 15

    Android action bar options menu item custom selectable background

  16. 16

    Android action bar options menu item custom selectable background

  17. 17

    Changing Action Bar Menu Item Icon When Pressed in Android Studio

  18. 18

    Android - How to show App logo in action bar

  19. 19

    How to remove an item from the action bar drop down navigation menu

  20. 20

    How to don't show menu item in Action Overflow?

  21. 21

    Show search bar with action (bar item)

  22. 22

    How to Show/Hide Action Bar item programmatically via Click Event

  23. 23

    How to Show/Hide Action Bar item programmatically via Click Event

  24. 24

    How to change the text color in Android Action bar

  25. 25

    How to align the Action bar Tab text in android

  26. 26

    How to centre text in Sherlock Action Bar with icon and sliding menu

  27. 27

    Android - How to add text to a menu item?

  28. 28

    android fragment action bar menu

  29. 29

    How to draw border around a bar in MPAndroidChart BarCart?

HotTag

Archive