• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to clear an edittext by cross button in the right side

 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created an edittext for search, which contains in the left side a search icon and in the right side a cross icon:

I want to know how can i clear the content of editText when I click in the cross button. Thank you in advance.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add an on click listener to the button which sets the text in the edit text field to nothing.
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This should run without problem
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Add an on click listener to the button which sets the text in the edit text field to nothing.

I don't understand you, could you please explain me more
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ibrahim yener wrote:

I see that you don't understand what i want.
I have created an edit text which contains in the right side a cross,
i want to clear this edit text when i click on this cross.
Did you get me ?
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In onCreate method or onResume method.

How about post some your code.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just put it in the beginig of my post
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarra Sakka wrote:
ibrahim yener wrote:...

I see that you don't understand what i want.


In fact, the code he posted does exactly what you described. You will have to adapt it to your program (i.e. change the resource names). If it doesn't do what you want then you should explain what you expect in more detail and what the code posted doesn't do or does incorrectly.

ibrahim, please do not re-post the code fixed for Sarra's code. Remember we are NotACodeMill here at the ranch, we can help Sarra learn, but giving him/her code to copy and paste without understanding doesn't help him/her learn.

Edit: Oops. Now I see that I am incorrect! You haven't created a composite view with two buttons and an edit field, you created a single view with drawables on either side, and you want the drawables to have actions. You can't do that, the drawables are display only. You might be able to do something with listening to touch events and mapping areas of touch to one action or another. If you really want to do that, you should research touch listeners. But I would suggest you don't do that, and instead, if you want an image that behaves like a button, to put a button with an image on it. So instead of an EditText with a drawable, you have an ImageButton and an EditText.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote: If it doesn't do what you want then you should explain what you expect in more detail and what the code posted doesn't do or does incorrectly.

The Cross isn't a button, is an icon in the right side of an edit text.
i want how can i clear this edit text when i click on this cross?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarra Sakka wrote:
Steve Luke wrote: If it doesn't do what you want then you should explain what you expect in more detail and what the code posted doesn't do or does incorrectly.

The Cross isn't a button, is an icon in the right side of an edit text.
i want how can i clear this edit text when i click on this cross?


I realized that just after posting my last response... The icons are icons, not buttons, no events associated with them specifically. You could try to add a touch listener to the EditText and look at the X position of the ACTION_UP event to determine which icon the user was above when they clicked... but you should also then have pressed state feedback so the user knows that their press works, and you would need to accurately map different sized screens. But as I said in my edit above: if you want a button behavior, you should probably just use a button - perhaps an ImageButton so you can add a simple onClick listener.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for this reply
But i don't want put the cross in a button, i will search more, thank you
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use a button? I think you should step back and think about what you want to do not how (or how not) to do it. I think you are stuck in a partial solution which will make your task much harder rather than thinking about the problem that needs to be solved.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Steve
Here's a new solution which i use a FrameLayout as the followings:

And it's work now ;)
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are welcome.
I guess you need to learn how to programming Java and also how to be appreciate.

By the way, Steve you told me that not to help him but you gave him solution.
i don't get it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic