Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add text to android drawable xml

Tags:

android

xml

I am trying to use an android drawable xml.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <item android:drawable="@color/colorPrimary"/>
    <item>
        <bitmap
            android:src="@drawable/ic_logo_black"
            android:gravity="center"/>
    </item>
    <Text is it possible to insert some text here???/>
</layer-list>

For branding purposes, I would like to insert some text into this xml. (It doesn't even matter to me if the text is translated. I just want it so that whenever I am using this drawable xml, there are some text inserted in an area i specify)

Is this even possible? Can I actually put texts/characters in an drawable.xml? Is the only way for me to modify the image and add in text that way?

Thanks

like image 530
Zhen Liu Avatar asked Aug 10 '17 19:08

Zhen Liu


People also ask

How do you add text in drawable?

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.

How do I add text to Layerlist?

One way to add Texts in your drawable layer-list is by creating a png file of the text and adding it using bitmap.

What is stroke in android XML?

Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color.


2 Answers

Unfortunately, you can't do that, For more Refer here.

like image 66
Muthukrishnan Rajendran Avatar answered Sep 28 '22 10:09

Muthukrishnan Rajendran


Yes you can, if you just want to put some text inside of a button, use TextView with background set to your drawable.

Drawable resource

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/action_map_icon_on_v2" android:state_pressed="true"/>
  <item android:drawable="@drawable/action_map_icon_on_v2" android:state_selected="true"/>
  <item android:drawable="@drawable/action_map_icon_off_v2"/>
</selector>

Layout file

<TextView
            android:id="@+id/action_map"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingTop="5dp"
            android:textSize="12sp"
            android:textStyle="bold"
            android:textAlignment="center"
            android:text="MAP"
            android:background="@drawable/action_map_button"
/>
like image 33
Maciek Mikrut Gmail Avatar answered Sep 28 '22 09:09

Maciek Mikrut Gmail