Have you ever seen a badge like this while using other library ->
Do you want to add that to your own app?
Well, to do this, just use this library.
Sample.mp4
Note: This is not updated to the features supported in the latest version.
- Now you can make the following type of badges with this library.
All the logos will be suppoted only from this and this website
- Add a redirect link on the click
- Change the color of the label of the background of the left part.
- Add a custom logo from Base64.
It is very easy to add it to your project. Just add this to your settings.gradle
file:
maven {url 'https://jitpack.io'}
Then add it to your root level build.gradle
file:
implementation 'com.github.sambhav2358:BadgeGenerator:Latest-Version'
And if you dont feel this convenient, you can download the .aar
file from here and then add it to your app.
- XML
<com.sambhav2358.badgegeneratorlibrary.BadgeView
android:id="@+id/badge"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="15dp"
android:layout_weight="1"
app:badgeLabel="This is a sample label"
app:badgeMessage="This is a sample message"
app:redirectUrl="links.net"
app:logoImage="whatsapp"
app:logoBase64="myBase64Image"
app:logoWidth="25"/>
NOTE: This libraray does not support to change the badge color from xml.
- Java
binding.badge.setColor(binding.colorED.getText().toString());
binding.badge.setMessage(binding.messageED.getText().toString());
binding.badge.setLabel(binding.labelED.getText().toString());
binding.badge.setType(BadgeType.social);
If you want to get the input of BadgeType
from edittext or something for the type, you can do like this
binding.badge.setType(BadgeType.startText + ed.getText().toString());
The default supported badge types are
You can now also add redirect links. In this, you provide the link, and then it will open the browser and you can view the webpage. You can provide a url like this.
binding.badge.setLink("https://stackoverflow.com");
Note: Do not add OnClickListener when you are adding a redirect link. This will override the redirect task.
You can now change the color of the of the right side part of the message part like this
binding.badge.setLabelColor("#abcdef");
// You can also use the predefined ones like this
binding.badge.setLabelColor(BadgeColor.blue);
You can also add a logo now(Added on 2 March 2022)
binding.badge.setLogo("anta");
All the logos will be suppoted only from this and this website
The logos provided in those links might not help you. So, you can also use a custom logo which is encoded as a Base64
string.
It works something like this
binding.badge.setCustomLogo("your base64 string.");
Sometimes you might not have a image in Base64
. This library does it for you. Just use the Base64Converter
class. Something like this.
binding.badge.setCustomLogo(Base64Converter.getBase64FromBitmap(myBitmap));
You can convert the following types of files to base 64. More coming in the future.
- Uri
- Bitmap
- File
- FilePath
Also you can modify the tint of the logo like this
binding.badge.setLogoColor("aaa");
// You can also use the predefined ones like this
binding.badge.setLogoColor(BadgeColor.yellow);
Also you can change the width of the logo like this
binding.badge.setLogoWidth("40");
You can change the background color of the right side part or the message part Something like this
binding.badge.setColor(BadgeColor.blue);
You can also use custom colors for the badge like this
binding.badge.setColor("#FF6200EE");
The default colors present in the BadgeColor
class are:-
- brightGreen
- green
- yellowGreen
- yellow
- orange
- red
- lightGrey
- blue
- And a lot more. Refer to the BadgeColor to see all the available colors.
Also sometimes you might want to save the badge. You can save it the following types
- PNG
- SVG
Downloading SVG
binding.badge.downloadBadgeSVG(Environment.DIRECTORY_PICTURES);
Download PNG
binding.badge.downloadBadgePNG(Environment.DIRECTORY_PICTURES);
Now you dont want to add all these properties and use it, just provide the url and the library does the rest for you.
Note: You should only provide a valid url. You can refer to this website and try all the possible combination. Providing a wrong url will not load thw badge.
Eg:
binding.badge.makeBadgeFromUrl("https://img.shields.io/github/downloads/sambhav2358/BadgeGenerator/total");
To get the url, you need to follow these steps.
- Open the shiels.io website.
- Pick any of the category
- Then i will pick any type. Here i choose
GitHub release (latest by date)
And provide the details.
7. Done. Now the badge will be loaded
When we use a picture, it cannot be converted to svg. So, the the badge is made in png form. This decreases the image quality
I have no ideas of what to add next. Please suggest me by adding an issue.
For everyone who wants me to add a feature, just create an issue with the feature you want.
Free to use and modify the code.
Build with ❤️ by Sambhav Khandelwal