M msdeveloper Member Licensed User Longtime User Nov 17, 2012 #1 Is there a way to limit the number of characters a user can enter into EditText? In VB6 I would use the MaxLength property.
Is there a way to limit the number of characters a user can enter into EditText? In VB6 I would use the MaxLength property.
Erel B4X founder Staff member Licensed User Longtime User Nov 17, 2012 #2 Something like: B4X: Expand Collapse Copy Sub EditText1_TextChanged (Old As String, New As String) If New > 100000 Then EditText1.Text = Old End Sub Upvote 0
Something like: B4X: Expand Collapse Copy Sub EditText1_TextChanged (Old As String, New As String) If New > 100000 Then EditText1.Text = Old End Sub
M msdeveloper Member Licensed User Longtime User Nov 17, 2012 #3 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
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
R Roger Garstang Well-Known Member Licensed User Longtime User Nov 19, 2012 #4 Yes, everyone always forgets to set the cursor. I actually get the cursor position before and use the Min function to restore it to where it was or the end of the string as you have. That way if in the middle of the string the cursor is in the same spot. Upvote 0
Yes, everyone always forgets to set the cursor. I actually get the cursor position before and use the Min function to restore it to where it was or the end of the string as you have. That way if in the middle of the string the cursor is in the same spot.
Sinan Tuzcu Well-Known Member Licensed User Longtime User Jan 13, 2013 #5 Erel said: Something like: B4X: Expand Collapse Copy Sub EditText1_TextChanged (Old As String, New As String) If New > 100000 Then EditText1.Text = Old End Sub Click to expand... this code does not working, because > 100000 this is number and not string. Upvote 0
Erel said: Something like: B4X: Expand Collapse Copy Sub EditText1_TextChanged (Old As String, New As String) If New > 100000 Then EditText1.Text = Old End Sub Click to expand... this code does not working, because > 100000 this is number and not string.
M Mahares Expert Licensed User Longtime User Jan 13, 2013 #6 Here is the correct code: B4X: Expand Collapse Copy Sub EditText1_TextChanged (Old As String, New As String) If New.Length > 100000 Then EditText1.Text = Old End Sub Upvote 0
Here is the correct code: B4X: Expand Collapse Copy Sub EditText1_TextChanged (Old As String, New As String) If New.Length > 100000 Then EditText1.Text = Old End Sub
Eric H Active Member Licensed User Longtime User Aug 16, 2013 #7 This code isn't working for me the way I would expect (I can enter 2342789692834 and it doesn't stop me). B4X: Expand Collapse Copy 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
This code isn't working for me the way I would expect (I can enter 2342789692834 and it doesn't stop me). B4X: Expand Collapse Copy 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
klaus Expert Licensed User Longtime User Aug 16, 2013 #8 What do you expect ? In your code you limit the length of the text to 1000 characters ! Best regards. Upvote 0
What do you expect ? In your code you limit the length of the text to 1000 characters ! Best regards.
Eric H Active Member Licensed User Longtime User Aug 16, 2013 #9 Doh! I mistakenly was thinking max value and not max length Appreciate the help Upvote 0
B Bryan Member Licensed User Longtime User Aug 8, 2014 #10 This can result in your app crashing if using a dictionary/Spellchecking B4X: Expand Collapse Copy 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: Expand Collapse Copy 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: Aug 8, 2014 Upvote 0
This can result in your app crashing if using a dictionary/Spellchecking B4X: Expand Collapse Copy 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: Expand Collapse Copy 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
mangojack Expert Licensed User Longtime User Aug 8, 2014 #11 Maybe read post #4 by Erel Here .... it should be .. B4X: Expand Collapse Copy If New.Length > 20Then EditText1.Text = New.Substring2(0, 20) Unsure if that solves the spellchecker problem . Upvote 0
Maybe read post #4 by Erel Here .... it should be .. B4X: Expand Collapse Copy If New.Length > 20Then EditText1.Text = New.Substring2(0, 20) Unsure if that solves the spellchecker problem .
B Bryan Member Licensed User Longtime User Aug 9, 2014 #13 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
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.
Erel B4X founder Staff member Licensed User Longtime User Aug 9, 2014 #14 Remove this event and use IME.SetLengthFilter instead. Upvote 0