How to Put Text in a Drawable

Can we add text to a drawable?

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.

Simplest way to put text on drawable programmatically

public BitmapDrawable writeOnDrawable(int drawableId, String text){

Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextSize(20);

Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);

return new BitmapDrawable(bm);
}

Use this method.will help you.

Add text to android drawable xml

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

How to put text in a drawable?

I've read the book "Professional Android 2 Application Development" (by Reto Meier). Amongst others, it contains an example project where you create a simple compass application where you "draw" text, markers etc.

The brief explanation is that you create a class that extends the android.view.View class and overrides the onDraw(Canvas) method.

All the source code form the book is available for download here: http://www.wrox.com/WileyCDA/WroxTitle/Professional-Android-2-Application-Development.productCd-0470565527,descCd-DOWNLOAD.html. If you download the code and look inside the project named "Chapter 4 Compass", I believe you would find what you're looking for :)

How to insert drawables in text

You can create SpannableString and add any object to your string

TextView textView = (TextView) findViewById(R.id.textView);

ImageSpan imageSpan = new ImageSpan(this, R.drawable.ic_launcher);
SpannableString spannableString = new SpannableString(textView.getText());

int start = 3;
int end = 4;
int flag = 0;
spannableString.setSpan(imageSpan, start, end, flag);

textView.setText(spannableString);

How to add text to a drawable

Thanks for the link. Very helpful.
Turns out i was not declaring my Bitmap correctly.

This solved the problem

            picIV.buildDrawingCache();
Bitmap bitmap= picIV.getDrawingCache();

Its a deprecated method, but it works.

How to center text and align drawable to the left?

I saw an answer posted here which has been deleted since. That solution should work for you.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"
android:drawableLeft="@mipmap/ic_launcher"
android:textAlignment="center"
android:textSize="20sp"
android:id="@+id/toolbar_title"
android:layout_gravity="center"
android:layout_marginRight="10dp"/>

The last line is what's supposed to do the trick. Change the margin value from 10dp to whatever works best for the image you are using.

Or

You could use an ImageView anchored to a TextView to get the same result. But you will have more control in this case as the size of drawable will not affect the alignment of the text.

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="someText"
android:layout_gravity="center"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_anchor="@id/toolbar_title"
android:layout_gravity="start|center"
app:layout_anchorGravity="start|center"/>

</android.support.design.widget.CoordinatorLayout>

And this is how the output looks like :

Sample Image

Edit

I now realize that the problem you are facing is probably due to an offset(?) that appears when using a ToolBar. This answer provides a good solution for that. It goes like this

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">

</android.support.v7.widget.Toolbar>

<TextView
android:id="@+id/toolbar_title"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_toLeftOf="@id/toolbar_title"/>

</RelativeLayout>

Over here I am not placing the TextView and ImageView inside the Toolbar as it would again offset it slightly to the right. Instead I am placing it over the Toolbar



Related Topics



Leave a reply



Submit