android show dot character in StaticLayout - android

I would like to show a dot character "\u2022"
in a StaticLayout inside a custom view,
how it can be done?
TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
CharSequence text = "test \\u2022 test";
int textWidth = paint.measureText(text, 0, text.length());
StaticLayout layout = new StaticLayout(text, paint, (int) textWidth, Layout.Alignment.ALIGN_CENTER, 1f, 0f, true);

Related

Android Wear(Long Textview)

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);
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();
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

Drawing HTML with Paint

I'm trying to draw text on a Google Map Fragment by generating a bitmap and placing it as a marker. However the text needs to be stylized via HTML. I was wondering if this is possible.
public static Bitmap createPureTextIcon(String text){
Paint textPaint = new Paint();
//textPaint.setTypeface(TypefaceUtil.get(m))
textPaint.setTextSize(MAP_DISPLAY_TEXT_SIZE);
textPaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.LEFT);
float baseline = -textPaint.ascent(); // ascent() is negative
int width = (int) (textPaint.measureText(text) + 0.5f); // round
int height = (int) (baseline + textPaint.descent() + 0.5f);
CharSequence m = ViewUtil.noTrailingwhiteLines(Html.fromHtml(text));
//height * 2
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(m.toString(), 0, baseline, textPaint);
//canvas.translate(0, height*4);
return image;
}
Example HTML
I mainly need it to render the <br />
<p style=\"text-align: center;\">Looking<br />at Earth </p>
Use a StaticLayout
StaticLayout layout = new StaticLayout(Html.fromHtml(m.toString()), textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
layout.draw(canvas);
Try canvas.drawText(HTML.fromHtml(text), 0, baseline, textPaint); fromHTML converts a string of HTML into a Spannable that can be drawn.

Draw text inside a filled rectangle using Canvas Android

How to draw a filled rectangle with specified bounds and inside that rectangle text to be drawn using Canvas Android ?? I tried
mPaint.setColor(Color.GREEN);
canvas.drawText(mText, x, y, mPaint);
mPaint.setColor(Color.BLACK);
canvas.drawRect(x, y, x + w, y + h, mPaint);
but text is not inside of that rectangle. Can any buddy tell me how to draw a rectangle surrounding specified text with consideration of text size ??
Here i have hardcoded x and y values. You can change them
mpaint= new Paint();
mpaint.setColor(Color.RED);
mpaint.setStyle(Style.FILL);
paint2= new Paint();
paint2.setColor(Color.GREEN);
paint2.setTextSize(50); //set text size
float w = paint2.measureText(s)/2;
float textSize = paint2.getTextSize();
#Override
protected void onDraw(Canvas canvas) {
paint2.setTextAlign(Paint.Align.CENTER);
canvas.drawRect(300-w, 300 - textsize, 300 + w, 300, mpaint);
canvas.drawText(s, 300, 300 ,paint2); //x=300,y=300
}
Edit :
Its bad a idea to call measureText in onDraw. You can do that outside of onDraw.
There is a video on also about performance and why you should avoid allocations in onDraw. https://www.youtube.com/watch?v=HAK5acHQ53E
Resulting snap shot
If you have to center the text inside de rect you have use this code
mpaint= new Paint();
mpaint.setColor(Color.RED);
mpaint.setStyle(Style.FILL);
paint2= new Paint();
paint2.setColor(Color.GREEN);
paint2.setTextSize(50); //set text size
float w = paint2.measureText(s)/2;
float textSize = paint2.getTextSize();
#Override
protected void onDraw(Canvas canvas) {
paint2.setTextAlign(Paint.Align.CENTER);
Rect rect = new Rect(300-w, 300 - textsize, 300 + w, 300);
canvas.drawRect(rect, mpaint);
canvas.drawText(s, rect.centerX(), rect.centerY() ,paint2); // center text inside rect
}
This might be very late for this particular query but I think many will find this answer useful. So, the problem with the Canvas for any CustomView is that, you can get the width for a particular text, but it's not that easy to get the height of the text. Also if you are using canvas.drawText(....) with simple Paint object, you can not draw multi line text. So, use the below code within your onDraw() method.
String displayText = "Hello World";
int mainTextPositionX = getWidth() / 2 ;
int mainTextPositionY = getHeight() / 2;
StaticLayout textStaticLayout;
TextPaint textPaint;
textPaint = new TextPaint();
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setColor(Color.BLUE);
textPaint.setAntiAlias(true);
textPaint.setTextSize(convertDpToPixel(30, context));
textPaint.setTextAlign(Paint.Align.CENTER);
highlightedRectPaint = new Paint();
highlightedRectPaint.setStrokeWidth(12);
highlightedRectPaint.setStyle(Paint.Style.STROKE);
highlightedRectPaint.setColor(Color.RED);
highlightedRectPaint.setAntiAlias(true);
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
textStaticLayout = StaticLayout
.Builder
.obtain(displayText, 0, displayText.length(), textPaint, (int) textPaint.measureText(displayText))
.build();
}else{
textStaticLayout = new StaticLayout(
displayText, textPaint, (int)textPaint.measureText(displayText), Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
}
Rect highlightedTextBorderRect = new Rect();
highlightedTextBorderRect.top = mainTextPositionY-20;
highlightedTextBorderRect.left = mainTextPositionX-
((int)textPaint.measureText(displayText)/2)-20;
highlightedTextBorderRect.right = mainTextPositionX+
((int)textPaint.measureText(displayText)/2) + 20;
highlightedTextBorderRect.bottom = mainTextPositionY+
(int)textStaticLayout.getHeight()+20;
canvas.save();
canvas.translate(mainTextPositionX, mainTextPositionY);
textStaticLayout.draw(canvas);
canvas.restore();
canvas.drawRect(highlightedTextBorderRect,highlightedRectPaint);
just make sure that, you declare all the objects and variable outside of the draw() method. And this will draw a rectangle outline around the text with multi line support. If you want the rectangle to have a fill, then just use the highlightedRectPaint and change the setStyle(Paint.Style.FILL). Hope that helps.

How to reliably determine the width of a multi line string?

I am trying to calculate the width of a multiline text paragraph. To my knowledge, the only class that can do this in Android is the StaticLayout (or DynamicLayout) class. When using this class i do no get the proper length of my text snippet but rather the measured the dimensions are sometimes smaller and sometimes greater depending on the text size.
So i am basically looking for a way to reliably measure the width of a multiline text string.
The following image shows how the measured width diverges from the actual text length in various text sizes.
The screenshot is created running the the following code in a custom View:
#Override
protected void onDraw( Canvas canvas ) {
for( int i = 0; i < 15; i++ ) {
int startSize = 10;
int curSize = i + startSize;
paint.setTextSize( curSize );
String text = i + startSize + " - " + TEXT_SNIPPET;
layout = new StaticLayout( text,
paint,
Integer.MAX_VALUE,
Alignment.ALIGN_NORMAL,
1.0f,
0.0f,
true );
float top = STEP_DISTANCE * i;
float measuredWidth = layout.getLineMax( 0 );
canvas.drawRect( 0, top, measuredWidth, top + curSize, bgPaint );
canvas.drawText( text, 0, STEP_DISTANCE * i + curSize, paint );
}
}
You could try using get text bounds.
private int calculateWidthFromFontSize(String testString, int currentSize)
{
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextSize(currentSize);
paint.getTextBounds(testString, 0, testString.length(), bounds);
return (int) Math.ceil( bounds.width());
}
private int calculateHeightFromFontSize(String testString, int currentSize)
{
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextSize(currentSize);
paint.getTextBounds(testString, 0, testString.length(), bounds);
return (int) Math.ceil( bounds.height());
}
I had a similar issue where the measured text width was off by a bit, once you set a Typeface to the paint this will go away.
So before you use the paint object in the StaticLayout just set the Typeface:
textPaint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL))
or whatever typeface you want.
Here's a way where you can get the multiline width of any text length. usableButtonWidth can be a given text space. I used usableButtonWidth = width of the button - padding. But any default value can be used.
Using StaticLayout, you can get the height of the text for the usableButtonWidth. Then I just try to reduce the available width by decreasing it by 1px in a loop. If the height increases then we come to know that the previously used width was the maximum permissible.
Return this value as the width of multiline text.
private int getTextWidth(){
if(this.getText().length() != 0){
Paint textPaint = new Paint();
Rect bounds = new Rect();
textPaint.setTextSize(this.getTextSize());
if(mTypeface != null){
textPaint.setTypeface(mTypeface);
}
String buttonText = this.getText().toString();
textPaint.getTextBounds(buttonText, 0, buttonText.length(), bounds);
if(bounds.width() > usableButtonWidth){
TextPaint longTextPaint = new TextPaint();
longTextPaint.setTextSize(this.getTextSize());
if(mTypeface != null){
longTextPaint.setTypeface(mTypeface);
}
StaticLayout staticLayout = new StaticLayout(this.getText(), longTextPaint, usableButtonWidth,
Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
int textLineCount = (int)Math.ceil(staticLayout.getHeight() / bounds.height());
int multiLineTextWidth = usableButtonWidth;
while ((int)Math.ceil(staticLayout.getHeight() / bounds.height()) == textLineCount && multiLineTextWidth > 1){
multiLineTextWidth--;
staticLayout = new StaticLayout(this.getText(), longTextPaint, multiLineTextWidth,
Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
}
return multiLineTextWidth+1;
} else {
return bounds.width();
}
} else {
return 0;
}
}

Android Staticlayout Font Size

Is there anyway to change the font size of a static layout? This what I have so far to display the
text in the correct place to begin with.
int question_text_width = game_question_bg.getWidth();
int question_text_xPos = 100;
int question_text_yPos = 100;
StaticLayout question_text = new StaticLayout(text, text_paint, question_text_width, Layout.Alignment.ALIGN_CENTER, 1.2f, 1.0f, false);
//Position Text
canvas.translate(question_text_xPos, question_text_yPos);
//As a form of setMaxLine
canvas.clipRect(new Rect(0, 0, game_question_bg.getWidth(), game_question_bg.getHeight()));
question_text.draw(canvas);
//Reset canvas back to normal
canvas.restore();
You can set the font size using the TextPaint you pass as an argument when the StaticLayout is created:
TextPaint text_paint = new TextPaint();
float fontSize = 25;
text_paint.setTextSize(fontSize);
StaticLayout question_text = new StaticLayout(text, text_paint, question_text_width, Layout.Alignment.ALIGN_CENTER, 1.2f, 1.0f, false);

Categories

Resources