Android Question How to add Clear button to a EditText

ttsolution

Member
Licensed User
Longtime User
Dear all,

How can we add a clear button to a edittext ?

For example: the user want to change the price so they want to clear all numbers in edittext quick rather than use the back key.

Many thanks for any help/sugession

Jonh
 

ilan

Expert
Licensed User
Longtime User
if you click and hold the back key (delete key) that would delete the whole text

what you could do is make your own keyboard and put a button that set edittext.text = ""
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
what about shake event? shake the phone to clear the edittext ??
maybe thats a quick enough solution for you
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
what about shake event? shake the phone to clear the edittext ??
maybe thats a quick enough solution for you

That's clever...

How about a 'drop your phone in water and the water sensor will clear the EditText'?

... oh, never mind, there is no water sensor. ;) It'll probably still work, though.

http://www.b4x.com/android/forum/threads/shakedetector-shake-it-baby.43267/#content

Code would be something like this:

B4X:
' Declare a ShakeDetector in Sub Globals
Sub Globals
  'These global variables will be redeclared each time the activity is created.
  'These variables can only be accessed from this module.
  Dim Detector As ShakeDetector
  Dim TheEditTextBoxName as EditText
End Sub

' Initialize the object in Sub Activity_Create
Sub Activity_Create(FirstTime As Boolean)
  'Do not forget to load the layout file created with the visual designer. For example:
  Activity.LoadLayout("Layout1")
  ' Load the Layout which has TheEditTextBoxName like you normally load a Layout
  ' and change 'TheEditTextBoxName' to the name of your EditText box
  Detector.Initialize("Detector")
End Sub

' Start the Detector when the activity is resumed
Sub Activity_Resume
  Detector.Start
End Sub

' Stop the Detector when the activity is paused
Sub Activity_Pause (UserClosed As Boolean)
  Detector.Stop
End Sub

' Catch the shake event
Sub Detector_Shake
  TheEditTextBoxName.Text = ""
End Sub


And, you'd have to download the ShakeDetector library and place it in your Library folder, and check the checkmark under the 'Libs' tab for it to load it (you need to close your App and re-open B4a).

Only problem would be... you might accidently shake your phone when you placed it on a table, or just swinging it around to put it into your pocket, or you might get on a ride at the state fair and be riding a ride which shakes you back-and-forth...

BAM there goes your text.


Makes me want to, somehow, use the ShakeDetector library in my app, though.
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
so what you can do is if edittext is focused then shake event will do something... very simple

put it here:

B4X:
Sub Detector_Shake
   If edittext1.RequestFocus = True Then edittext1.Text = ""
End Sub

something like this

or
B4X:
Sub Detector_Shake
   If edittext1.Text <> "" Then edittext1.Text = ""
End Sub
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
You will depart from the standard Android UI design and users may be more confused, while you want them to be happy.
If you must do this, then I would put only one button for all edittextviews and write it's event's sub so that it clears only the
edittext that has the focus at that moment. You can put the button anywhere in the layout.

Of course the shake idea is a nice joke and I would add to it, that after shaking well then pour the text in a trash bin by changing the orientation :)
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
You will depart from the standard Android UI design and users may be more confused, while you want them to be happy.
If you must do this, then I would put only one button for all edittextviews and write it's event's sub so that it clears only the
edittext that has the focus at that moment. You can put the button anywhere in the layout.

Of course the shake idea is a nice joke and I would add to it, that after shaking well then pour the text in a trash bin by changing the orientation :)

;)

Show an animation of a cocktail shaker as it shakes, then show a toilet when they stop and have it flush.
 
Upvote 0
Top