Autosize Label based on it's text

Inman

Well-Known Member
Licensed User
Longtime User
Is it possible to stretch a label (increase particularly the height) based on the length of it's text? You know like the Label.AutoSize=True option from VB.
 

manios

Active Member
Licensed User
Longtime User
Any idea if your Code will work on ICS? Because Inman said at the Top that the reflection lib doesnt work correctly with it?

Yes ist works with my Nexus S running 4.0.3
 
Upvote 0

Quillok

Member
Licensed User
Longtime User
Thanks manios for testing :)

Klaus it works fine for me, but I've got a last problem, and I'm not sure if it's easy to solve or I oversee something.

My problem is, that my label should have a backgroundimage. When i gave fixed size to my label i used gravity.fill for it, but now with the autosizing the label is as big as the backgroundimage in it's original size.
My first idea was to create two labels, the first one with the text only, autosize it, then read the size of it, and then create a second one, in the background from the first label, use the size of the front label and then use the image with gravity.fill

But i'm sure that this is not the easiest way to solve this. Maybe someone of you have a better idea?
 
Upvote 0

Quillok

Member
Licensed User
Longtime User
Because when I add the image the size of the label is changed to the imagesize.
But I think I know what you mean, i write the text in the label, autosize it, read it's size, and set the height and width to the values the autosize label has. So the size is fixed and then i could add the backgroundimage width gravity.fill :)

Should work then with no problems, thanks for helping!
 
Upvote 0

thetallone

Member
Licensed User
Longtime User
Label in Upright possition

Is there a possibility to orientate a Label in Upright Position.
eg normal Text is 0°
the labels text should be turned to 90° or to a defined alligned angle
 
Upvote 0

thetallone

Member
Licensed User
Longtime User
label rotate

thanks for the hint!!

instead of using a label, it is a good workaround with a canvas

Dim Canvas1 As Canvas

Canvas1.Initialize(Activity)

Canvas1.DrawTextRotated(DateTime.Date(timestamp_left) ,15dip, 50%y, Typeface.DEFAULT, 15, Colors.White,"CENTER",-90)
 
Upvote 0

trahan625

New Member
Licensed User
Longtime User
Found it

Klaus, can you please help me too. I have the same problem.
The code is
B4X:
Dim DescrHeight as float
....
pnlMovie.AddView(lblDescr,0,0,10,-2)
....
' Reading data from database
lblDescr.Text=Cursor1.GetString("descr")  ' <-- The text is about 100 characters long, not empty
....
' Resizing views to fit into panel
PW=pnlMovie.Width: PH=pnlMovie.Height ' <-- The panel is about 400 x 700
lblDescr.Left= pw * 0.02: lblDescr.Top=ivPoster.Top + ivPoster.Height + Margin
lblDescr.Width= pw - 2 * margin ' <-- Margin is 5px
DoEvents
Reflect.Target=lblDescr
DescrHeight=Reflect.RunMethod("getHeight")
Msgbox(DescrHeight, lblDescr.Height )
and the message box replies (0, -2)

Thank you in advance

adamioan, I know this is couple years to late, but I found this info on the web about the getHeight..

"You have to wait until your view has been instantiated and given its dimensions"

I was doing the same you were, in the Activity Create Routine, What I did was add a button to the activity and in the Click event for that button I added the reflect.runmethod code..

Works great :)
 
Upvote 0

Oswald

Member
Licensed User
Longtime User
vice versa

Hello,
my problem is similar but a little bit different :)
size of the label (vertical and horizontal) is static.
but the text is dynamic, depending of the text length I need to change the textsize to fit it in to the label, with the the maximum textsize.

I need your help
BX
:sign0085:
 
Upvote 0
Top