Abhi
Abhi

Reputation: 9005

How to add text to drawable in Android

I want to draw some text on image which is in Drawable folder. In my code I am getting drawable in the following way

Drawable drawable = getResources().getDrawable(R.drawable.bubble);

Is there any way to add text through code that is programmatically to this drawable image?

Upvotes: 1

Views: 730

Answers (1)

wollw
wollw

Reputation: 414

I'm not sure how flexible it is but it is possible to use a TextView and give it a background like so:

<TextView  
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My Text"
    android:background="@+drawable/image"
    />

or programmatically with setBackgroundResource or setBackgroundDrawable.

Edit:

With an xml layout file like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#FFFFFF"
    android:text="Hello!"
    android:background="@+drawable/image"
    android:gravity="center"
    />
</LinearLayout>

I get a result like this:

Centered text in an image.

Upvotes: 1

Related Questions