Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Count Badge in Menu Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.27k
    Comment on it

    This blog is about counter badge on icon of Menu . like counter of Notification.

    Here is code of this tutorial.

    1. Create project.

    2. Create text_circle.xml in res->drawable folder.

    3. Write below code to text_circle.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <corners
            android:bottomLeftRadius="10dp"
            android:bottomRightRadius="10dp"
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp" />
        <solid android:color="@android:color/holo_red_dark" />
    
    </shape>

    4. Create one layout file with name:- counter_menu_item_layout.xml.

    5. Copy below code to xml.

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/BadgeFrameLayout"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:background="@mipmap/ic_launcher">
    
        <RelativeLayout
            android:id="@+id/BadgeRelativeLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:background="@drawable/text_circle">
    
            <TextView
                android:id="@+id/BadgeCount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerHorizontal="true"
                android:padding="2dp"
                android:textColor="@android:color/white"
                android:textSize="10sp" />
        </RelativeLayout>
    
    </FrameLayout>

    6. Create menu folder under res folder.

    7. create menu file in menu folder.

    8. Paste below code to menu xml.

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity">
    
        <item
            android:id="@+id/BadgeAction"
            android:icon="@drawable/ic_notification"
            android:orderInCategory="1"
            android:title="Notification"
            app:showAsAction="always" />
    
    </menu>

    9. Open java class of Activity.

    10. Write Override onCreateOptionsMenu() to Activity.

    11. Paste below code to onCreateOptionsMenu().

     getMenuInflater().inflate(R.menu.menu, menu);
    
            MenuItem menuItem = menu.findItem(R.id.BadgeAction);
            menuItem.setIcon(buildCounterDrawable(appBadgeCount, R.drawable.ic_notification));
    
            return true;

    12. create  buildCounterDrawable().

    private Drawable buildCounterDrawable(int count, int backgroundImageId){}

    13. Paste below code to buildCounterDrawable().

    LayoutInflater inflater = LayoutInflater.from(this);
            View view = inflater.inflate(R.layout.counter_menu_item_layout, null);
            view.setBackgroundResource(backgroundImageId);
    
            if (count == 0) {
                View counterTextPanel = view.findViewById(R.id.BadgeRelativeLayout);
                counterTextPanel.setVisibility(View.GONE);
            } else {
                TextView textView = (TextView) view.findViewById(R.id.BadgeCount);
                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);

     

    result:-

    Happy Coding :D

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: