Skip to main content

Adding Badge (Item Count) to Android Button

https://www.viralandroid.com/2016/01/adding-badge-item-count-to-android-button.html

Badge can be used to show number notification, item/product count, message etc. We can see many applications used badge, not only android app it can be used in ipad, iphone, window and even also in web app. Facebook used badge to count notification, friend request, message etc. and many ecommerce website also used to count number of order product, item added to bucket etc. In this tips, I am going to show how to add badge to android button, image button, imageview and like the same way you can use where you want in your application.

Adding badge in android application is little bit tricky part. There is no any shortcut ways of implementing badge in android application; we have to do our self. Here you will learn how to implement notification alert badge in an android application.

To implementing badge in android app here, I have used relative layouts, buttons and text views.

Related:
Android Toolbar Example: How to Use Toolbar as ActionBar
Android Material Design ActionBar/App Bar
Adding Badge (Notification Count) to Android ActionBar Icon

Android Example: How to Add Badge (Item Count) to Android Button


Following are the different XML file to add a notification alert badge in android button.

Create a new XML file in your app drawable directory and give the name badge_item_count. Following is the complete content of badge_item_count.xml file where I have creates a rectangle with 8dp corner radius.

res/drawable/ badge_item_count.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#f20000" />
<stroke
android:width="2dip"
android:color="#FFF" />
<padding
android:bottom="6dp"
android:left="6dp"
android:right="6dp"
android:top="6dp" />
</shape>
view rawbadge_item_count.xml hosted with ❤ by GitHub

Then you have to modify your app XML layout file to implement badge in android button. Here I have added several RelativeLayoutButtons and TextView. Following is the complete content of android XML layout file.

res/layout/badge_item_notification_count_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<!--Adding Badge (Item Count)/Notification Count to Android Button-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
<RelativeLayout
android:id="@+id/badge_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/relative_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="65dip"
android:layout_height="65dip"
android:background="@drawable/notification_icon" />
</RelativeLayout>
<TextView
android:id="@+id/badge_notification_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/relative_layout"
android:background="@drawable/badge_item_count"
android:text="27"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/badge2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@+id/badge_layout1">
<RelativeLayout
android:id="@+id/relative_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/myButton1"
android:layout_width="65dip"
android:layout_height="65dip"
android:background="@drawable/email_icon" />
</RelativeLayout>
<TextView
android:id="@+id/badge_notification_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/relative_layout1"
android:background="@drawable/badge_item_count"
android:text="11"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/badge3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@+id/badge2">
<RelativeLayout
android:id="@+id/relative_layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/myButton3"
android:layout_width="65dip"
android:layout_height="65dip"
android:background="@drawable/promotion_ic" />
</RelativeLayout>
<TextView
android:id="@+id/badge_notification_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/relative_layout2"
android:background="@drawable/badge_item_count"
android:text="55"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/badge4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/badge_layout1"
android:layout_marginTop="50dp">
<RelativeLayout
android:id="@+id/relative_layout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/myButton4"
android:layout_width="wrap_content"
android:layout_height="65dip"
android:background="#72b332"
android:elevation="4dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:text="Notification Badge Android"
android:textColor="#fff" />
</RelativeLayout>
<TextView
android:id="@+id/badge_notification_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/relative_layout3"
android:background="@drawable/badge_item_count"
android:text="1030"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>



Following is the default content of java activity file.

src/ AndroidNotificationCountBadge.java
//Adding a notification badge to a buttons
package viralandroid.com.androidxmluserinterfacetutorial;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class AndroidNotificationCountBadge extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.badge_item_notification_count_layout);
}
}

Android Example: How to Add Badge (Item Count) to Android Button

Now, run your Adding Badge (Item Count) to Android Button application, you will see like the screenshot above. Make sure you have added icons in drawable folder with appropriate name which is used in XML layout 

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App §   2.4.7  Set the color §   2.4.8  Adding Animations o    2.5  To plot grouped bar chart §   2.5.1  Creating Dataset o    2.6  Get the best of Android plus exclusive deals and freebies in your inbox!

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap then to bytes. Also