1

I found this solution to add outline to Custom TextView https://stackoverflow.com/a/16689513/7767664

@Override
public void onDraw(Canvas canvas) {

    Log.i("LOG", "onDraw textView"); // loop

    final ColorStateList textColor = getTextColors();

    TextPaint paint = this.getPaint();

    paint.setStyle(Style.STROKE);
    paint.setStrokeJoin(Join.ROUND);
    paint.setStrokeMiter(10);
    this.setTextColor(strokeColor);
    paint.setStrokeWidth(strokeWidth);

    super.onDraw(canvas);
    paint.setStyle(Style.FILL);

    setTextColor(textColor); // second call causes infinite loop
    super.onDraw(canvas);
}

it draws ok, but it's looped (onDraw is called infinitely)

loop is caused by calling setTextColor method twice in onDraw, if you call it only once then everything is ok (but you get only one color for text and outline)

how this example can be fixed or what better solution to get the same result?

James Riordan
  • 1,140
  • 1
  • 7
  • 23
user924
  • 5,286
  • 3
  • 35
  • 97
  • 1
    https://stackoverflow.com/questions/39106454/add-opaque-shadow-outline-to-android-textview – Mike M. Nov 18 '17 at 11:52
  • @MikeM. first answer looks similar to example in my question (super.onDraw and color set methods also are called twice), but I'll look at it (copy methods onDraw and methods related to set color to my custom textView) and see if would be the same result (get looped), second answer is stupid (two textviews) – user924 Nov 18 '17 at 12:01
  • The `TextView#setTextColor()` method invalidates the `View`, and is what causes the infinite draw loop in the example you're using. That's why my answer there specifically avoids it. (Yeah, I don't like the other answer, either, but it's the OP's, which is why they accepted it. :-) They still gave me the bounty, though, so I can't complain.) – Mike M. Nov 18 '17 at 12:06
  • @MikeM. ok, I'll answer later when I get it working for my custom textview (have some more code to mix with you solution) – user924 Nov 18 '17 at 12:13
  • @MikeM. p.s. seems many users use this https://stackoverflow.com/a/16689513/7767664 solution (it got 16 votes) and don't know about infinite loop, if you have enough reputation you could edited it - add some comment to explain why it's bad – user924 Nov 18 '17 at 12:18
  • 1
    Yeah, I can't really edit another user's post to change the answer's message, or the code's function. I've left comments about the infinite looping on other answers using similar code, but there's probably at least a couple dozen answers here using that same method. Can't get 'em all. Anyway, anybody who cares, and is paying attention will see your comment there now, and understand. – Mike M. Nov 18 '17 at 12:41

0 Answers0