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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.