Question About Labels

rmoore

Member
Licensed User
Longtime User
I am relatively new to B4A and am experiencing a problem with labels. Perhaps one of you B4A experts could put me on the right track.

I have distilled the problem to its essentials in the code below.

The first time the button is clicked, the smaller text appears where I would expect - near the top of the label. When the button is re-clicked, the larger text appears in the label, again positioned as I would expect. On the next iteration however, the smaller text appears again, but this time positioned lower, on the baseline of the larger text, I believe.

I can't find any reference on how to control the vertical position of the text.

B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim lbl As Label
   Dim btn As Button
   Dim b As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
   lbl.Initialize("")
   Activity.AddView(lbl,40%x,40%y,20%x,20%y)
   lbl.Visible = False
   lbl.Color = Colors.Yellow
   lbl.TextColor = Colors.Black
   lbl.Text = "8"
   btn.Initialize("btn")
   Activity.AddView(btn,40%x,80%y,20%x,10%y)
   btn.Text = "Action"
   btn.TextSize = btn.Width/6
End Sub

Sub btn_Click
   lbl.Visible = True
   b = Not(b)
   If b Then      ' Smaller Text      
      lbl.TextSize = lbl.Width / 4
   Else            ' Larger Text
      lbl.TextSize = lbl.Width / 2
   End If
End Sub/CODE]

Thanks in advance for any suggestions.

Ron
 

mc73

Well-Known Member
Licensed User
Longtime User
You can try something like
B4X:
 lbl.gravity=gravity.center
if you're talking about alignment.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In your code you haven't set the original text size.
If you continue pressing the Button the text will then appear every time on the same places.

It is no good practice to set the text size proportional to the Label width (same for the Button).
If you have two devices with almost the same screen size but one device has a density of 1 and the other a density of 1.5 the physical Label size would be almost the same but the text will be 1.5 bigger on the second device because of the different densities.
In your example if you turn the device it changes a lot too.

Best regards.
 
Upvote 0

rmoore

Member
Licensed User
Longtime User
Thanks for the quick replies.

MC73, I couldn't fix my problem with Gravity. Apparently the default Gravity for label text is already Gravity.TOP, which is what I wanted anyway. I tried setting it explicitly, and again immediately after setting TextSize, but it made no difference.

Klaus, I didn't think it relevant to set the TextSize initially, but I tried it anyway as you suggested and it made no difference. Yes, the text does appear at the same place (the line on which the larger text is based) each time, apart from the first time. Once the larger text has been displayed, the smaller text appears at the "same" place. I want to know how I can make the smaller text appear where it did in the first display of the label, ideally without having to re-initialize the label.

I have made a mental note of your comments about the pixel density and the setting of text size, although at the moment I don't understand this area. I am in early days with my Android development and have a lot to learn.

Ron
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
... I didn't think it relevant to set the TextSize initially,...
You are right, no need to set the original text size because the Label is hidden at the beginning I missed this.
On my device and in the Emulator it works OK.
Can you send your project as a zip file (IDE menu Files / Export As Zip) so we could test it.

Attached two screenshots, I just moved the views upwards to get smaller pictures.

About density you should have a look at chapter 5 Screen sizes and resolutions in the Beginner's Guide.

Best regards.
 

Attachments

  • Small.jpg
    Small.jpg
    13.3 KB · Views: 135
  • Big.jpg
    Big.jpg
    13.8 KB · Views: 130
Upvote 0

rmoore

Member
Licensed User
Longtime User
Klaus,

I hadn't run this program before in the B4A emulator, as I had got out of the habit of using it once I discovered how much better it was to test on a real device.

The program works fine in the B4A emulator (apart from some minor text size problems), but not on my two Android devices!

I have a Samsung Galaxy 10" tablet and a Kocaso 7" tablet. Both are running Android 4.0.

I have attached a zip file of my program as you suggested.

Ron
 

Attachments

  • TestLabels.zip
    5.5 KB · Views: 121
Upvote 0

klaus

Expert
Licensed User
Longtime User
I tested your program and it works as expected on:
- Emulator 320 x 480 x 1
- Nexus One 480 x 800 x 1.5
- Asus TF 700 1200 x 1980 x 1.5

So I don't understand what your problem is or what you expect.
Below partial screenshots from the Nexus One.

Without screenshots from your devices it's difficult to help you.

Best regards.
 

Attachments

  • 8_1.jpg
    8_1.jpg
    4.8 KB · Views: 121
  • 8_2.jpg
    8_2.jpg
    6 KB · Views: 119
  • 8_3.jpg
    8_3.jpg
    4.7 KB · Views: 119
  • 8_4.jpg
    8_4.jpg
    6 KB · Views: 125
Upvote 0

rmoore

Member
Licensed User
Longtime User
Klaus,

Thanks for your prompt attention.

My 7" Kocaso is connected via the B4A-Bridge, so I am unable to create screenshots as you have. As I said before, my program works correctly on the B4A emulator, so I have taken photos of my tablet screen. The screens would be the same (also incorrect) on my 10" Samsung Galaxy.

I had a little trouble sizing my .jpg files for the upload, so let me know if I need to remake them.

Ron
 

Attachments

  • Screen1.JPG
    Screen1.JPG
    45.8 KB · Views: 123
  • Screen2.JPG
    Screen2.JPG
    42.9 KB · Views: 126
  • Screen3.JPG
    Screen3.JPG
    52.8 KB · Views: 127
  • Screen4.JPG
    Screen4.JPG
    43.9 KB · Views: 122
  • Screen5.JPG
    Screen5.JPG
    51.9 KB · Views: 119
Upvote 0

rmoore

Member
Licensed User
Longtime User
Klaus,

I tried the DoEvents line, but no luck.

Since you have no problem with my program on any of your devices, and I have a problem with both of my devices from different manufacturers, I am wondering if there is a difference in our two IDEs, or in the way that they have been parameterized.

Presumably you recompiled my source with your IDE? I am new to this system, but perhaps I could send you a compiled version of my program (a .apk file?) to see if there might be a problem with the setup of my IDE?

Ron
 
Upvote 0

rmoore

Member
Licensed User
Longtime User
Klaus,

I tried your apk file but it ran incorrectly on my Samsung. I suppose I might have messed up and run my own version as I am not yet familiar enough with Android to be certain of my actions.

I am attaching my own apk file (renamed for your convenience!) for you to run and let me know the result on your Android.

Ron
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Tried in around 6-7 different devices. Apparently, there was one (a 7"" oem) which reproduced the 'bug'. I think it's a device related issue, the way it handles ICS.
 
Upvote 0

rmoore

Member
Licensed User
Longtime User
klaus,mc73,

Thanks for trying my program on your devices. It seems that I am unlucky in having two devices, both of which exhibit the problem.

I am going to see if I can use Canvas for my purposes.

mc73, what is ICS? Is it something that can be worked around?

Ron
 
Upvote 0
Top