Android Question Changing Label Value with number not working

aaronk

Well-Known Member
Licensed User
Hi,

I can't work out why this is not working and hoping someone can work it out..

First of all I am loading a layout which has 3 labels and 6 buttons in it.

What I am trying to do is adjust the label value as you press on the buttons.
When the Activity loads the default values are:
Hours = 0
Mins = 0
pmam = "AM"

I am using the following code to adjust the values..

B4X:
Sub hour_up_Click
    If hours.Text = 23 Then Return
    hours.Text = hours.Text + 1
End Sub
Sub hour_down_Click
    If hours.Text = 1 Then Return
    hours.Text = hours.Text - 1
End Sub
Sub mins_up_Click
    If mins.Text = 59 Then Return
    mins.Text = mins.Text + 1
End Sub
Sub mins_down_Click
    If mins.Text = 0 Then Return
    mins.Text = mins.Text - 1
End Sub
Sub ampm_up_Click
    If pmam.Text = "PM" Then Return
    pmam.Text = "PM"
    hours.Text = hours.Text + 12
End Sub
Sub ampm_down_Click
    If pmam.Text = "AM" Then Return
    pmam.Text = "AM"
    hours.Text = hours.Text - 12
End Sub
Problem I have is when I press the buttons to adjust the value it's changing it to 0.1, 0.2 etc.

(hours, mins, pmam are all labels that are created with the designer script and loaded to the Activity from the Activity_Create Sub)

Any ideas on what I am doing wrong ?
 

aaronk

Well-Known Member
Licensed User
Can you upload your project (File - Export as zip)?
See the attached file..

You will notice when you tap on the + and - buttons it should count up like 1, 2, 3 etc. however it will count like 0.1, 0.2, 0.3 etc.
 

Attachments

aaronk

Well-Known Member
Licensed User
I think I might of worked it out..

In my Sub Globals I put:
B4X:
Dim hour_value As Int = 0
Dim min_value As Int = 0
Then I changed the value using this:
B4X:
Sub hour_up_Click
    If hours.Text = 23 Then Return
    hour_value = hour_value + 1
    hours.Text = hour_value
End Sub
Sub hour_down_Click
    If hours.Text = 1 Then Return
    hour_value = hour_value - 1
    hours.Text = hour_value
End Sub
Sub mins_up_Click
    If mins.Text = 59 Then Return
    min_value = min_value + 1
    mins.Text = min_value
End Sub
Sub mins_down_Click
    If mins.Text = 0 Then Return
    min_value = min_value - 1
    mins.Text = min_value
End Sub
Sub ampm_up_Click
    If pmam.Text = "PM" Then Return
    pmam.Text = "PM"
    hours.Text = hours.Text + 12
End Sub
Sub ampm_down_Click
    If pmam.Text = "AM" Then Return
    pmam.Text = "AM"
    hours.Text = hours.Text - 12
End Sub
Now it works like it should, however still don't know why the other way isn't working.
 

Mahares

Well Known Member
Licensed User
Now it works like it should, however still don't know why the other way isn't working.
I downloaded and tested your project on a Galaxy tab2 with OS 4.2.2 and it works like it is supposed to, without any issues. The numbers increase or decrease by one. It is odd you were getting the behavior you were.
 

aaronk

Well-Known Member
Licensed User
I downloaded and tested your project on a Galaxy tab2 with OS 4.2.2 and it works like it is supposed to, without any issues. The numbers increase or decrease by one. It is odd you were getting the behavior you were.
I am using my Nexus 5 running Android Lollipop, so maybe it doesn't work correctly with Android Lollipop.

You should change your code to use NumberFormat:
B4X:
mins.Text = NumberFormat(mins.Text + 1, 0, 0)
This seems to fix it, thanks heaps.
 
Top