Share My Creation Tips Calculator

This is my first project developed with Basic4Android
Its a very straight-forward tips calcuator

TipsCalc.png


B4X:
'Tips Calculator - Jim Brown

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim EditBill As EditText
   Dim SpinnerTip As Spinner
   Dim SpinnerNumPeople As Spinner
   Dim LabelBillTotal As Label
   Dim LabelEachPays As Label
   Dim billamount As Double
   Dim percent As Double
   Dim numpeople As Double
   Dim billtotal As Double
   Dim perperson As Double
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("TipsCalcLayout")
   SpinnerTip.AddAll(Array As String("0","5","10","15","20","25","30"))
   SpinnerTip.SelectedIndex=2
   For n=1 To 25 : SpinnerNumPeople.Add(n) : Next
   SpinnerNumPeople.SelectedIndex=1
   EditBill.Text="100.0"
End Sub

Sub SpinnerTip_ItemClick (Position As Int, Value As Object)
   percent=Value
   Calculate
End Sub

Sub SpinnerNumPeople_ItemClick (Position As Int, Value As Object)
   numpeople=Value
   Calculate
End Sub

Sub ButtonCalculate_Click
   Calculate
End Sub

Sub Calculate
   ' need to check if edit box contents are valid
   If Not(IsNumber(EditBill.Text)) Then
      EditBill.Text="0.0"
   End If
   billamount=Abs(EditBill.Text)
   billtotal=billamount+((billamount*percent)/100.0)
   perperson=billtotal/numpeople
   LabelBillTotal.Text=Round2(billtotal,2)
   LabelEachPays.Text=Round2(perperson,2)
End Sub
 

Attachments

  • TipsCalc.zip
    4.8 KB · Views: 641

CharlesR

Member
Licensed User
Hi

I am very new to B4A and I think I may not have the file/folders set up correctly. I chose your tips calculator to work through, but when I load it it gives an error saying that the .bal file cannot be located. I have unzipped the download and can see the .bal file. Can anyone recommend a file structure for project development.

Many thanks
 

mistermentality

Active Member
Licensed User
Hi

I am very new to B4A and I think I may not have the file/folders set up correctly. I chose your tips calculator to work through, but when I load it it gives an error saying that the .bal file cannot be located. I have unzipped the download and can see the .bal file. Can anyone recommend a file structure for project development.

Many thanks

Works fine for me, as long as you unzip the whole zip it should work fine.

B4A creates the needed file structure for you, it makes a Files folder where your included assets (background pictures, media files, etcetera) and when you run or compile your application will create an Objects folder as well which will contain your finished .apk file.

When you load the .b4a file for the tips calculator look to the right of the screen and click the files tab, if you then do not see the .bal file listed it means it has not been unzipped properly or the file was accidentally deleted somehow. But if you see it listed it should be working.

Dave
 

walkz

New Member
Licensed User
Initialization of data

Jim,

I found I was getting an initialization error, if user fails to set data, an error in data and result would occur.

Fixed by changing Sub Activity_Create as shown.

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("TipsCalcLayout")
SpinnerTip.AddAll(Array As String("0","5","10","15","20","25","30"))
For n=1 To 25 : SpinnerNumPeople.Add(n) : Next
EditBill.Text="100.0"
SpinnerNumPeople.SelectedIndex=1
SpinnerTip.SelectedIndex=2
'added
billamount=100
percent=10
numpeople=2
Calculate
End Sub
 

devjet

Member
Licensed User
I just had a look at the Tips Calculator.
When the application is started initially and no parameter is entered, calculation is wrong as it does not seem to take spinner control values selected as default. One has to select each field and then calculate.
Does anyone have a idea how this can be simply corrected?
Is there some "OnLoad" function?

Regards
Hans
 

mistermentality

Active Member
Licensed User
I just had a look at the Tips Calculator.
When the application is started initially and no parameter is entered, calculation is wrong as it does not seem to take spinner control values selected as default. One has to select each field and then calculate.
Does anyone have a idea how this can be simply corrected?
Is there some "OnLoad" function?

Regards
Hans

I think that might be what the post above yours resolves.

Dave
 
Top