Android Question (Closed) Embed object into EditText?

anOparator

Active Member
Licensed User
Longtime User
Is there a way to guarantee that an EditText is never empty?
I have EditText1, EditText2, and EditText3 being written to 1.txt, 2.txt, and 3.txt, if the user deletes the content from the text file the app crashes.
Even with "Hint text" the file size is still zero bytes.
 
Last edited:

anOparator

Active Member
Licensed User
Longtime User
End users update 3 text files and send them as email attachments.
This works as intended.
However, if user deletes all contents of any instance of EditText1, that text file becomes zero bytes and is no longer available as an email attachment.
I need to hide/embed something into EditText1 so it is never zero bytes.
The app doesn't actually crash, it just feels like it does.
B4X:
Sub Globals
   Private EditText1 As EditText       '
   Private Button1 As Button
   Private Button2 As Button
   Private Button3 As Button
End Sub

Sub Button1_Click
   EditText1.Text = File.ReadString(Starter.shared, "1.txt")
   ....      ' User edits/adds text to EditText1
   File.WriteString(Starter.shared, "1.txt", EditText1.Text)
End Sub

Sub Button2_Click
   EditText1.Text = File.ReadString(Starter.shared, "2.txt")
   ....      ' User edits/adds text to EditText1
   File.WriteString(Starter.shared, "2.txt", EditText1.Text)
End Sub

Sub Button3_Click
   EditText1.Text = File.ReadString(Starter.shared, "3.txt")
   ....      ' User edits/adds text to EditText1
   File.WriteString(Starter.shared, "3.txt", EditText1.Text)
End Sub
 
Last edited:
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Create a function similar to:
B4X:
Private Sub TextValue(EditTextValue as string) as String
   If EditTextValue.Trim = "" Then
      Return "<no data entered>"
   Else
      Return EditTextValue.Trim
   End If
End Sub
Then, in your write replace the "EditText1.text" with "TextValue(EditText1.Text)" You could just return a single space instead of the message if desired.
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Create a function similar to:
B4X:
Private Sub TextValue(EditTextValue as string) as String
   If EditTextValue.Trim = "" Then
      Return "<no data entered>"
   Else
      Return EditTextValue.Trim
   End If
End Sub
Then, in your write replace the "EditText1.text" with "TextValue(EditText1.Text)" You could just return a single space instead of the message if desired.
I Like your solution offered, it is good, but because I send the email from a menu on Activity5 and the File.WriteString is in Activity3 which is paused I'm going to give users an Alert that files which have been emptied shall not be listed in the attachment queue.

I see from https://www.b4x.com/android/forum/threads/customview-numpad.64191/ that a CustomView can be created for an EditText view, and will make a [Wish] to be able to embed a space, number, or something into an CustomView_EditText.

I apologize that I was unable to show the relevant code in a clearer way.
 
Last edited:
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Depending on how often and where you need it, it could be as simple as:
B4X:
File.WriteString(Starter.shared, "1.txt", EditText1.Text.Trim & " ")
Or, for the function you could change "Private Sub TextValue(…" to "Public Sub TextValue(…" and place it in a module. That way you could use the routine from any activity or other module.

I don't know the needs or scope of your exact project so your mileage may vary as they say ;)
 
Last edited:
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Thanks for the idea, I added my 'Label1.Text (Full name) to the File.WriteString so that 1.txt is never empty.
B4X:
 File.WriteString (Starter.shared, Starter.nmbr & ".txt", Label1.Text & Chr(10) & EditText1.Text)
 
Upvote 0
Top