Basic4Android: Notepad – Part2 (Code)
This tutorial continues from the last one in the NotePad app set Basic4Android: Notepad – Part1 (Designing). In this tutorial i am taking the design and adding code to make the app load the text from files and display it in one of the four slots.
App start up
When the app starts up we are going to load in the design with the buttons and the text box in. We are also going to put things into the menu. All of this start up code goes inside the Activity create sub.
Sub Activity_Create(FirstTime As Boolean)
'Code to run here
End Sub
Loading the layout
First of all loading the layout, all this is done with one line of code which just loads the file we saved in the first tutorial. We made main.bal in the first tutorial and now we are displaying that.
Activity.LoadLayout("main.bal")
Adding menu items
Adding menu items is very simple, we are going to add three menu items Clear, Save, Exit. These will only appear when the menu button is pressed so it saves on screen space. It is one line of code for each menu item. We are only using text items so the line of code is this:
Activity.AddMenuItem("title", "event")
The title is what you want the menu button to say example “Clear” and the event is where you want to run the code example “clear” which would run the sub “clear_Click”. We are adding the three menu items below:
Activity.AddMenuItem("Clear", "clear")
Activity.AddMenuItem("Save", "save")
Activity.AddMenuItem("Exit", "exit")
Loading the slots
When each slot button is pressed two things will happen.
- The data for that slot will be loaded and displayed.
- The strings will be changed so that when saved the data is saved to the right slot.
We will do the second one first as it is the simplest to do and requires only a few lines of code. First we need to set up the variable to store which slot is open. To do this we Dim the string in the Globals sub. Inside there under the setting up of the buttons and text box add the two lines.
Dim slot As String slot = "0"
That line lets the program know that if slot is used that it is a bit of data. Right under that line we are going to set slot to 0. This is because we don’t want bugs when a user clicks save straight away. Now we need to put a bit of code into each button so that the slot string changes. For example the button for slot 1 will be:Sub slot1_Click slot = "1" End Sub
This needs to be added to all of the other subs, they should have been generated in the first tutorial, so all you need to do is add the second line to each sub. Remember to change the number so that slot2_Click contains slot = “2″ and so on.
Now we need to load the data for each slot. This is very simple to do we just need to load a text file when each button is clicked. this is done in one line, but we are going to make sure the file is there before opening to stop any errors. So under the line above we need to check for a file like this:
If File.Exists(File.DirInternal,"notepadapp-slot" & slot & ".txt") Then
notepadbox.Text = File.ReadString(File.DirInternal, "notepadapp-slot" & slot & ".txt")
ToastMessageShow("Data loaded for slot " & slot & ".", True)
Else
notepadbox.Text = "There is no data for slot" & slot
ToastMessageShow("No data loaded for slot " & slot & ".", True)
End If
That code checks to see if the file notepadapp-slow1.txt is there, if it is then it will load the data into the text box. If not then it will display a message. The slot can still be loaded without there being data, and when it is saved for the first time it will create that file for the next time. That code needs to be put in all the four slot buttons and does not need to be modified.
Saving the slots
saving the slots is very easy, it is again just one line of code to do so. You will need to create a new sub like below with the following code inside. It can be anywhere in the code i added it at the bottom.
Sub save_click
File.WriteString(File.DirInternal, "notepadapp-slot" & slot & ".txt", notepadbox.Text)
ToastMessageShow("Data saved for slot " & slot & ".", True)
End Sub
The Clear button
The clear button just resets the files to default and removes all the data from it, it is only a few lines of code. it sets the text box to having no data and then writes that to the file. A new sub is need for the clear button just like with save.
Sub clear_click
notepadbox.Text = "There is no data for slot" & slot
File.WriteString(File.DirInternal, "notepadapp-slot" & slot & ".txt", notepadbox.Text)
ToastMessageShow("Data deleted for slot " & slot & ".", True)
End Sub
The exit button
The last button in the menu is the exit button, this is to fully stop the program and not leave it running in the background. I like this in my apps so i can close them and speed up my phone and save battery. The code is very simple and uses a new sub.
Sub exit_Click
Activity.Finish
ExitApplication
End Sub
Finishing off
The app now fully works, there is one last thing i want to do to make it work better across phones with big screens. There are many things that can be edited along with colors designs, and loading the slots in a list box. This is the trick i used to make it better on bigger phones it just makes the text box adjust to the screen size, this code goes right after the menu code near the top.
notepadbox.Width = Activity.Width
notepadbox.Height = (Activity.Height - 50)
Done
That is now the app working fully. There is many things that can be added and made better, but this is a working android program that uses files to store data.
Screenshots
notepadappscreenshot (1) notepadappscreenshot (2) notepadappscreenshot (3)
End result
The app on Google Play
Written by Zachary.