Android Question B4XInputTemplate result

advansis

Active Member
Licensed User
Longtime User
Hi, guys.
I'm trying to use the B4XDialog.ShowTemplate method to ask the user to perform an action (say, to perform a file renaming or copy). Here is the code:

B4X:
    Dim DLG As B4XDialog
    DLG.Initialize(Activity)
    Dim opt As B4XInputTemplate
    opt.Initialize
    opt.lblTitle.text="Rename the file ?"
    opt.Text="myFile.txt"
    Dim oldName As String=opt.text
    Wait For (D.ShowTemplate(opt, "Rename", "Duplicate", "Cancel")) Complete (Result As Int)
    Dim newName as String=opt.text
    ..........

Now:
  • if the Result is Positive (Rename), it works correctly
  • the same happens for Cancel (obviously ;))
  • But if I want to follow the Negative program branch (to Duplicate), the opt.Text value IS NOT SET after closing the dialog. For example: in the dialog input box I will find "myFile.txt", so I correct it with "File2.txt" and press "Duplicate". The value of newName remains "myFile.txt" and not "File2.txt"
If possible, I would prefer not to ask the user twice and not to create a new view/activity.
The value of opt.TextField1.Text is not useful (I think is the same value of opt.Text)
Thanks in advance
 

advansis

Active Member
Licensed User
Longtime User
Hi Erel, I omitted the code because it is not relevant ([.........] at the end of code). The problem is "opt.text" variable that is not set in case of negative/cancel response. Thanks
 
Upvote 0

advansis

Active Member
Licensed User
Longtime User
Understood. This is by design.

You can get the input text in all cases with opt.TextField1.Text
Hi Erel, the value of opt.TextField1.Text is not set correctly (as stated in my first post).
I created this little project (targetSdkVersion="26"):

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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 xui As XUI
    Dim mySavePath As String
    Dim myFileName As String
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim B As Button
    B.Initialize("BTN")
    B.text="RENAME"
    Activity.AddView(B,50dip,50dip,100dip,100dip)
   
    Dim RP As RuntimePermissions
    mySavePath=RP.GetSafeDirDefaultExternal("")
    myFileName="Test.txt"
   
    File.WriteString(mySavePath,myFileName,"Something to write...")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub BTN_Click
    Dim D As B4XDialog
    D.Initialize(Activity)
    Dim opt As B4XInputTemplate
    opt.Initialize
    opt.lblTitle.text="Choose an action"
    opt.Text=myFileName
    Dim oldName As String=myFileName
    Wait For (D.ShowTemplate(opt, "Rename", "Duplicate", "Cancel")) Complete (Result As Int)
    If Result = xui.DialogResponse_Cancel Then Return
    Dim newName As String=opt.TextField1.Text
    Log(newName)
    If newName=oldName Then Return
    File.Copy(mySavePath,oldName,mySavePath,newName)
    If Result=xui.DialogResponse_Positive Then
        File.Delete(mySavePath,oldName)
    End If
End Sub

If I choose 'Duplicate', it doesn't work because opt.TextField1.Text is not set... In the LOG I find "Test.txt" instead of the new name...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are correct.
This is the relevant code:
B4X:
Private Sub DialogClosed(Result As Int)
   If Result = xui.DialogResponse_Positive Then
       Text = TextField1.Text
   End If
   TextField1.Text = Text
End Sub
Grab B4XInputTemplate from the b4xlib (unzip it), change its name to MyInputTemplate, remove the line that sets TextField.Text and use it instead of B4XInputTemplate.
 
Upvote 0

advansis

Active Member
Licensed User
Longtime User
Thanks Erel. I think I will do in such a way...
Any chance to have this feature in next releases, without grabbing? It always difficult to fork code you use every day... :(
 
Upvote 0
Top