Skip to main content

Android textview outline text



Is there a simple way to have text be able to have a black outline? I have textviews that will be different colors, but some of the colors don't show up on my background so well, so I was wondering if there's an easy way to get a black outline or something else that will do the job? I'd prefer not to have to create a custom view and make a canvas and such.




Comments

  1. You can put a shadow behind the text, which can often help readability. Try experimenting with 50% translucent black shadows on your green text. Details on how to do this are over here: http://stackoverflow.com/questions/2486936/android-shadow-on-text

    To really add a stroke around the text, you need to do something a bit more involved, like this:
    http://stackoverflow.com/questions/1723846/how-do-you-draw-text-with-a-border-on-a-mapview-in-android

    ReplyDelete
  2. I've just been trying to figure out how to do this and couldn't find a good guide online but eventually figured it out. As Steve Pomeroy suggested, you do have to do something more involved. In order to get the outlined text effect, you draw the text twice: once with a thick outline and then the second time we draw the main text over the outline. But, the task is made easier because you can very easily adapt one of the code samples provided with the SDK, namely the one under this name in your SDK directory: "/samples/android-/ApiDemos/src/com/example/android/apis/view/LabelView.java". Which can also found on the Android developer website here.

    Depending on what you're doing, it's very easy to see you will only need to make minor modifications to that code, such as changing it to extend from TextView, etc. Before I discovered this sample I forgot to override onMeasure() (which you must do in addition to overriding onDraw() as is mentioned in the "Building Custom Components" guide on the Android Developer website), which is part of why I was having trouble.

    Once you've done that, you can do what I did:

    public class TextViewOutline extends TextView {

    private Paint mTextPaint;
    private Paint mTextPaintOutline; //add another paint attribute for your outline
    ...
    //modify initTextViewOutline to setup the outline style
    private void initTextViewOutline() {
    mTextPaint = new Paint();
    mTextPaint.setAntiAlias(true);
    mTextPaint.setTextSize(16);
    mTextPaint.setColor(0xFF000000);
    mTextPaint.setStyle(Paint.Style.FILL);

    mTextPaintOutline = new Paint();
    mTextPaintOutline.setAntiAlias(true);
    mTextPaintOutline.setTextSize(16);
    mTextPaintOutline.setColor(0xFF000000);
    mTextPaintOutline.setStyle(Paint.Style.STROKE);
    mTextPaintOutline.setStrokeWidth(4);

    setPadding(3, 3, 3, 3);
    }
    ...
    //make sure to update other methods you've overridden to handle your new paint object
    ...
    //and finally draw the text, mAscent refers to a member attribute which had
    //a value assigned to it in the measureHeight and Width methods
    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent,
    mTextPaintOutline);
    canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
    }


    So, in order to get the outlined text effect, you draw the text twice: once with a thick outline and then the second time we draw the main text over the outline.

    ReplyDelete
  3. The framework supports text-shadow but does not support text-outline. But there is a trick: shadow is something that is translucent and does fade. Redraw a shadow couple of time and all the alpha will get summed and a result is an outline.

    A very simple implementation extends TextView and overrides the draw method. Every time a draw is requested our subclass does 5-10 drawings.

    public class OutlineTextView extends TextView {

    private Typeface typeface;

    public OutlineTextView(Context context) {
    super(context);
    }

    public OutlineTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

    public OutlineTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    }


    @Override
    public void draw(Canvas canvas) {
    for (int i = 0; i < 5; i++) {
    super.draw(canvas);
    }
    }
    }


    <OutlineTextView
    android:shadowColor="#000"
    android:shadowRadius="3.0"
    />

    ReplyDelete
  4. This is the custom TextView I used in VPlayer for subtitle rendering, with outline(border) support. https://bitbucket.org/ABitNo/zi/src/783b6b451ba1/src/me/abitno/zi/widget/view/OutlineTextView.java

    ReplyDelete
  5. So you want a stroke around the textview? Unfortunately there is no simple way to do it with the styling. You'll have to create another view and place your textview over-top, making the parent view (the one it's on top of) just a few pixels bigger - this should create an outline.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex