EditText Max Characters Limit

msdeveloper

Member
Licensed User
Longtime User
Is there a way to limit the number of characters a user can enter into EditText? In VB6 I would use the MaxLength property.
 

msdeveloper

Member
Licensed User
Longtime User
EditText MaxLength

Thanks. I figured that out right after I posted. I did add one additional part.

Sub txtReading_TextChanged(strOld As String, strNew As String)

'When new length invalid--
' Revert back to previous text, move cursor to the end
If strNew.Length > 3 Then
txtReading.Text = strOld
txtReading.SelectionStart = txtReading.Text.Length
End If


End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is the correct code:
B4X:
Sub EditText1_TextChanged (Old As String, New As String)   
     If New.Length > 100000 Then EditText1.Text = Old
End Sub
 
Upvote 0

Eric H

Active Member
Licensed User
Longtime User
This code isn't working for me the way I would expect (I can enter 2342789692834 and it doesn't stop me).

B4X:
Sub Globals
    Dim etxQuantity As EditText
End Sub

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

Sub etxQuantity_TextChanged (Old As String, New As String)
    If New.Length > 1000 Then etxQuantity.Text = Old
End Sub

What's wrong? its the same code as above, just EditText1 was changed to etxQuantity...

Thanks
 
Upvote 0

Bryan

Member
Licensed User
Longtime User
This can result in your app crashing if using a dictionary/Spellchecking

B4X:
Sub EditText1_TextChanged (Old As String, New As String)   
If New.Length > 20 Then EditText1.Text = Old
End Sub

If you happen to type into the EditText1 somewhere say 15 characters, and then start typing another word, and your dictionary/spellchecking pops up because of misspelling, and you choose a word that will put you over your max character limit then your app will crash. Maybe someone else can confirm this as I'm running my app in an emulator.

B4X:
java.lang.IndexOutOfBoundsException: setSpan (11 ... 23) ends beyond length 20
    at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:945)
    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:527)
    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:520)
    at android.widget.TextView.setSpan_internal(TextView.java:11473)
    at android.widget.TextView$SuggestionsPopupWindow.onItemClick(TextView.java:9988)
    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
    at android.widget.AbsListView$1.run(AbsListView.java:3168)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
    at dalvik.system.NativeStart.main(Native Method)

Best to use
EditText1.InputType = Bit.OR(EditText1.InputType, 524288)
to turn off spellchecking
 
Last edited:
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Maybe read post #4 by Erel Here ....

it should be ..
B4X:
If New.Length > 20Then EditText1.Text = New.Substring2(0, 20)

Unsure if that solves the spellchecker problem .
 
Upvote 0

Bryan

Member
Licensed User
Longtime User
If New.Length > 20Then EditText1.Text = New.Substring2(0, 20)

It Works, but spell checker still crashes my app if It inserts a word that exceeds the edittext1 limit.
 
Upvote 0
Top