Skip to content Skip to sidebar Skip to footer

Android Canvas DrawText From Right To Left

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 fr

Solution 1:

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

Solution 2:

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


Solution 3:

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

            Paint textPaint = new Paint();
            textPaint.setColor(Color.BLACK);
            textPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            textPaint.setTextSize(20);
            textPaint.setTypeface(Typeface.DEFAULT_BOLD);
            textPaint.setStrokeWidth(1);


            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 + "..";
            }else{
                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 .


Solution 4:

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.


Post a Comment for "Android Canvas DrawText From Right To Left"