Android Question get width of text in a label by reflection

ArminKH

Well-Known Member
Hi
I want measure the width of a text in a label just by using reflection or java object lib
Because by using canvas for persian or arabic language texts(combined by english text or engilsh numbers) some times that return wrong value
Please help i think reflection lib is better for doing this
Tnx for your response
 

stevel05

Expert
Licensed User
Can you provide an example project where the values are incorrect?
 

ArminKH

Well-Known Member
Can you provide an example project where the values are incorrect?
See this screen shot
The text is fit in label and the measure width is for label1.text.trim
2015_01_29_11.34.22.png
 

ArminKH

Well-Known Member
It doesn't matter if you use reflection or the standard methods. Eventually they all call the same native API.
Ok erel but what is the solution?if i add any char to this text,the text width is lower than width of label but the height of text be double
 

ArminKH

Well-Known Member
You should better explain the problem and post the relevant code.
OK Excuse me 4 my english
please see screenshot,my text is one line and is completely fit in label and width of text is equal with width of label
but after measuring width of text by using canvas the result is different(see result in screenshot),and text width is not equal with label's width
i think when the text completely is fit in a view,the text width and view width must be equal.is this wrong?
if you add this character "ـ" to end of text the line count will be 2 and height of text increased but the text width is still less than label's width(tested on 5'' device with density 1.5 and label width is 502 on my device)
and now my problem is this:
if my text is completely fit in label why my measured text width is less than label's width?
and if my text width is less than label's width why my line count and height of text after add an other character are increased?
please see my attached project
tnx again erel and stevel
 

Attachments

Last edited:

ArminKH

Well-Known Member
@Erel
Erel after hours i find out what is wrong
Please see text of label from designer
The characters between چرا and (1 is unusual
That is not space and is similar to add space by using Tab key on keyboard
And after many test now i know the canvas cant measure the width of this special part of text and return 0 for it
Erel i just copy text from ms word app and paste in label.that number(first number in text)is created by ms word auto number creator and i dont know what is that part

And now after many hours i dont know what is solution for solving this problem
Please help because this is necessary for me

Tnx agaaaaaaain
2015_01_29_11.34.22.png
 
Last edited:

stevel05

Expert
Licensed User
If it's a Tab character you can replace it within the string i.e. Str.Replace(Tab," ") or similar
 

ArminKH

Well-Known Member
If it's a Tab character you can replace it within the string i.e. Str.Replace(Tab," ") or similar
I dont know exactly what is that but i think that is similar to tab
Dear stevel my goal is just measuring width of text,when i dont know the width of this part so i dont know replace that with how many space !!!
 

ArminKH

Well-Known Member
And at last i think each tab is equal to 3 space and we can add the width of 3 space to text for each tab
But i dont know this issue just is for tab or not?
 

stevel05

Expert
Licensed User
If it is a Tab, then the value could vary depending on the source of the text. There is no way of knowing what the original size would have been, you'll just have to make a choice, or set a variable in case you want to change it for different sources.
 

ArminKH

Well-Known Member
If it is a Tab, then the value could vary depending on the source of the text. There is no way of knowing what the original size would have been, you'll just have to make a choice, or set a variable in case you want to change it for different sources.
Now i test replacing tab with other characters but my try is lost :( because the tab width is not completely equal with 3 space's width
I think the tab has realy different story
 
Last edited:

stevel05

Expert
Licensed User
Perhaps it's not a Tab character.

Check the Ascii value of the offending character.

B4X:
For i = 0 to str.Length - 1
    Log(Asc(str.CharAt(i)))
Next
 

ArminKH

Well-Known Member
Perhaps it's not a Tab character.

Check the Ascii value of the offending character.

B4X:
For i = 0 to str.Length - 1
    Log(Asc(str.CharAt(i)))
Next
No that is tab because
Label1.Text.Contains(Tab) returned true
But i cant replace it !!!
 

ArminKH

Well-Known Member
@Erel
Any idea? About measuring the width of Tab or other special character ???!!!
Please help me this is necessary for me :(
 
Last edited:

stevel05

Expert
Licensed User
That is a class specific to the Eclipse software and not a generic java class.

Tab is a marker in the text that would be formatted within each application however the application is set up to deal with it. The best you can do is specify how you want it to be dealt within your implementation. I have tested replacing the Tab character, it works as expected here.

See attached.
 

Attachments

Top