Help me

smfazel

Member
hi
i don't know English very nice, if my question is not in a suitable place, please move it in a suitable place, just don't remove it

now my question:

i have a button (this name is "B1") and a label (this name is "L1") the default L1.text=0 (i set it in desiner)

i wanna every time I push the button
"B1" A number is added to the "L1.text"

i mean my app is a simple counter

i write this code:

sub B1_click
L1.text=L1.text+1
end sub

its work correctly but the numbers is with a "dot" (1.0, 2.0, 3.0,...)

i just wanna my L1.text show the numbers as simple every time I push the button "B1" (1, 2, 3, ....)

please help me
thanks all
 

Mahares

Expert
Licensed User
Longtime User
Here is the complete code you need:

B4X:
Dim B1 as Button
Dim L1 as Label

L1.text=0

Sub B1_Click
  L1.text=Round(L1.text+1 )  'increment by 1 every time you click the button
End Sub
 
Upvote 0

smfazel

Member
cool

Here is the complete code you need:

B4X:
Dim B1 as Button
Dim L1 as Label

L1.text=0

Sub B1_Click
  L1.text=Round(L1.text+1 )  'increment by 1 every time you click the button
End Sub

cool :sign0098:
its work correctly, but the "L1.text=0" was missing, the "0" for L1.text must set in designer, under the "common properties>text" of "L1"

now 2 important question:

1- when i close app and start it again i wanna load the former number i was count
i mean i want my app remember the latest number i was count before (hint: i set L1.text=0 in designer, will it apply every time i lunch the app? )

2- i have also a button this name is "reset" this button reset my counter to "0", it work correctly but a want it work with a conform widow (yer,no)
i mean i don't wanna my counter reset directly by reset button, i want reset it by a conform window after i push the "reset" button.

:sign0085:

thanks..... a lot
 
Last edited:
Upvote 0

edgar_ortiz

Active Member
Licensed User
Longtime User
No one answer me?

Believe it or not, people work here and if they answer you, is to thank him.

If you want :
1- when i close app and start it again i wanna load the former number i was count
i mean i want my app remember the latest number i was count before (hint: i set L1.text=0 in designer, will it apply every time i lunch the app? )

Store te data in a file, you can check:
http://www.b4x.com/forum/basic4android-getting-started-tutorials/6690-text-files.html#post38928

Good Luck

Edgar
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
This should do what you need. Call the sub loadbtns in the Activity_Resume and call savebtns when you Exit your app.

B4X:
Sub Globals
      Dim sets As Map   'Add these two lines to your globals sub
      sets.Initialize
End Sub

Sub savebtns
      sets.Clear
      sets.Put("Lb1", L1.Text)
      File.WriteMap(File.DirInternal, "lblset.set", sets)
End Sub

Sub loadbtns
      sets = File.ReadMap(File.DirInternal, "lblset.set")
      L1.Text = sets.Get("Lb1") + 0
End Sub
 
Upvote 0

smfazel

Member
This should do what you need. Call the sub loadbtns in the Activity_Resume and call savebtns when you Exit your app.

B4X:
Sub Globals
      Dim sets As Map   'Add these two lines to your globals sub
      sets.Initialize
End Sub

Sub savebtns
      sets.Clear
      sets.Put("Lb1", L1.Text)
      File.WriteMap(File.DirInternal, "lblset.set", sets)
End Sub

Sub loadbtns
      sets = File.ReadMap(File.DirInternal, "lblset.set")
      L1.Text = sets.Get("Lb1") + 0
End Sub

thanks, but it not work correctly, please help me anyone can help me
please answer following questions:
1- how can i call the savebtns when i exit from app?
2- what is the "LB1" and "lblset.set"?
3- i set the L1.text=0 in designer, i mean in designer on the text field for L1, i set the 0 for L1.text, may it apply every time i run the app?
4- you say to me "Call the sub loadbtns in the Activity_Resume", and i copy the loadbtns under the Activity_Resume:
B4X:
Sub Activity_Resume
Sub loadbtns
      sets = File.ReadMap(File.DirInternal, "lblset.set")
      L1.Text = sets.Get("Lb1") + 0
End Sub
End Sub
but i see a error when compiling the app its:
Compiling code. Error
Error parsing program.
Error description: Syntax error.
Occurred on line: 23
Sub loadbtns

please help me anyone
 
Last edited:
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
You had this:

B4X:
Sub Activity_Resume
Sub loadbtns
      sets = File.ReadMap(File.DirInternal, "lblset.set")
      L1.Text = sets.Get("Lb1") + 0
End Sub
End Sub

But it should be this:

B4X:
Sub Activity_Resume
      loadbtns
End Sub

LB1 is just a name for the saved value (The key) in the Map file. You can name it as you like but must look for the same name when you are loading the values back into memory.

You would most times have a button to exit your app. So you would place the savebtns just before the activity.finish command like:

B4X:
Sub ButtonExit_Click
      savebtns
      activity.finish
End Sub
 
Last edited:
Upvote 0

smfazel

Member
I made just a few changes and it works great!!

It's attached.

thanks all
its work really GREAT :sign0098:
but it had a small problem i fixed it:
if you use from back key for exit from app, then the latest number not saved, i fix it with this code:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then                    
        savebtns
    End If
End Sub

but i have a small problem yet:
i add this code to my app:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
   If KeyCode=KeyCodes.KEYCODE_VOLUME_UP Then
      number.Text = "" & (number.Text + 1)
      End If
End Sub
with this code i can use the volume up key for count
it work correctly but every time i press the volume up key, the "Ringer Volume" window open automatically
how can i fix this problem? :sign0085:
thanks all friends...
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Try this code:


B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
      If KeyCode=KeyCodes.KEYCODE_VOLUME_UP Then
            number.Text = "" & (number.Text + 1)
      End If
      Return True
End Sub
Put the return true in the If block, otherwise the sub will consume all key presses and not just the volume up.:)
 
Upvote 0

smfazel

Member
very thanks, but the sound is active yet, every time i press the volume up key i listen the ringer volume sound (diid), what i should do?
 
Upvote 0

smfazel

Member
You should use KeyUp instead of KeyPress for the volume button.

i dont know what you say, but i use your post in this topic:
http://www.b4x.com/forum/basic4android-updates-questions/10027-possible-use-phones-volume-buttons-keys-my-app.html#post55673
its work really great

can you explan how this codes work?

B4X:
Sub Process_Globals
    Dim phone1 As Phone
    Dim previousVolume As Int
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub
Sub Activity_Resume
    previousVolume = phone1.GetVolume(phone1.VOLUME_RING)
    phone1.SetVolume(phone1.VOLUME_RING, 0, False)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    phone1.SetVolume(phone1.VOLUME_RING, previousVolume, False)
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    Select KeyCode
        Case KeyCodes.KEYCODE_VOLUME_UP
            Log("Up")
            Return True
        Case KeyCodes.KEYCODE_VOLUME_DOWN
            Log("Down")
            Return True
    End Select
End Sub
 
Last edited:
Upvote 0

smfazel

Member
Heeeeeeeelp me:


my app have a "settings" key in menu
How i can open a settings screen (with checkBox And Radio button) when i press the settings key in menu?

how can i link the checkbox and radio buttons to app events?

:sign0085:
 
Upvote 0
Top