Android canvas drawText de derecha a izquierda

I have an app that handels Arabic too, but my Arabic users have a problem that the drawText flip the word .. Arabic must be from right to left. How do I make the canvas drawText from right to left?

enter image description here

See in the picture the highlighted text is the right text its a textView and it's fine. But the canvas DrawText the one in a circle is wrong. It must be from right to left, how do I make the canvas drawText from right to left?

preguntado el 25 de agosto de 12 a las 12:08

thanks for adding the image, cuz i didn't have permission to add one x.x -

can you show your code onDraw() how your drawing.. -

i don't have onDraw() .. i use drawText: c.drawText(txt, (float) (c.getWidth()/2.7), (float) (c.getHeight()/2 + paint.getTextSize()/1.5), paint); -

txt is my string name .. which is being used by the textview and the canvas, in the textview ( the highligted ) its properly, but in the canvas (the circle ) it must be Right to Left -

if you use canvas.drawTextOnPath() you can draw text for your required direction.. -

4 Respuestas

On the canvas just create two points on sides where you want to draw text, and then create path between them. use this method it will work fine

Path path = new Path();
Paint paint = new Paint();
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x, p1.y);
canvas.drawTextOnPath(String.valueOf(txt), path, (float) (c.getWidth() / (2.3)),  (float) (c.getHeight()/2 + paint.getTextSize()/1.5), paint);

Respondido 25 ago 12, 14:08

thanks, but how do I create a point so I insert it replace of p2 and p1 cuz I get an error from the p's - Stv. Austin

Don't worry about the Point object if you don't understand it. Both of the methods above use two floating point variables so just use those or constants. e.g: path.moveTo(106,80); path.lineTo(3,80); - mimicocotopo

Make sure that Android emulator that contains the Arabic language, I had the same problem but when I tried the application on an actual mobile device,It solved. There are no problems in your application in the language, make sure Android emulator supports the Arabic language

Respondido 31 ago 12, 06:08

you can get subString from your string and draw in your canvas:

            Paint textPaint = new Paint();

            String subString = mString;
            float textWidth = textPaint.measureText(mString);

           int endOffset = Math.round(rectWidth * (mString.length() - 1) / textWidth);
            if (textWidth > rectWidth) {
                endOffset =endOffset - 2;
                subString = mString.substring(0, endOffset);
                subString = subString + "..";
                for(int j=mString.length();j<endOffset+1;j++){
                    subString+=" ";

            canvas.drawText(subString, padding , (float) (startHeight + eachHeight / 3 + textPaint.getTextSize() / 1.5), textPaint);

in this way we have a same result even in RTL or LTR string .

Respondido 12 Jul 16, 08:07

If your target device is api level greater than 11 you can use rotateY=180 attribute in TextView element. Also the parent view should set to rotateY = 180.

respondido 18 mar '14, 07:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.