Zhen Liu
Zhen Liu

Reputation: 7920

Add text to android drawable 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

Upvotes: 10

Views: 22780

Answers (4)

Thriveni
Thriveni

Reputation: 771

Hi May be this link will help you

https://stackoverflow.com/a/60523334/6494118

where ever you are trying to add the Drawable you can make use of this method.

private Drawable getCircleDrawableWithText(Context context, String string) {

    Drawable background = ContextCompat.getDrawable(context, R.drawable.circle_yellow);
    Drawable text = CalendarUtils.getDrawableText(context, string, null, android.R.color.white, 12);
    Drawable[] layers = {background, text};

    return new LayerDrawable(layers);
}

The resource file for shape or background you can use this following code. You can replace this with different shape. Drawable file circle_yellow will be below which give the shape of circle.

     <?xml version="1.0" encoding="utf-8"?>
     <shape
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="oval">

     <solid
         android:color="@color/yellow"/>

     <size
       android:width="120dp"
        android:height="120dp"/>
     </shape>

Upvotes: 1

findusl
findusl

Reputation: 2644

This guy apparently managed to create a vector drawable with a text. But I don't know how:

How to access or translate text in Vector Drawables

Maybe he used the Inkscape option to turn text to path. I will look deeper into it tomorrow.

Upvotes: 1

Maciek Mikrut Gmail
Maciek Mikrut Gmail

Reputation: 45

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"
/>

Upvotes: 0

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

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

Upvotes: 6

Related Questions