'Canvas center two different paints drawText align with each other
First of all really sorry for my English. I am new to android studio. I want to center two different canvas drawText align with each other. Currently, I just set an X-direction position to both the text but if changing texts then they are overlapping. Below is the code. Hope it will make some clearance.
Paint address= new Paint();
address.setTextAlign(Paint.Align.LEFT);
address.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
address.setTextSize(TEXTSIZE);
canvas.drawText("Address:", (canvas.getWidth() / 2 - 50), 45, address);
// Get the user address
Paint addressData = new Paint();
addressData.setTextAlign(Paint.Align.LEFT);
addressData.setColor(Color.rgb(112, 119, 119));
addressData.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.NORMAL));
addressData.setTextSize(TEXTSIZE);
canvas.drawText("User address data(varying)", (canvas.getWidth() / 2), 45, addressData);
If there is any other way to do it please do let me know. Thanks in advance.
Solution 1:[1]
Yes, there is a better way, Use Spannables and setSpan from and to indices of the text address and addressData. You can create spans for each of the ways you to style parts of the text. Span Help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Arlak |