Just to add. If someone wants to implement a bubble with a filled circle using the shape of a ring instead of an oval, here is an example code to add the number of bubbles to the buttons on the action bar. But it can be added to any button.
(name it bage_circle.xml ):
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:useLevel="false" android:thickness="9dp" android:innerRadius="0dp" > <solid android:color="#F00" /> <stroke android:width="1dip" android:color="#FFF" /> <padding android:top="2dp" android:bottom="2dp"/> </shape>
You may need to adjust the thickness to suit your needs.
The result will be something like this:

Here is the location of the button (name it badge_layout.xml ):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.joanzapata.iconify.widget.IconButton android:layout_width="44dp" android:layout_height="44dp" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/action_bar_icon_bg" android:id="@+id/badge_icon_button"/> <TextView android:id="@+id/badge_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/badge_icon_button" android:layout_alignRight="@id/badge_icon_button" android:layout_alignEnd="@id/badge_icon_button" android:text="10" android:paddingEnd="8dp" android:paddingRight="8dp" android:paddingLeft="8dp" android:gravity="center" android:textColor="#FFF" android:textSize="11sp" android:background="@drawable/badge_circle"/> </RelativeLayout>
In the menu, create an element:
<item android:id="@+id/menu_messages" android:showAsAction="always" android:actionLayout="@layout/badge_layout"/>
In onCreateOptionsMenu get a link to the menu item:
itemMessages = menu.findItem(R.id.menu_messages); badgeLayout = (RelativeLayout) itemMessages.getActionView(); itemMessagesBadgeTextView = (TextView) badgeLayout.findViewById(R.id.badge_textView); itemMessagesBadgeTextView.setVisibility(View.GONE); // initially hidden iconButtonMessages = (IconButton) badgeLayout.findViewById(R.id.badge_icon_button); iconButtonMessages.setText("{fa-envelope}"); iconButtonMessages.setTextColor(getResources().getColor(R.color.action_bar_icon_color_disabled)); iconButtonMessages.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (HJSession.getSession().getSessionId() != null) { Intent intent = new Intent(getThis(), HJActivityMessagesContexts.class); startActivityForResult(intent, HJRequestCodes.kHJRequestCodeActivityMessages.ordinal()); } else { showLoginActivity(); } } });
After receiving notification of messages, set the quantity:
itemMessagesBadgeTextView.setText("" + count); itemMessagesBadgeTextView.setVisibility(View.VISIBLE); iconButtonMessages.setTextColor(getResources().getColor(R.color.white));
This code uses Iconify-fontawesome .
compile 'com.joanzapata.iconify:android-iconify-fontawesome:2.1.+'
Abdullah Umer Jul 28 '19 at 10:24 2019-07-28 10:24
source share