AS PROMISED - Part. 7/b
RemAll-2014 Vers.1.2.2
Well, it is time to speak about the code,
As previously said the code depends also from the layout chosen.
In our case you do not need of a starting button because you start the required function by a click on the archive fields.
The matter is quite easy. When you click on a label of the list relative to the selected archive if the text of the label contain a key word (I.E jpg, png etc or http,www etc) the item needed to show the field contents, picture or web page woòò be created.
The code used is the following;
Sub Labelio_Click
If BtnBack2.Text = Lang.L(F,42) Then Return )
If BtnS.Text = Lang.L(F,28) Then Return ) 'these lines lock the use of top bar buttons
If Lfun.Text = Lang.L(F,67) Then Return )
Dim Lio As Label
Lio.Initialize("Labelio")
Lio = Sender
If (Lio.Text).IndexOf(".jpeg") <> -1 Then
FileIm = Lio.Text
CreaImage
Return
End If
If (Lio.Text).IndexOf(".png") <> -1 Then
FileIm = Lio.Text
CreaImage
Return
End If
If (Lio.Text).IndexOf(".jpg") <> -1 Then
FileIm = Lio.Text
CreaImage
Return
End If
.............
...............
If (Lio.Text).IndexOf("http") <> -1 Then
NamUr = Lio.Text
CreaWebView
Return
End If
If (Lio.Text).IndexOf("www.") <> -1 Then
NamUr = "http://" & Lio.Text
CreaWebView
Return
End If
RecSrc = Lio.Text : If RecSrc = "-" Then Return
'Others possible extensions can be tested obviously
The last line gets the value to pass to the Image or Web View (avoiding to utilize empty fields)
If one of the If/End if coded to select the enclosed key search is true then starts the relative dynamic object construction.
To do this the program uses the following code
Sub CreaImage
Dim Im As ImageView
Im.Initialize("ImageView")
Im.Visible = True
'Im.Color = Colors.Gray 'used during the developing time to verify position
Im.BringToFront
Activity.AddView(Im, 0, LG.Top+LG.Height, SceltaLbl.Width, ClblNote.Width)
Note that i fixed dimensions using the values of others differentes items. Usually the instruction should be
Activity.AddView(ViewName, Left, Top, Width, Height) with the dip notation
Im.Gravity = Gravity.Fill
Im.Bitmap = LoadBitmap("/mnt/sdcard/Ricon/", FileIm)
qq = Activity.NumberOfViews : LN = qq-1
End Sub
or for URL browser
(do not forget to add the WebViewExtras library to the project)
Sub CreaWebView
If WL = False Then
BtnF_Click
BtnGo.Gravity = Gravity.RIGHT
BtnGo.Typeface = Typeface.DEFAULT_BOLD
BtnGo.Text = ">>" : BtnGo.Visible = True
BtnEnd.Gravity = Gravity.LEFT
BtnEnd.Typeface = Typeface.DEFAULT_BOLD
BtnEnd.Text = "<<" : BtnEnd.Visible = True
BtnBack2.Text = Lang.L(F,85) '"Chiudi-Browser"
SceltaLbl.Text = Lang.L(F,84)
WV.Initialize ("WV")
WV.JavaScriptEnabled = True
WVx.addJavascriptInterface(WV,"B4A")
Activity.AddView (WV, Label7.Height, BtnEnd.Top, Label4.Width+BtnEnd.Top, SceltaLbl.Width)
WV.Visible = True : WL = True
Lfun.TextColor = Colors.Red : Lfun.Text = Lang.L(F,86)
WV.LoadUrl(NamUr)
.................
The boolean value WL check if the Web View exists and if value is False the webview will be created, while the lines of code that follow modify the title of the 2 buttons, usually invisible, showing them both marked with << or >> to use them with the browser.
Sub BtnEnd_Click
If WL = True Then
WV.Back
Return
End If
If EditText.Text = "" Then Return
IME.HideKeyboard
SearchDat
End Sub
and the same for the other button.
The privacy button also has been used to close the browser changing the code that usually close the activity.
Sub BtnBack2_Click
If WL = True Then
Dim o As Reflector
o.Target = WV
o.RunMethod2("clearCache","True","java.lang.boolean")
BtnGo.Gravity = Gravity.CENTER_HORIZONTAL
BtnGo.Text = "" : BtnGo.Visible = False
BtnEnd.Gravity = Gravity.CENTER_HORIZONTAL
BtnEnd.Text = "" : BtnEnd.Visible = False
If Lfun.Text <> "" Then
WV_PageFinished(UU)
End If
Lfun.TextColor = Colors.Black :
WV.StopLoading
WVx.clearCache(WV,False)
WV.RemoveView
SceltaLbl.Text = Lang.L(F,21)
BtnBack2.Text = Lang.L(F,26)
qq = Activity.NumberOfViews : LN = qq
WL = False
Return
End If
.....................
.....................
Activity.finish
End Sub
We can note that clicking the button the WL value turns False so the previously mentiomed buttons are reinstated in their original function.
To close browser also stop loading and removes the webview but, more important, uses WebViewExtras (WVx) to clear cache.
Last adding to the code are few lines that show the information "loading" any time the browser connects to the request url or a new page is called.
The code uses the WebView events pagefinished and override as shown below.
Sub WV_PageFinished(Url As String)
Lfun.Text = ""
UU = Url
End Sub
Sub WV_OverrideUrl(Url As String) As Boolean
Lfun.Text = Lang.L(F,86)
End Sub
Well, that's all. A I have said many times that I do not want to teach anything, but just to show a little 'code that works for me.
The attached image shows to which of the dymanic items the above code refers.