Android Question In an EditText, How to convert each keypress to uppercase.

joaquinortiz

Active Member
Licensed User
Longtime User
How to convert each alphabetic keystroke into uppercase and show it in Edittext.

I can't find any way to do this. I just find EditText1.text.Uppercase, but this works until lostfocus. I need it when each key is pressed, may be in EditText_TextChanged.
 

Biswajit

Active Member
Licensed User
Longtime User
B4X:
edittext.InputType = Bit.or(1,4096) 'TYPE_CLASS_TEXT | TYPE_TEXT_FLAG_CAP_CHARACTERS
 
Upvote 0

joaquinortiz

Active Member
Licensed User
Longtime User
Dear friends, @Biswajit & @klaus. It doesn't works in both cases. May be I put it in a wrong place!. What you recommend me?

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim LicActiva As Int
    Dim lstrRespuesta As String
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("frmActivaLicencia")
    '/////////////////////////////////////////////////////////////////////////////
    'Se definen los parametros de animacion para objetos
    ConfiguraAnimationButton
    lblBack.Tag = anButton
    btnIniciar.Tag = anButton
    LimpiaTextos
    txtKey1.Tag = 8
    txtKey2.Tag = 4
    txtKey3.Tag = 4
    txtKey4.Tag = 4
    txtKey5.Tag = 12
   
    '//////////////////////////////////////////
    '1.- fcnLicenciaYaEstaActivada? Si es SI, entonces se manda un mensaje. De lo contrario habilita para que capture las Llaves
    lArray = myAccess.fcnObtieneDatosLicencia()
    LicActiva = lArray(8)
    If LicActiva <> 0 Then 'Significa que el campo Estatus = 1
        lstrRespuesta = "Ya se activó su LICENCIA." & CRLF & CRLF & _
                        "Gracias!!!"
        Msgbox2Async(lstrRespuesta, myGlobalVariables.gSistema, "Aceptar", "", "", Null, False)
        Wait For Msgbox_Result (Result As Int)
        If Result = DialogResponse.POSITIVE Then
            SalirFormulario
        End If
    Else
        txtKey1.SingleLine = True
'        txtKey1.InputType = 4096
        txtKey1.InputType = Bit.Or(1,4096)
       
        txtKey1.RequestFocus

    End If
   
End Sub
 

Attachments

  • ConvertToUpperCase.jpeg
    ConvertToUpperCase.jpeg
    153.2 KB · Views: 297
Upvote 0

klaus

Expert
Licensed User
Longtime User
As suggested by DonManfred just after LoadLayout.
If you have set any lowercase text before, you must convert it to uppercase.
txtKey1.InputType = Bit.Or(1,4096) doesn't convert any existing text in txtKey1.Text to uppercase!
 
Upvote 0

joaquinortiz

Active Member
Licensed User
Longtime User
it is only set if LicActiva = 0

I suggest to set it right after loading the layout.
Dear Friends @DonManfred & @klaus.
I move the code line, after loading Layout and it's still doing the same.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim LicActiva As Int
    Dim lstrRespuesta As String
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("frmActivaLicencia")
    txtKey1.SingleLine = True
    txtKey1.InputType = 4096
    '/////////////////////////////////////////////////////////////////////////////
    'Se definen los parametros de animacion para objetos
    ConfiguraAnimationButton
    lblBack.Tag = anButton
    btnIniciar.Tag = anButton
    LimpiaTextos
    txtKey1.Tag = 8
    txtKey2.Tag = 4
    txtKey3.Tag = 4
    txtKey4.Tag = 4
    txtKey5.Tag = 12
    
    '//////////////////////////////////////////
    '1.- fcnLicenciaYaEstaActivada? Si es SI, entonces se manda un mensaje. De lo contrario habilita para que capture las Llaves
    lArray = myAccess.fcnObtieneDatosLicencia()
    LicActiva = lArray(8)
    If LicActiva <> 0 Then 'Significa que el campo Estatus = 1
        lstrRespuesta = "Ya se activó su LICENCIA." & CRLF & CRLF & _
                        "Gracias!!!"
        Msgbox2Async(lstrRespuesta, myGlobalVariables.gSistema, "Aceptar", "", "", Null, False)
        Wait For Msgbox_Result (Result As Int)
        If Result = DialogResponse.POSITIVE Then
            SalirFormulario
        End If
    Else
        txtKey1.RequestFocus
    End If
    
