Android Question custom input dialog select text

giggetto71

Active Member
Licensed User
Longtime User
Hi,
I am trying to open a custom input dialog in a way that the text is already selected when the dialog opens. basically I have a label with a certain value, upon label clicking the input dialog is open and the label value is pre set in the dialog text box field but the cursor stays a the beginning of the text so if the user wants to change the value he has to move the cursor the end, delete the text and type the new value.
If I could open the dialog with the text already selected the user could type directly the new value.
any idea?
thanks!!!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Download the dialogs example and change the code to:
B4X:
Sub btnEnterDetails_Click
   Dim sf As Object = DetailsDialog.ShowAsync("Enter details", "Ok", "Cancel", "", LoadBitmap(File.DirAssets, "form.png"), True)
   DetailsDialog.SetSize(100%x, 400dip)
   Wait For (sf) Dialog_Ready(pnl As Panel)
   pnl.LoadLayout("DetailsDialog")
   '0x00002000 = TYPE_TEXT_FLAG_CAP_WORDS (capitalize first character of each word)
   DialogFirstName.EditText.InputType = Bit.Or(0x00002000, DialogFirstName.EditText.InputType)
   DialogLastName.EditText.InputType = Bit.Or(0x00002000, DialogLastName.EditText.InputType)
   DialogFirstName.Text = "Test"
   DialogFirstName.EditText.SelectAll
   DialogAge.Add("")
   For i = 1 To 120
       DialogAge.Add(i)
   Next
   DetailsDialog.GetButton(DialogResponse.POSITIVE).Enabled = False
   Sleep(100)
   DialogFirstName.EditText.RequestFocus '<----
   Dim ime As IME 'ignore
   ime.ShowKeyboard(DialogFirstName.EditText) '<-----
   Wait For (sf) Dialog_Result(res As Int)
   'force the keyboard to hide
   DialogFirstName.EditText.Enabled = False
   DialogLastName.EditText.Enabled = False
   If res = DialogResponse.POSITIVE Then
       ToastMessageShow($"${DialogFirstName.Text} ${DialogLastName.Text} is ${DialogAge.SelectedItem} years old"$, True)
   End If
End Sub
https://www.b4x.com/android/forum/t...-2017-custom-dialogs-and-async-methods.80204/
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
sorry to bother again..I tried the code you pasted and it worked just fine. So I went back to the original example and tried to modify the example to my purposes and modify both the code and the layout file to have only one text box. basically I just commented all the references to last name and age both in the event and in the globals and removed them from the layout. but at runtime the app crashes as soon as the eneter details event fires..I am sure I am missing something fundamental but I spent few hrs and could not find..I am sure you can find it in 2 seconds..:)
thanks

B4X:
Sub btnEnterDetails_Click
    Dim sf As Object = DetailsDialog.ShowAsync("Enter details", "Ok", "Cancel", "", LoadBitmap(File.DirAssets, "form.png"), True)
    DetailsDialog.SetSize(100%x, 75%y)
    Wait For (sf) Dialog_Ready(pnl As Panel)
    pnl.LoadLayout("DetailsDialog")
    '0x00002000 = TYPE_TEXT_FLAG_CAP_WORDS (capitalize first character of each word)
    DialogFirstName.EditText.InputType = Bit.Or(0x00002000, DialogFirstName.EditText.InputType)
    'DialogLastName.EditText.InputType = Bit.Or(0x00002000, DialogLastName.EditText.InputType)
    'DialogAge.Add("")
    'For i = 1 To 120
    '    DialogAge.Add(i)
    'Next
    DetailsDialog.GetButton(DialogResponse.POSITIVE).Enabled = False
    Wait For (sf) Dialog_Result(res As Int)
    'force the keyboard to hide
    DialogFirstName.EditText.Enabled = False
    'DialogLastName.EditText.Enabled = False
    If res = DialogResponse.POSITIVE Then
        'ToastMessageShow($"${DialogFirstName.Text} ${DialogLastName.Text} is ${DialogAge.SelectedItem} years old"$, True)
    End If
End Sub

Sub DialogFirstName_TextChanged (Old As String, New As String)

CheckAllFieldsValid

End Sub



Sub CheckAllFieldsValid

Dim valid As Boolean = DialogFirstName.Text.Length > 0

DetailsDialog.GetButton(DialogResponse.POSITIVE).Enabled = valid

End Sub
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
I am sorry..I thought I pasted them...the code at line 60 is:

B4X:
Dim valid As Boolean =  DialogFirstName.Text.Length > 0

Logger connected to: samsung SM-J710F
--------- beginning of crash
--------- beginning of main
Copying updated assets files (2)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 60 (Main)
java.lang.RuntimeException: Object should first be initialized (FloatLabeledEditText).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.FloatLabeledEditTextWrapper.getText(FloatLabeledEditTextWrapper.java:94)
at b4a.example.main._checkallfieldsvalid(main.java:510)
at b4a.example.main._dialogfirstname_textchanged(main.java:526)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:360)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.objects.EditTextWrapper$1.afterTextChanged(EditTextWrapper.java:83)
at android.widget.TextView.sendAfterTextChanged(TextView.java:9508)
at android.widget.TextView.setText(TextView.java:5140)
at android.widget.TextView.setText(TextView.java:4956)
at android.widget.EditText.setText(EditText.java:111)
at android.widget.TextView.setText(TextView.java:4931)
at android.widget.TextView.setTransformationMethod(TextView.java:2364)
at android.widget.TextView.applySingleLine(TextView.java:9140)
at android.widget.TextView.setSingleLine(TextView.java:9115)
at anywheresoftware.b4a.objects.EditTextWrapper.setSingleLine(EditTextWrapper.java:140)
at anywheresoftware.b4a.objects.FloatLabeledEditTextWrapper._initialize(FloatLabeledEditTextWrapper.java:59)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:64)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:162)
at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
at b4a.example.main$ResumableSub_btnEnterDetails_Click.resume(main.java:453)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:360)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:245)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.agraham.dialogs.InputDialog$ExtendedBALayout$1.run(InputDialog.java:2467)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
I just checked and actually the text field is left empty as in the original example. I just deleted the other ojects. anyway...I got the point! thanks
 
Upvote 0
Top