tags:

views:

65

answers:

2

i am drawing text in my custom view in android using canvas.drawtext. i need to change back color, and want text right aligned. for example i want to print the text in a 10, 10, 100, 20 rectangle of color yellow and text color red and right aligned. how can i do that ?

A: 
public void onDraw(Canvas c) {
    String text = "red right-aligned text";
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL_AND_STROKE);

    int rectX = 10;
    int rectY = 10;
    int rectWidth = 100;
    int rectHeight = 20;
    float textWidth = p.measureText(text);

    paint.setColor(Color.YELLOW);
    c.drawRect(rectX, rectY, rectX + rectWidth, rectY + rectHeight, paint);

    paint.setColor(Color.RED);
    c.drawText(text, rectX + rectWidth - textWidth, rectY, paint);
}
Andy Zhang
He asked for help on changing the back colour. I think your assumption ("background color is already yellow") is not good in this case.
Andreas_D
Oops, yes, my mistake. I'll change my response.
Andy Zhang
A: 
gc.setBackground(...)
gc.fillRectangle(...)
gc.setForeground(...)
gc.drawText(...)
Daniel