2

What I want to do is simple. I want to be able to draw a message box to the screen that wraps the height of the box to the size of the text within it.

I am using android's canvas objects to do this, but I do not know how this can be done with this library.

I am trying to reproduce this:

enter image description here

I want to be able to change the text that is within the banner and have it wrap the height. Can someone tell me how this is done?

EDIT

This is what I have currently. There is a lot of math and it barely works for one resolution let alone all of them.

        int x = (int) SCREEN_WIDTH / 2, y = (int) ((SCREEN_HEIGHT / 1.3f) + messageBox.getPaint().getTextSize() + 10);
        String[] message = messageBox.getMessage().split("\n");
        for (String line: message)
        {
            System.out.println(message.length);
              canvas.drawText(line, x, y, messageBox.getPaint());
              y += -messageBox.getPaint().ascent() + messageBox.getPaint().descent();
        }

and

private void resizeBox() {
    if(message == null) 
        return;

    String[] msg = message.split("\n");

    this.bottom += (this.getPaint().getTextSize() + 10) * msg.length;

}
Kyle Jensen
  • 419
  • 9
  • 27
  • you can try using framelayout to show a view on top of your canvas. frame layout will allow you to have layers of view. refer [this blog](http://www.onlymobilepro.com/2012/05/22/android-frame-layout-for-absolute-beginner/) for implementation details. – Rahul Tiwari May 19 '15 at 07:22
  • Unfortunately I dont think this is what I need. I am not using XML within my application, I just draw RectF's and draw Text using the canvas. – Kyle Jensen May 19 '15 at 11:41
  • in that case you have to calculate height of your text and adjust your view bounds. [This question](http://stackoverflow.com/questions/3654321/measuring-text-height-to-be-drawn-on-canvas-android) might help you. – Rahul Tiwari May 19 '15 at 11:53
  • I have tried implementing the following solution but it still does not stay within the bounds of the rectangle its supposed to – Kyle Jensen May 19 '15 at 16:27
  • 1
    can you post here what you have tried? – Rahul Tiwari May 19 '15 at 17:02
  • Ive basically strayed away from that and tried a more static approach and it still is not even working. There must be an easy way to do this or else why do people use android =.= The example you gave seems like it could be on the right track, but I have no idea how to use it. Could you explain? – Kyle Jensen May 20 '15 at 00:49
  • why are you not using xml? why are you drawing RectF's? The decision to use Android or not is very rarely based on the fact you can (or cannot) wrap arbitrary sentences in a drawn rectangle on a canvas. In short you have decided to give yourself a problem that cannot be resolved, why would anyone do that? As soon as you start writing your own code to calculate the size of windows you know you are going in completely the wrong direction. – Hector May 21 '15 at 03:39

1 Answers1

2

I am not using XML within my application

Has someone given you the impression that using XML within [an Android] application is a choice?

It's not.

What you are trying to accomplish would take barely 5 minutes if you use the tools at your disposal.

There must be an easy way to do this or else why do people use android =.=

No idea what you mean by this.

I just draw RectF's and draw Text using the canvas

Okay, then:

enter image description here

In this image, the raw text is:

"Oops!\n\n" +
"Highscore: 59\n\n" +
"Your score: 12\n\n" +
"Better luck next time :(\n" +
"Touch the red box to try again."

As you can see, the line-widths fit comfortably in the given space - lines don't wrap.

In the next image, the last line-break (\n) has been removed to give you an example of what happens when line-widths do not fit inside the screen width:

enter image description here

Text for the second image is:

"Oops!\n\n" +
"Highscore: 59\n\n" +
"Your score: 12\n\n" +
"Better luck next time :( Touch the red box to try again."

We're using StaticLayout to render the text here: Link

StaticLayout takes care of all the details required to draw text on Canvas. Here's what the onDraw(Canvas) looks like:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mStaticLayout == null) {
        mStaticLayout = new StaticLayout(mText, mTextPaint, getWidth(),
            Layout.Alignment.ALIGN_CENTER, 1f, 0f, true);
    }

    canvas.save();

    // Go to where we need to draw this text
    canvas.translate(0, getHeight() / 2 - mStaticLayout.getHeight() / 2);

    // 'mBoundingBoxPaint' is an instance of 'Paint'
    mBoundingBoxPaint.setColor(Color.BLACK);
    mBoundingBoxPaint.setAlpha(100);

    // Draw the bounding box *before* drawing the text
    canvas.drawRect(0, /* paddingTop */-50, mStaticLayout.getWidth(),
                      mStaticLayout.getHeight() + /* paddingBottom */50, 
                      mBoundingBoxPaint);

    // Draw the text
    mStaticLayout.draw(canvas);
    canvas.restore();
}

You can read more on working with StaticLayout at the link given above. Or, you can go through its source code to get a better understanding of how it renders the text.

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Thank you so much! This was a perfect explaination of what needed to be done. I havent really ever had someone to teach me and am quite new to android so I didnt know if you had to develop in XML. Again thank you for the well thought out answer! – Kyle Jensen May 21 '15 at 11:59
  • @KyleJensen No problem, Kyle. If you have any more questions, you can leave a comment here. I don't visit the site regularly - so, my response can take some time. Good luck. – Vikram May 29 '15 at 02:24