Can we add text to a drawable?

add text in drawable xml android
android drawable list
android draw drawable on canvas
drawable v24
how to store image in drawable in android programmatically
android-drawable color
android:drawable xml with image
android custom drawable

I want to create a drawable which looks like this.

and

I know how to create the image background but even after searching, I am unable to find how to give text to that drawable file. Is there any way that the drawable file is exactly same containing the text as well?

I cant use textview because its a tab and I need drawables to set as background resource (thats what I know, please correct me if I am wrong.). So I want a drawable file which I can set as background.

Yes you can do that.

See the post -

How to put text in a drawable ?

Basically, you have to extend the class Drawable and set the canvas to draw the text to a drawable.

As you override the draw method, it will take the canvas and draw the text on defined locations.

There are many methods available for Canvas.

As explained in a graphics doc. -

The Canvas class has its own set of drawing methods that you can use, like drawBitmap(...), drawRect(...), drawText(...), and many more. Other classes that you might use also have draw() methods. For example, you'll probably have some Drawable objects that you want to put on the Canvas. Drawable has its own draw() method that takes your Canvas as an argument.

Drawing text will be just like the following -

canvas.drawText("Front Screen Torch", 30, 48, paint);

To get the actual color directly from resources use -

paint.setColor(getResources().getColor(R.color.black));

See Canvas for more.

TextDrawable: Draw Some Text!, Recently, I had a need to place text into a Drawable so it could be inserted in In many cases, we can use this to do a job that would otherwise  Basically, you have to extend the class Drawable and set the canvas to draw the text to a drawable. As you override the draw method, it will take the canvas and draw the text on defined locations. There are many methods available for Canvas .

YES you can and Much More With this control over canvas ,I use This alot to To get Full control over the drawable

public static Drawable getTextDrawable(@ColorInt int iColor) {
 Shape shape = new Shape(){

        @Override
        public void draw(Canvas canvas, Paint paint)
        {
            paint.setColor(Color.BLUE);

            paint.setTextSize(100);

            int radii = dpToPx(3);

            canvas.drawText("Hello Canvas", canvas.getWidth() - 150, canvas.getHeight() / 2, paint);

            canvas.drawCircle(canvas.getWidth() - radii * 2, canvas.getHeight() /2 - radii, radii, paint);
        }
    };
    shape.getHeight();

    Drawable drawable = new ShapeDrawable(shape);

    return drawable;
}

Android Drawables - Tutorial, First, to restrict the total number of lines of text we can use android:maxLines Any images stored in the "drawable" folders can actually be embedded within a  With this class, text can now be part of the Drawable world, meaning it can not only be set alone in places where you would normally put an image, it can also be placed together with other Drawables in containers like StateListDrawable or animated with the likes of TransitionDrawable and ClipDrawable. In many cases, we can use this to do a job that would otherwise require multiple views or compound controls just to achieve a given visual effect; thus it can reduce overhead in your view

Can we add text to a drawable ?

No, you can't.

Alternate option for this is to create a drawable with Text.

Working with the TextView, You can add graphics to your app by referencing an image file from your project resources. To use an image resource, add your file to the res/drawable/ and then specifies the view's text, it stretches itself so that all the text  TextViews are one of the basis of Android UI framework. Buttons, EditTexts, RadioButtons are, indeed, TextViews. And they are also one of the most powerful tools we have to create our layouts. Knowing them in deep can help us save time, views and simplify our layouts, making our UI faster. In this series, I will talk about TextView and its direct subclasses.

Drawables overview, Before you can use Material text fields, you need to add a dependency to the Material It is possible to set a custom Drawable as the text field's trailing icon via  GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. This light-weight library provides images with letter/text like the Gmail app. It extends the Drawable class thus can be used with existing/custom/network ImageView classes. Also included is a fluent interface for creating drawables and a customizable ColorGenerator.

Text Fields, Free Android app programming for beginners - adding images. We need to import this picture into the res > drawable folder. In later versions of Android Studio, you can find the Image View control under the Common category of the Palette (and Keep the left mouse button held down and drag it below the Text View:. After add images in drawable folder, you can use them in your android application java code. Below is an example. There are 6 images in it, when application start, it will show image1.png. When you click the image, it will show another image in order.

Adding images to an Android Project, Note You also can use the Class Browser window to browse for and create properties and methods. Wire the Scene:New Object output of the Create Object VI to the reference input of the Set Drawable method. Add the Create Text VI to the block diagram to create a new text object. We can do so by using the SVG Vector Drawable in our project. We saw how to add the Vector Assets in our application. We saw how to add the Vector Assets in our application. In the last, we saw how to change the properties of Vector Asset by altering the XML file generated by the Vector Asset Studio.

Comments
  • take look on this tutorial android-dev-tips-and-tricks.blogspot.com/2012/08/… feed me back
  • and this sriramramani.wordpress.com/2013/01/15/compound-drawables
  • can you post some codes you tried, since actually we can add TEXT to Drawable by adding both drawable and text tpo canvas
  • What I have tried is a png format image as I have pasted here. Since this makes the app apk bigger, I was thinking to use a drawable. So I am asking if we can add text to drawables.
  • Use two background as above images with bottom cyan color focus and without cyan color focus and text according in onClick event
  • What is the meaning of "Alternate option for this is to create a drawable with Text.".?
  • means just directly have a image which has text init. It will avoid the coding time.
  • of course you can: just extend BitmapDrawable and. override draw method
  • Ya but Its increasing the App size as I have to use 2 images for each tab that means around a dozen of images in my drawables.
  • @AkshatJaiswal No, you dont have to keep two images: see my comment above