2

Short question:

Suppose I have a TextView, and I wish that the text itself (not the backround) will have a drawable set to it (and not just a solid color), how should I do it?

The only solution I've found is this one , but it's only for gradient colors, and I wish to be able to use any drawable.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270

2 Answers2

2

Ok, I think I've found a possible solution, but I have some notes about it:

  1. It's not for any drawable. It's for a bitmap, so you will need to somehow convert it to a bitmap.

  2. It requires that you know the size of the textView (which you can use this for this purpose)

  3. It requires that you have enough memory for the scaled bitmap that is in the same size of the textView.

  4. Not sure how to make it work per character or per line.

Here's the code:

public static void setBitmapOnTextView(final TextView tv, final Bitmap bitmap) {
    final TileMode tile_mode = TileMode.CLAMP;
    final int height = tv.getHeight();
    final int width = tv.getWidth();
    final Bitmap temp = Bitmap.createScaledBitmap(bitmap, width, height, true);
    final BitmapShader bitmapShader = new BitmapShader(temp, tile_mode, tile_mode);
    tv.getPaint().setShader(bitmapShader);
}

I hope that there is a better solution for this.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

edited:

I don't believe it's possible to change the actual stroke used on the text, the best closest option you probably have is to use a different font.

In android you can "draw" any text using any *.ttf font.

for that you must include the file in your assets folder and call from code (maybe there's a way using XML, but I don't know how) this:

TypeFace mFont = TypeFace.createFromAsset(mContext.getAssets(), "my_font.ttf");
mTextView.setTypeFace(mFont);

I reckon that is the closes you'll get to have a different stroke on the text.

original answer:

TextView have the following properties:

Drawable Top
Drawable Bottom
Drawable Left
Drawable Right

that you set on the XML. Or you can call

setCompoundDrawables(left, top, right, bottom)

from code!

Budius
  • 39,391
  • 16
  • 102
  • 144
  • I believe that what 'android developer' wanted to ask was that the font / strokes for the text of a textview itself need to be made with a drawable. Your suggested solution would put drawable(s) around the text, but would not solve what was asked. – mastDrinkNimbuPani Apr 09 '13 at 18:35
  • @mastDrinkNimbuPani Yes this is correct. I don't want anything related to drawables around the text, the text should be the one that has the drawable. It's like textures being set to the text. One nice thing could be gradient, but I would also like to be able to use images too. – android developer Apr 09 '13 at 21:06
  • oohh..... I didn't properly understand the question than. I'll edit the answer, please check it. – Budius Apr 10 '13 at 12:10
  • @Budius Using ttf fonts allows you to set exactly how you wish to show each character, by editing the font itself? If so, I wasn't aware of it (was sure it's always black & white "templates") . However, it's still not what I've asked for. BTW, when you edit your answer, please post any comment so that it will notify of the changes. – android developer Apr 17 '13 at 14:22
  • I've never created a font before myself (just get them from design department) but I reckon yes you can (http://fontforge.org/). And I did post a comment mentioning my edit, it's above your comment, 10-Apr 12h10m – Budius Apr 17 '13 at 14:28