[英]In Android Action Bar how to add notification count?
I created a app with action bar.In my action bar i have Flag Notification menu. 我创建了一个带有动作栏的应用程序。在我的动作栏中,我有Flag Notification菜单。 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. 请帮助任何人来解决此问题。
Thanks in advance.Sorry for my bad english :( 在此先感谢您,我的英语不好,抱歉:(
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.
试试这个,它可以在我的MOTO e2上使用。 Make sure ur using above API 14
确保您使用上述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 在onCreateOptionMenu中添加代码
@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 您可以从这里获取参考: https : //github.com/cvoronin/ActionBarMenuItemCounter
I copy this answere from How to display count of notifications in toolbar icon in android 我从如何在Android的工具栏图标中显示通知数中复制此应答
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;
}
<!-- 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.