Android Question EditText limited the rows on "EnterPressed"

Alexander Stolte

Expert
Licensed User
Longtime User
Hello,

i dont found a answer in this Forum about this, you can set the max lenght of the text, but not the max rows. Where can i set the max rows of the EditText? Because i do not want, users can thousand lines go down and can upload this.

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
   Dim i As Int = IndexOfNth(New, CRLF, 3)
   If i > -1 Then
     EditText1.Text = New.SubString2(0, i)
     EditText1.SelectionStart = EditText1.Text.Length
   End If
End Sub

Sub IndexOfNth(s As String, SearchFor As String, Count As Int) As Int
   Dim res As Int = -1
   For i = 0 To Count - 1
     res = s.IndexOf2(SearchFor, res + 1)
     If res = -1 Then Return -1
   Next
   Return res
End Sub
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
I was trying a solution when @Erel posted. In any case if someone wants to try to see if mine also works ...

B4X:
Sub Globals
  
    Private EditText1 As EditText
    Dim maxlines As Int=3
  
End Sub

Sub EditText1_TextChanged (Old As String, New As String)
      
    Dim lines As Int
     For x=0 To New.Length-1
            If New.CharAt(x)=CRLF Then lines=lines+1
    Next
     If lines=maxlines Then
        EditText1.Text=Old
        EditText1.SelectionStart = EditText1.Text.Length
    End If
  
End Sub
 
Upvote 0
Top