In the subject of the question, under the auxiliary icon I meant the small one in the lower right corner of the main one. Here is an example: a small icon with a number shows the number of alerts:

enter image description here

I want to ask, naturally, how to add this small icon with a number and program it again so that the necessary icon is displayed depending on the number of notifications, but if you offer a solution, instead of a small icon, the figure drawn by Java means in a circle, this will also work (perhaps even better).


Update

I tried katso solution; The activation code causes the application to crash.

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 

I have already encountered such a problem and on the basis of an explanation

It is called when the class field is initialized; the class field is initialized when an instance of the class is created before calling its constructor. And all this happens long before calling the onCreate method.

declared the variables before the onCreate method and initialize them inside it. Now this error:

enter image description here

Just in case I post the full application code. As an icon ic_bell you can use any image. Some of the code is for some reason not displayed, but it can be copied from the input field that appears when you click "fix".

  • MainActivity.java

    import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

     View count; TextView notifCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); count = menu.findItem(R.id.badge).getActionView(); notifCount = (TextView)count.findViewById(R.id.notif_count); } private int mNotifCount = 0; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); notifCount.setText(String.valueOf(mNotifCount)); return super.onCreateOptionsMenu(menu); } private void setNotifCount(int count){ mNotifCount = count; invalidateOptionsMenu(); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.badge) { return true; } return super.onOptionsItemSelected(item); } 

    }

  • menu_main.xml

     <item android:id="@+id/badge" android:actionLayout="@layout/actionbar_notifitcation_icon" android:showAsAction="always" android:icon="@drawable/ic_bell" android:title="@string/hotlist" /> 

  • actionbar_notification.xml

     <ImageView android:id="@+id/image" android:src="@drawable/ic_bell" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"/> <TextView android:id="@+id/notif_count" android:layout_width="wrap_content" android:minWidth="17dp" android:textSize="12sp" android:textColor="#ffffffff" android:layout_height="wrap_content" android:gravity="center" android:background="@drawable/rounded_square"/> 

  • rouded_square

  • Add a custom view instead of the standard one in the Toolbar - georgehardcore
  • Why don't you want to use the prebuilt libraries from my comment above? They are tested and the implementation of your question will take a couple of minutes from strength .. - pavlofff
  • Because of the same discomfort that ardent opponents of third-party libraries are experiencing. Of course, if this is such a thing as jQuery for JavaScript, then it makes sense to connect, but to connect third-party libraries under each button is somehow not ice ... - Sideways Gleb
  • In general, about them in order to do so that for 4 days not to sit on each button. This is just another class widget, the same as the standard widget (for example, from the SDK or the Support library). That you yourself will ever write it, that connect to the project ready - the result is the same, but of course yours. - pavlofff

1 answer 1

Menu item

menu / menu_actionbar.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/badge" android:title="@string/hotlist" app:actionLayout="@layout/actionbar_notifitcation_icon" app:showAsAction="always" /> </menu> 

button layout

layout / actionbar_notifitcation_icon.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@android:style/Widget.ActionButton" android:layout_width="54dp" android:layout_height="54dp" android:clickable="true"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:src="@drawable/ic_bell" /> <TextView android:id="@+id/notif_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rounded_square" android:gravity="center" android:layout_alignBottom="@+id/image" android:layout_alignRight="@+id/image" android:textColor="#ffffffff" android:padding="4dp" android:textSize="12sp" /> </RelativeLayout> 

Background badge

drawable / rounded_square.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="2dp" /> <solid android:color="#ffff0000" /> <stroke android:color="#ff222222" android:width="2dp"/> </shape> 

In activit:

  private int mNotifCount = 1; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_actionbar, menu); MenuItem item = menu.findItem(R.id.badge); View count = MenuItemCompat.getActionView(item); TextView notifCount = (TextView) count.findViewById(R.id.notif_count); notifCount.setText(String.valueOf(mNotifCount)); return super.onCreateOptionsMenu(menu); } private void setNotifCount(int count) { mNotifCount = count; invalidateOptionsMenu(); } 

example

  • Thank you for the time spent on the decision! There are two problems left and I am sure it will work. 1) Android Studio requires you to create a getSupportMenuInflater method. What should he look like? 2) Where notif_count you define the notif_count ? - Lateral Gleb
  • @GurebuBokofu updated the code, look. getSupportMenuInflater() replaced by getMenuInflater() , a method available in AppCompatActivity ; notif_count is a TextView in layout'e - katso
  • I apologize for the long pause; Unfortunately I could not test your decision for two days. Unfortunately, your activation code causes the application to crash. If it's not difficult for you, please see the question update. - Hokov Gleb
  • @Gurebu Bokofu, show, the error rate. - katso
  • @GurebuBokofu, updated the answer. Replaced notifСount with notif_count, should work. Learn to read the logs, it says for what reason and in which line the error. - katso