9

I want to create a drawable which looks like this.

enter image description here and enter image description here

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.

enter image description here

  • take look on this tutorial http://android-dev-tips-and-tricks.blogspot.com/2012/08/xml-drawables-part-i.html feed me back – mohammed momn Feb 17 '14 at 06:51
  • and this http://sriramramani.wordpress.com/2013/01/15/compound-drawables/ – mohammed momn Feb 17 '14 at 06:52
  • can you post some codes you tried, since actually we can add TEXT to Drawable by adding both drawable and text tpo canvas – Vishal Santharam Feb 17 '14 at 06:58
  • 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. –  Feb 17 '14 at 07:01
  • Use two background as above images with bottom cyan color focus and without cyan color focus and text according in onClick event – Santhosh Feb 17 '14 at 07:09
  • All I see there are black rectangles with text in them and the one on the left has a horizontal blue bar at the bottom. Have you tried simply using `TextViews` and adding the blue bar as a bottom drawable? – Squonk Feb 17 '14 at 07:09
  • No I cant use textviews, because I have to set that drawable as tab background. See updated ques. –  Feb 17 '14 at 07:16
  • If they're ActionBar tabs then this is done for you. – Squonk Feb 17 '14 at 07:17
  • No its not action bar.. Tab widget tab host.. Can we add text to tab widget item via code.? –  Feb 17 '14 at 07:19
  • Yes - you can add text, icon or both and the `TabHost` will also provide a drawable to indicate it has been selected. – Squonk Feb 17 '14 at 07:23
  • Can you please show some example to show that the selected one is highlighted in the same way and the text to the item (Like FlashLight Torch here) is added from the code. Please post that as an answer and I will accept it.. Thanx a lot.. –  Feb 17 '14 at 07:27

3 Answers3

1

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.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
0

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;
}
sourav pandit
  • 8,647
  • 2
  • 19
  • 17
-3

Can we add text to a drawable ?

No, you can't.

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

user3317558
  • 482
  • 2
  • 13
  • What is the meaning of "Alternate option for this is to create a drawable with Text.".? –  Feb 17 '14 at 07:01
  • means just directly have a image which has text init. It will avoid the coding time. – user3317558 Feb 17 '14 at 07:02
  • 1
    of course you can: just extend BitmapDrawable and. override draw method – pskink Feb 17 '14 at 07:16
  • 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. –  Feb 17 '14 at 07:16
  • @AkshatJaiswal No, you dont have to keep two images: see my comment above – pskink Feb 17 '14 at 07:52