Place my app's manual in program for reading

jschuchert

Active Member
Licensed User
Longtime User
Hopefully I can demonstrate this so that it will be easy to understand. I want to place my manual in an activity so that the user can read it within the application. I included the file (let's call it "manual.txt") under 'Files' in the IDE so that it can be referenced under 'dirassets' when compiled. I pick the file from a dialog or reference it directly. Here is the code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
activity.LoadLayout ("start")
txtfilename.RequestFocus 
File.Copy (File.DirAssets ,"manual.txt",File.DirInternal ,"manual.txt")
End Sub

Sub btnFile_Click
Dim text As String 
   Dim fd As FileDialog
   Dim bmp As Bitmap
   fd.FastScroll = True
   fd.FilePath=File.DirInternal 
   fd.FileFilter = ".txt" 
   ret = fd.Show("B4A File Dialog", "Select", "Cancel", "", Null)
   text=File.ReadString(File.DirInternal ,fd.ChosenName )
   txtmanual.text=text
    
End Sub

This works fine in the 'Main' activity but there really isn't room for a large text box to contain the manual (it's pretty long) and I don't want to use a panel. Therefore, I thought I could put it in an activity called 'manual' and show it there where I have a large text box and an 'Open' button. Declarations, etc. are in order. Here is the code for that:
B4X:
Sub btnOpen_cilck
   Dim fd As FileDialog
   Dim bmp As Bitmap
   fd.FastScroll = True
   fd.FilePath=File.DirInternal 
   fd.FileFilter = ".txt"
   ret = fd.Show("B4A File Dialog", "Select", "Cancel", "", Null)
   text=File.ReadString (File.DirInternal ,fd.ChosenName )
   txtmanual.text=text
End Sub

I also tried it like this:
B4X:
sub btnOpen_click
File.OpenInput (File.Dirinternal,"manual.txt")
text=File.ReadString (File.dirinternal ,"manual.txt")
txtmanual.text=text
End Sub

It does not work when I take it out of the main module. In fact I am unable to get the file dialog to show. I have tried it in various ways ( without the file dialog and referencing the file directly) but just can't do it in any place except the main module. Incidentally, I really like the file dialog agraham made. I will use it elsewhere even if I can't get this to work. I'm sure I am missing some logic here but can't see it. Surely I should be able to display a text file (could I also use a 'doc or 'pdf'?)

As always, I appeciate your time, patience and expertise.

Jim
 

moster67

Expert
Licensed User
Longtime User
An alternative approach could be that you save your manual/help-file as a html-file and show it using the WebView? This will also permit you to format your manual with Bold, Colored-characters and so on.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
WebView is definitely the way to go. It also allows you to create a Table Of Contents with internal links to parts of the manual, as well as being able to make links to external web sites, to display pictures, etc.

In my app, two buttons in the upper-right corner are "Back" (normally not visible) and "Help". When Help is clicked, it brings up WebView with the manual and changes the text on the Help button to "X", which is used to close the WebView. The Back button is made visible and is used to return from internal links back to the TOC. Since the buttons are at the upper-right, they are already close to their normal positions for these functions.

B4X:
Sub Globals
   Dim webViewHelp As WebView
End Sub
 
Sub Activity_Create
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]   webViewHelp.SetLayout([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], activity.Width, activity.Height)
   webViewHelp.LoadURL([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"file:///android_asset/manual.htm"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])
   webViewHelp.Visible = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]End Sub
 
Sub btnHelp_Click
   If webViewHelp.Visible = False Then
      webViewHelp.BringToFront
      webViewHelp.Visible = True
      btnHelp.Text = "X"
      btnBack.Visible = True
      btnHelp.BringToFront
      btnBack.BringToFront
   Else
      btnHelp.Text = "Help"
      btnBack.Visible = False
      webViewHelp.Visible = False
   End If
End Sub
 
Sub btnBack_Click
   webViewHelp.Back
End Sub

Since the manual is large, I load it into WebView in Activity_Create rather than waiting for the Help button to be pushed where the load time is more noticeable.
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I also use a webview.

I have a help activity, the layout of which has two buttons (ButtonForward and ButtonBack) to go forward or backward in the pages visited, and also I populate a spinner with the html files found in File.DirAssets, so that navigating to a different section is easier.

I also set a global variable in the main activity to the page I want to display on starting help (main.main.helpurl), so that help is context sensitive.

The full activity code is below, some of the lines relate to my app, but it is easy enough to follow. If you want to use it feel free, I am also happy to answer any questions on it.
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim WebViewHelp As WebView
   Dim SpinnerHelp As Spinner
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Help_Layout")
   General.ColourButtons (Activity)
End Sub

Sub Activity_Resume
   Dim HelpList As List
   Dim test As String
   
   HelpList = File.ListFiles (File.DirAssets)
   For i = 0 To HelpList.Size-1
      test= HelpList.Get (i)
      If (test.EndsWith  (".html")) Then SpinnerHelp.Add  (test.SubString2 (0,test.Length -5)) ' if a html file strip the .html and add to spinner
   Next
   WebViewHelp.LoadUrl ("file:///android_asset/"&main.main.helpurl&".html") ' load default page
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SpinnerHelp_ItemClick (Position As Int, Value As Object)
   main.main.helpurl = SpinnerHelp.GetItem (Position)
   WebViewHelp.LoadUrl ("file:///android_asset/"&main.main.helpurl&".html") ' add html to selected page and display
End Sub
Sub ButtonBack_Click
   WebViewHelp.Back 
End Sub
Sub WebViewHelp_PageFinished (Url As String) ' page loaded so set spinner to reflect that page
   ' I do this here rather than ItemClick as the pages have links in them to other pages, so if the user clicks on the link the spinner gets updated.
   Dim page As String
   page=url.SubString2 (22, url.Length - 5)
   page = page.Replace ("%20", " ") 'deal with spaces!
   SpinnerHelp.SelectedIndex = SpinnerHelp.IndexOf (page)   
End Sub
Sub ButtonForward_Click
   WebViewHelp.Forward 
End Sub
 
Last edited:
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Either my computer or access to the forum has delayed my reply.

Thanks for the great responses. I will definitel check out WebViewer and Klaus' suggestion also. Erel, I finally discovered why the new actvity was not working. When I named the edittext I did it in the 'Event' name and not the actual name field. I have made that mistake before because the scoll bar in the designer drops a little and doesn't show the name field, only the event name. Glad I turned on the debugger or might not have found it. It told me I needed to initialize that view but since it was done in the designer, it led me to the problem.

Is there any chance there wil ever be a "basic4 'ios'? or for any apple OS?

Thanks, guys, for all the help and especially for your time in posting code.

Jim
 
Upvote 0

dealsmonkey

Active Member
Licensed User
Longtime User
I am working on hosting my help files on a remote server and then showing in a webview.

The advantages are I can keep them updated without having to update the app on the market, the disadvantage is the user needs internet access to read them !

Another advantage of using HTML is you could "slip" the odd advert into the help file if you so wished :eek:
 
Upvote 0
Top