Bug 4788 - EditText .SetError doesn't show the android error icon
Summary: EditText .SetError doesn't show the android error icon
Status: RESOLVED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: Bindings ()
Version: 4.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Atsushi Eno
URL:
Depends on:
Blocks:
 
Reported: 2012-05-01 03:15 UTC by Edwin Klesman
Modified: 2016-09-02 03:42 UTC (History)
5 users (show)

Tags: XATriaged
Is this bug a regression?: ---
Last known good build:


Attachments
error icon drawable (956 bytes, image/png)
2012-05-01 03:15 UTC, Edwin Klesman
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Edwin Klesman 2012-05-01 03:15:44 UTC
Created attachment 1783 [details]
error icon drawable

The function to set the error for an EditText field that shows the fly over indication and a red error icon is not working as expected in the Mono for Android sdk.

There is no function that only takes an error string (and uses the standard Android error icon) and when i give along a drawable to use as icon in the EditText field it won't show:

element.SetError(errorMessage,Resources.GetDrawable(Resource.Drawable.error_icon)); 

The errorMessage is shown allright, but the drawable error_icon (see attachment) won't show up.

I really want to show the icon since it is a clear indicator for the user that something is (still) wrong in one or more edittext fields.
Comment 1 Jonathan Pobst 2012-05-01 14:28:09 UTC
This is kinda confusing, because the overload that only takes a string got changed into a property, so if you want to use the default Android icon, do this:

element.Error = errorMessage;

or if you have a formatted string:

element.ErrorFormatted = errorMessage;

---

Still need to look into why SetError (string, Drawable) isn't working as expected, but maybe this will let you work around that.
Comment 2 Edwin Klesman 2012-05-02 01:59:16 UTC
I was getting around using a custom icom using

element.SetError(errorMessage, null); // passing an icon ref here doesn't work element.SetCompoundDrawablesWithIntrinsicBounds(0,0,Resource.Drawable.error_icon,0);	

but i just confirmed that using .Error works (nice!) and shows the - more crisp - default icon.

it's just a tad more confusing since all the Android examples use setError and there is no single parameter overload. Is making one-parameter functions properties common practice for mono for android? if so, i think i'll be able to switch a little faster next time ;)
Comment 3 Atsushi Eno 2012-05-30 13:15:28 UTC
While I couldn't find out the reason why it doesn't work yet, I found a workaround that gets SetError(string,Drawable) as expected: use dummy ImageView and hold the drawable in it, like:

			SetContentView (Resource.Layout.Main);

			Android.Graphics.Drawables.Drawable icon;
			icon = Resources.GetDrawable (Resource.Drawable.error_icon);
			var iv = new ImageView (this); // dummy
			this.AddContentView (iv, new ViewGroup.LayoutParams (0, 0)); // dummy
			iv.SetImageDrawable (icon);
			iv.Visibility = ViewStates.Invisible;
			var ed = FindViewById<EditText> (Resource.Id.myEdit);
			
			ed.SetError ("error by default", icon);

I assume this is some native resource preservation issue.
Comment 4 Atsushi Eno 2016-09-02 03:42:40 UTC
It is due to wrong use of API. The documentation explicitly says:

"The drawable must already have had setBounds(Rect) set on it."

https://developer.android.com/reference/android/widget/TextView.html#setError(java.lang.CharSequence,%20android.graphics.drawable.Drawable)

If you add call to SetBounds(), it expectedly shows the icon. The workaround code above must be internally doing that too.