0

Hi Guys am drawing an text using canvas, and i need to change the textview everyhour, if the textview extends the width it should be displayed in the next line For example , i have a text like "Good Morning" and "Good Morning rachel ! welcome you"

The first textview Good morning will be correctly displayed in Single line but the second textview i need to print by two line how can i draw it

String mytext = "hi how are you how was the day "
  canvas.drawText(mytext , x, centerX, mTextPaint);
Ed.
  • 304
  • 2
  • 16

2 Answers2

2
scale = resources.getDisplayMetrics().density;

        TextPaint TextPaint=new TextPaint();
        TextPaint.setARGB(255, 255, 255, 255);
        TextPaint.setTextSize(28f);
        TextPaint.setTypeface(myfonttime);
        StaticLayout mTextLayout;
        mTextLayout = new StaticLayout(myText, TextPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

        int textHeight = mTextLayout.getHeight()- (int) (16 * scale +5.0);
        int textWidth = canvas.getWidth() - (int) (16 * scale);
        float x = cWidth / 2f - textWidth/2f  - bounds.left;
        float y = cHeight / 2f - textHeight/2f;

        canvas.save();
        canvas.translate(x, y);
        mTextLayout.draw(canvas);
        canvas.restore();
Edgar prabhu
  • 563
  • 3
  • 21
0

for example you have String like this

String text = "This is\nmulti-line\ntext";

Then draw in textview like this

canvas.drawText("This is", 100, 100, mTextPaint);
canvas.drawText("multi-line", 100, 150, mTextPaint);
canvas.drawText("text", 100, 200, mTextPaint);

Second way.

    TextPaint mTextPaint=new TextPaint();
StaticLayout mTextLayout = new StaticLayout(mText, mTextPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

canvas.save();
// calculate x and y position where your text will be placed

textX = ...
textY = ...

canvas.translate(textX, textY);
mTextLayout.draw(canvas);
canvas.restore();

for more refer this link

Community
  • 1
  • 1
viratpuar
  • 524
  • 1
  • 5
  • 22