Android Question PreferencesDialog, Get text field type from click in that text field

RB Smissaert

Well-Known Member
Licensed User
Longtime User
When the preferences dialog is showing with a number of different text fields (text, multi-line text, number or decimal number) is it possible
to determine the type of text field when clicking in that field?
I could work it out by looking at this:

B4X:
vTextField.Parent.Parent.Parent.Top

But that seems a bit convoluted.

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
You can find the item type from the PrefItems list. The item's type determines the UI elements.
In the B4XPages project when I am clicking in that textfield I only have that textfield view to get information from.
The only useful information I can see from that to determine the keyboard type is the Y position of that view on the page:

B4X:
Private Sub PrefDialog_TextFieldClicked(vTextField As B4XView)
    'this is used to put the keyboard top or bottom, not to cover the vTextField
    '---------------------------------------------------------------------------
    Dim iYPos As Int = vTextField.Parent.Parent.Parent.Top

I solved by just adding 2 lines to Sub CreateLayouts in the B4XPreferencesDialog b4xlib:

B4X:
Private Sub CreateLayouts (PrefItem As B4XPrefItem) As B4XView
    
    Dim p As B4XView = xui.CreatePanel("")
    
    p.Width = CustomListView1.sv.ScrollViewContentWidth
    p.Height = 50dip
    
    Select PrefItem.ItemType
        Case TYPE_BOOLEAN
            p.LoadLayout("booleanitem")
            Dialog.InternalSetTextOrCSBuilderToLabel(p.GetView(0), PrefItem.Title)
            p.GetView(0).TextColor = TextColor
        Case TYPE_MULTILINETEXT
            p.Height = PrefItem.Extra.Get("height")
            p.LoadLayout("textitemmulti")
            Dim ft As B4XFloatTextField = p.GetView(0).Tag
            ft.HintText = PrefItem.Title
            ft.Update
            ft.TextField.Tag = Array As Int(miTextFieldID, PrefItem.ItemType) '<<<<<
        Case TYPE_TIME
            CreateTimeItem(PrefItem, p)
        Case TYPE_NUMERICRANGE
            CreateNumericRangeItem(PrefItem, p)
        Case TYPE_TEXT, TYPE_PASSWORD, TYPE_NUMBER, TYPE_DECIMALNUMBER
            p.Height = 60dip
            If PrefItem.ItemType = TYPE_PASSWORD Then
                p.LoadLayout("passworditem")
            Else
                p.LoadLayout("textitem")
                Dim ft As B4XFloatTextField = p.GetView(0).Tag
                If PrefItem.ItemType = TYPE_NUMBER Then
                    #if B4A
                    Dim et As EditText = ft.TextField
                    et.InputType = et.INPUT_TYPE_NUMBERS
                    #else if B4I
                    Dim ttf As TextField = ft.TextField
                    ttf.KeyboardType = ttf.TYPE_NUMBER_PAD
                    #End If
                Else If PrefItem.ItemType = TYPE_DECIMALNUMBER Then
                    #if B4A
                    Dim et As EditText = ft.TextField
                    et.InputType = et.INPUT_TYPE_DECIMAL_NUMBERS
                    #else if B4I
                    Dim ttf As TextField = ft.TextField
                    ttf.KeyboardType = ttf.TYPE_NUMBERS_AND_PUNCTUATIONS
                    #End If
                End If
            End If
            
            Dim ft As B4XFloatTextField = p.GetView(0).Tag
            ft.HintText = PrefItem.Title
            ft.Update
            ft.TextField.Tag = Array As Int(miTextFieldID, PrefItem.ItemType)  '<<<<<
        Case TYPE_DATE
            TwoLabelsLayout("lblDate", p, PrefItem)
        Case TYPE_OPTIONS
            TwoLabelsLayout("lblOptions", p, PrefItem)
        Case TYPE_SHORTOPTIONS
            p.LoadLayout("shortoptions")
            p.GetView(0).TextColor = TextColor
            Dim original As List = PrefItem.Extra.Get("options")
            #if B4i
            Dim no As NativeObject = B4XComboBox1.mBtn
            B4XComboBox1.mBtn.Font = xui.CreateDefaultBoldFont(16)
            no.RunMethod("setTitleColor:forState:", Array(no.ColorToUIColor(TextColor), 0))
            no.SetField("contentHorizontalAlignment", 2) 'right
            #end if
            #if B4A
            Dim spnr As Spinner = B4XComboBox1.cmbBox
            spnr.TextColor = TextColor
            spnr.DropdownBackgroundColor = Dialog.BackgroundColor
            SetBackgroundTintList(spnr, xui.Color_Gray, TextColor)
            Dim options As List
            options.Initialize
            Dim cs As CSBuilder
            For Each opt As String In original
                options.Add(cs.Initialize.Alignment("ALIGN_OPPOSITE").Typeface(Typeface.DEFAULT_BOLD).Append(opt).PopAll)
            Next
            B4XComboBox1.SetItems(options)
            #else
            B4XComboBox1.SetItems(original)
            #End If
            Dialog.InternalSetTextOrCSBuilderToLabel(p.GetView(0), PrefItem.Title)
        Case TYPE_COLOR
            TwoLabelsLayout("lblColors", p, PrefItem)
        Case TYPE_EXPLANATION
            TwoLabelsLayout("lblExplanation", p, PrefItem)
        Case TYPE_SEPARATOR
            Dim lbl As Label
            lbl.Initialize("")
            Dim xlbl As B4XView = lbl
            xlbl.SetTextAlignment("CENTER", "CENTER")
            xlbl.TextColor = SeparatorTextColor
            xlbl.Font = xui.CreateDefaultBoldFont(14)
            xlbl.Color = SeparatorBackgroundColor
            p.Height = 30dip
            p.AddView(xlbl, 0, 0, p.Width, p.Height)
            Dialog.InternalSetTextOrCSBuilderToLabel(xlbl, PrefItem.Title)
            PrefItem.Required = False
    End Select
    
    If PrefItem.Required Then
        Dim rlbl As Label
        rlbl.Initialize("")
        Dim xlbl As B4XView = rlbl
        xlbl.Text = "*"
        xlbl.TextColor = xui.Color_Red
        xlbl.TextSize = 11
        xlbl.SetTextAlignment("TOP", "LEFT")
        p.AddView(xlbl, 01dip, 0dip, 10dip, 10dip)
    End If
    
    p.Color = ItemsBackgroundColor
    
    If mTheme <> THEME_DARK Then
        If p.GetView(0).Tag Is B4XFloatTextField Then
            Dim tf As B4XFloatTextField = p.GetView(0).Tag
            tf.TextField.TextColor = TextColor
            tf.NonFocusedHintColor = TextColor
            tf.Update
            If tf.lblClear.IsInitialized Then tf.lblClear.TextColor = TextColor
            If tf.lblV.IsInitialized Then tf.lblV.TextColor = TextColor
        End If
    End If
    Return p
End Sub

miTextFieldID is the number I pass to the library so my custom keyboard knows what view it is dealing with.

This seems to work all fine.

RBS
 
Upvote 0
Top