I've just been playing around with this solution and it works, sort
of, but feels a little hacky.

I was wondering if this alternative might work:

1. Use a LinearLayout and set the background to be the same
StateListDrawable used by EditText - I can't get this to work, but
then again I'm a newbie when it comes to StateListDrawables.
2. Add an EditText as the first child of the LinearLayout. Set the
background of the EditText to transparent.
3. Add an ImageView as the second child of the LinearLayout. Set the
src to be the "X" drawable.

This has the advantage of not having views on top of each other (where
one provides the image, and the other listens for the events).

Also, if, for example, the text is empty, then we can set the
visibility of the ImageView to GONE, so that the cross is no longer
visible AND the hint text has more space.

If this is a sensible approach, would appreciate some pointers on how
to configure the LinearLayout so its "behaves" like an EditText...

On Sep 28, 3:53 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> mjc147 wrote:
> > To answer my own question...
>
> > Use TextView.setCompoundDrawables(null, null, null, null) to hide the
> > cross.
>
> > Next thing is to detect when the cross is pressed on.
>
> > Currently I do this:
>
> > mEditText.setOnTouchListener(new OnTouchListener() {
> >    public boolean onTouch(View v, MotionEvent event) {
> >            if (mEditText.getCompoundDrawables()[2] == null) {
> >                    // cross is not being shown so no need to handle
> >                    return false;
> >            }
> >            if (event.getAction() != MotionEvent.ACTION_DOWN) {
> >                    // only respond to the down type
> >                    return false;
> >            }
> >            if (event.getX() > mEditText.getMeasuredWidth() -
> > mEditText.getPaddingRight() - x.getIntrinsicWidth()) {
> >                    mEditText.setText("");
> >                    return true;
> >            }
> >            else {
> >                    return false;
> >            }
> >    }
> > });
>
> > This works but feels rather messy to me. Also, I'm not sure about the
> > mEditText.getPaddingRight()...
>
> > Is there a better way?
>
> Possibly, now that you sent the screenshot (thanks!). BTW, what you are
> seeing may be part of the HTC Sense UI -- it's certainly not what you
> get in ordinary Android.
>
> Regardless, here is how I would proceed to try to get this to work:
>
> Step #1: Keep your current Drawable code, but replace your X with a 100%
> transparent image of the same size. There might be a better way to
> accomplish this step, but I can't think of one off the top of my head.
> Basically, I am guessing that if you type a lot into that field on your
> Hero, the text does not wind up under the X, so you need a placeholder
> to keep the text to the left.
>
> Step #2: Wrap your EditText in a RelativeLayout. Put whatever layout
> rules you presently have on the EditText (layout_width, etc.) on the
> RelativeLayout, and set the EditText to be fill_parent/fill_parent for
> width/height.
>
> Step #3: In the same RelativeLayout, *after* the EditText in the XML,
> add an ImageView with your X, with layout_alignParentLeft="true" and
> enough paddingRight to have it positioned properly. RelativeLayout, like
> FrameLayout, supports "vertically" stacking widgets, so by having your
> ImageView be after the EditText in the XML, it will "float" over the
> EditText and be visible/touchable.
>
> Step #4: Add an OnTouchListener to the ImageView in your code.
>
> A variation on this approach would be to use the X image in Step #1 and
> a transparent View in Step #3. This is the old "hot-spot" trick from a
> byegone programming era -- a background provides the image and you have
> something floating over top where things are touchable. If you specify a
> View with background=#00000000, it should be invisible, and if you
> size/position it properly, it will sit over top your X. It's a bit more
> difficult to debug, though you could temporarily use a translucent
> background (say, #22FF0000 for a slight red effect) until you get the
> hot-spot positioned properly.
>
> These approaches have the advantage of avoiding the OnTouchListener
> calculations you are doing in the EditText, which might have problems if
> EditText changes behavior in future releases. On the other hand, these
> approaches add a few more widgets and therefore take up a bit more memory.
>
> If the X appears to actually "click" when tapped on your Hero, then they
> may be using an ImageButton with a custom set of backgrounds rather than
> an ImageView. That is also possible to implement independently but gets
> decidedly more painful.
>
> Once I get my hands on an HTC Sense-ible device, I may try to cook up
> some canned widgets to offer some of their looks, like this one, in
> plug-and-play form.
>
> A question for you: does this "X" effect show up in places other than
> the Android Market search app on your Hero? Thanks!
>
> --
> Mark Murphy (a Commons 
> Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> _Android Programming Tutorials_ Version 1.0 Available!

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to