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:
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:
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


