1

I created a app with action bar.In my action bar i have Flag Notification menu. Whenever my app getting notification i need to highlight with some text.

I mean want to add a small count icon over the flag notification.Also i attached one sample screen below for my expected output.

Please help any one to achieve this problem.

My expected Output

Image

Thanks in advance.Sorry for my bad english :(

GowriShankar
  • 1,654
  • 18
  • 30

4 Answers4

2
  • I recommend you use Toolbar from API 21 instead of action bar. Toolbar let you add view to the bar manually and manipulate it programmatically as a usual view, look to this question, OP used toolbar with inner views. You have to migrate from action bar to toolbar in future, because toolbar is more suitable for MaterialDesign
  • OR look to this question, may be your question is duplicate
Community
  • 1
  • 1
Kirill Shalnov
  • 2,216
  • 1
  • 19
  • 21
  • Thanks for your answer @Kirill Shalnov. What about Action bar?We can't able to achieve using action bar ?Also, if i used Tool bar lower versions will support? – GowriShankar Jan 29 '15 at 12:27
  • @user3807045 yes, it has support to pre-Lollipop, It is more flexible than ActionBar. Updated my answer with link to a similar question, it conntain action bar solution – Kirill Shalnov Jan 29 '15 at 12:29
  • ok @Kirill Shalnov.Thanks for your help.I will take a look for tool bar. – GowriShankar Jan 29 '15 at 12:32
2

I made the same thing , whnever a new notification come counter will increase like as u said cart in shopping apps. Try this, it works on my MOTO e2. Make sure u r using above API 14

Create a layout like:

    <ImageView
android:id="@+id/counterBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/unread_background" />

 <TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="8sp"
android:layout_centerInParent="true"
android:textColor="#FFFFFF" />

In onCreateOptionMenu Add code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);

MenuItem menuItem = menu.findItem(R.id.testAction);
 menuItem.setIcon(buildCounterDrawable(count,  R.drawable.ic_menu_gallery));

return true;
 }

Now Build method for icon :

     private Drawable buildCounterDrawable(int count, int backgroundImageId)        {
      LayoutInflater inflater = LayoutInflater.from(this);
      View view = inflater.inflate(R.layout.counter_menuitem_layout, null);
      view.setBackgroundResource(backgroundImageId);

      if (count == 0) {
       View counterTextPanel = view.findViewById(R.id.counterValuePanel);
        counterTextPanel.setVisibility(View.GONE);
      } else {
     TextView textView = (TextView) view.findViewById(R.id.count);
       textView.setText("" + count);
      }

     view.measure(
          View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
         View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
       view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

          view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
       Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);

       return new BitmapDrawable(getResources(), bitmap);
        }

You can take the reference from here: https://github.com/cvoronin/ActionBarMenuItemCounter

I copy this answere from How to display count of notifications in toolbar icon in android

Community
  • 1
  • 1
Minkoo
  • 439
  • 1
  • 3
  • 16
1

You can try this one as well ::

      public static void setBadge(Context context, int count) {
String launcherClassName = getLauncherClassName(context);
if (launcherClassName == null) {
    return;
}
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", count);
intent.putExtra("badge_count_package_name", context.getPackageName());
intent.putExtra("badge_count_class_name", launcherClassName);
context.sendBroadcast(intent);
 }

 public static String getLauncherClassName(Context context) {

 PackageManager pm = context.getPackageManager();

 Intent intent = new Intent(Intent.ACTION_MAIN);
 intent.addCategory(Intent.CATEGORY_LAUNCHER);

 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfos) {
    String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
    if (pkgName.equalsIgnoreCase(context.getPackageName())) {
        String className = resolveInfo.activityInfo.name;
        return className;
    }
}
return null;
}
Minkoo
  • 439
  • 1
  • 3
  • 16
0
<!-- Create :- res/layout/notification_action_bar_notifitcation_icon.xml -->

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_gravity="center"
                android:clickable="true"
                style="@android:style/Widget.ActionButton">

                <ImageView
                    android:id="@+id/iv_icon"
                    android:src="@mipmap/ic_notifications_none_white_24dp"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:gravity="center"
                    />

    <TextView 
                    android:id="@+id/tv_counter"
                    android:layout_width="16dp"
                    android:textSize="10sp"
                    android:textColor="#ffffffff"
                    android:layout_height="16dp"
                    android:gravity="center"
                    android:text="10"
                    android:layout_alignTop="@id/iv_icon"
                    android:layout_alignRight="@id/iv_icon"
                    android:background="@drawable/rounded_notification_square"/>
            </RelativeLayout>

            <!-- Create :- res/drawable/rounded_notification_square.xml -->

        <?xml version="1.0" encoding="utf-8"?>

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

<!-- Create res/menu/menu.xml** -->

    <item
            android:id="@+id/action_notification"
            android:orderInCategory="100"
            android:title="Notification"
           app:actionLayout="@layout/notification_action_bar_notifitcation_icon"
            android:icon="@mipmap/ic_notifications_none_white_24dp"
            app:showAsAction="always" />

    //After following these steps you are done with notification counter as shown in above figure