2

I've implemented a View with a Text.Layout and I'm drawing it but I'm getting nothing. Here is my code

public class MyEfficientTextView extends View {
    Layout textLayout;
    SpannableStringBuilder spannableStringBuilder;
    int width = 0, height = 0;
    TextPaint textPaint;
    CharSequence text;

    public MyEfficientTextView(Context context) {
        this(context, null, 0);
    }

    public MyEfficientTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyEfficientTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        spannableStringBuilder = new SpannableStringBuilder();
    }

    public void setText(CharSequence charSequence) {
        text = charSequence;
        spannableStringBuilder.clear();
        spannableStringBuilder.append(text);
        textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
        textPaint.setColor(getResources().getColor(R.color.textColor));
        textLayout = new StaticLayout(spannableStringBuilder, textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w != width) {
            width = w;
            setText(text);
        }
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        if (textLayout != null) {
            canvas.translate(getPaddingLeft(), getPaddingTop());
            textLayout.draw(canvas);
        }
        canvas.restore();
    }
}

this code is just for testing if it's working or not and it's not efficient. Which part is wrong and causing it to not render text?

Amir_P
  • 8,322
  • 5
  • 43
  • 92
  • whats the size of your view? – pskink Sep 16 '17 at 13:03
  • I'm printing the `width` variable in my view everytime `setText` is calling and it's 912 @pskink – Amir_P Sep 16 '17 at 13:04
  • I checked it with `Log.d` and the text and width in every `onDraw` calling is correct. I'm calling `setText` in my activity but when the size is changed I'm calling it again with the same text it received from activity @pskink – Amir_P Sep 16 '17 at 13:11
  • at the first line of `setText` i have `text = charSequence;`!! @pskink – Amir_P Sep 16 '17 at 13:13
  • so is `onDraw` called after calling `setText`? – pskink Sep 16 '17 at 13:17
  • btw just used `MyEfficientTextView` and it draws the text with no problems – pskink Sep 16 '17 at 14:01
  • how did you implemented it? can you create a gist with both java and xml codes? @pskink – Amir_P Sep 16 '17 at 16:39
  • `MyEfficientTextView v = new MyEfficientTextView(this); setContentView(v); v.setText("Saga de fidelis boreas, visum demissio! Urbs fatalis hibrida est. Ventus placidus saga est.");` – pskink Sep 16 '17 at 16:41
  • thanks i think i found the problem. when i'm adding it in xml with `match_parent` width and `wrap_content` height it's taking whole of the screen's height. do you know why? @pskink – Amir_P Sep 17 '17 at 06:57
  • because this is how `View#onMeasure` measures the view by default, whats wrong with that it is taking the whole screens' height? it would be worse if the height be 0... – pskink Sep 17 '17 at 07:02
  • you're right. thanks for your helps @pskink – Amir_P Sep 17 '17 at 07:42

1 Answers1

0

you should override onMeasure and set the view width/height. your example code will result in default width/height (probably 0).

Siyamed
  • 495
  • 2
  • 11