Start a Project

Creating custom drawables in Android

We all know that we can create so many custom drawables in android but when it comes for a dynamic one we still got the goosebumps. So last week I got stuck in one such issue where I want to show a drawable next to my menu item in navigation drawer.

The drawable was a simple one in which I have to create a circular square and in the center there will be the first character of the menu item text. I know its easier to create an image for them and add it but my menu item is the dynamic one coming from server so it can probably change with some changes on server. I know now you will think to get the image from server and put the image in the server but I don’t think that’s a optimal solution as for this simple work my designer have to create a new image each time something is changed on server.

That is why I took the hard road and creates a dynamic drawable for my need and I can tell you that it was pretty much simpler. So I had to make a view like

I started with creating a Drawable class with name “DrawerIcon.java“. It is responsible to create the icon we just have to pass all the required data to this class’s

Now from the your activity where you have created the navigation drawer you have to call the static method createIcon() and rest it will handle itself.

The LayerDrawable drawer_icon_drawable in defined in drawable folder as

rounded_rextangle_grey.xml

And that is all you will have the required view ready for you.

 

Exit mobile version