End Sub
 

Attachments

  • ConvertToUpperCase2.jpeg
    ConvertToUpperCase2.jpeg
    79.4 KB · Views: 244
Upvote 0

klaus

Expert
Licensed User
Longtime User
Without more information it's impossible to help.
Where does the text come from?
The EditText view you are showing, is it really txtKey1 ?
Only text entered with the keyboard is set to uppercase, not text entered in another way.
Otherwise post a small project showing the problem.
 
Upvote 0

joaquinortiz

Active Member
Licensed User
Longtime User
Without more information it's impossible to help.
Where does the text come from?
The EditText view you are showing, is it really txtKey1 ?
Only text entered with the keyboard is set to uppercase, not text entered in another way.
Otherwise post a small project showing the problem.
@klaus.
txtkey1 is an EditText.
I'm not entering text in another way than keyboard.

I'll create a small project and post it. With this I hope you can help me.

Thanks for your time.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
auto-capitalization must be turned on in device settings for it to work!! don't ask why. it is my understanding that device settings can only be managed by the user, so you'll have to do things the hard way and simply change the input to all caps after the fact. no biggie, and probably irrelevant to the user.

also found another curiosity: if you look at the tool tips for edittext, check the tip for "edittext.input =". it says that if you change the input type, the view becomes single line. apparently not true. when i changed the input type from text to allcaps (after having turned on autocaplitalization in device settings),
my input appeared in all caps, and the view continued to be multi-line. not sure what the issue is there. (and, to be clear, all caps did not work using any of the methods suggested here until i activated auto-capitalization in my device settings. i stumbled upon this gem hidden here

* i shouldn't say you can't manage the device settings for the user; technically, at least in the case of all caps, you can modify the manifest, but i think you'd have a problem within the b4a framework as the manifest is generated automatically. i'm guessing you can alter it after the fact, but someone more familiar with the process will have to comment. if the b4a developer can't make the modification, then you will have to change the input manually from whatever it is to uppercase, which is trivial. (maybe a custom view set to all caps would work. i think you create your own manifest as part of that process.)
 

Attachments

  • caps.png
    caps.png
    6.5 KB · Views: 299
Last edited:
Upvote 0

emexes

Expert
Licensed User
How to convert each alphabetic keystroke into uppercase and show it in Edittext.
Why not just clean the input up when you use it? Also, lowercase letters are mostly easier to distinguish from digits( and themselves) than uppercase letters are.

One of my pet peeves with entering long numbers (eg credit cards, telephone, bank account) is when they don't allow you to type in the spaces and dashes that make it easier to check what you've typed against the source.
 
Upvote 0

Midimaster

Active Member
Licensed User
why dont you do it that easy way:
B4X:
Private Sub MyEdit_TextChanged (Old As String, New As String)
    If New=Old Then Return
    MyEdit.Text=New.ToUpperCase
    MyEdit.SelectionStart=New.Length
End Sub

the function would even change this "small" to "SMALL"
B4X:
Sub Globals
    Private  MyEdit As EditText
nd Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Button1_Click
    MyEdit.Text="small"
End Sub

Private Sub MyEdit_TextChanged (Old As String, New As String)
    If New=Old Then Return
    MyEdit.Text=New.ToUpperCase
    MyEdit.SelectionStart=New.Length
    Log(MyEdit.SelectionStart)
End Sub
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
.. is when they don't allow you to type in the spaces and dashes that make it easier to check what you've typed
For those I use a text file (kept on a safe USB data memory) where I can read both the "spaced" version and the "all together" one. Then I simply CTRL-C/CTRL-V the unreadable one to the website's box.
 
Upvote 0

ddk1

Member
Licensed User
I like Midimaster's answer. Just one small tweak else it recurses that sub one more time than necessary.
B4X:
If New = Old.ToUpperCase Then Return
MyEdit.Text = New.ToUpperCase
MyEdit.SelectionStart = New.Length
 
Upvote 0
Top