Customizing the setError icon for EditText - android

I want to set a setError method to my EditText, with a custom icon instead of the default Android icon. So I tried this:
((EditText)findViewById(R.id.edtTitle)).setError(getResources().getText(R.string.errEmptyTitle),
getResources().getDrawable(R.drawable.ico_warning_small);
It shows me the custom message, but not the custom icon. I tried this as well:
Drawable warning = (Drawable)getResources().getDrawable(R.drawable.ico_warning_small);
((EditText)findViewById(R.id.edtTitle))
.setError(getResources().getText(R.string.errEmptyTitle), warning);
Pretty much the same, but I still decided to give it a go. However this also didn't help - I still can't see the icon.
I tried to use some other Android system icon, just to see if I see them and no, I don't see them as well.
So what am I doing wrong? Is there any way to set that custom icon?

You need to set the bounds on the drawable before using it in setError.
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
editText.setError("error", drawable);

If you don't want to show any icon at all, use
editText.setError("error", null);

This issue is discussed and resolved here:
EditText setError() with icon but without Popup message
I hope that by extending the answer it would not get auto-converted to a comment.

Drawable customErrorDrawable = getResources().getDrawable(R.drawable.error_icon);
customErrorDrawable.setBounds(0, 0, customErrorDrawable.getIntrinsicWidth(), customErrorDrawable.getIntrinsicHeight());
editText.setError("please enter data",customErrorDrawable);

Related

android - setCompoundDrawables, setCompoundDrawablesWithIntrinsicBoundsis not show icon

When I setCompoundDrawables or setCompoundDrawablesWithIntrinsicBounds with a drawable in a EditText. Everytimes show layout of activity, I'm trying to add a x icon at the end of the text field but it's not working in first create. But refresh, icon x showing.
Do setCompoundDrawables set icon failed?
Please Help me.
It is sample problem same but i can't fix my problem: setCompoundDrawablesWithIntrinsicBounds is not working properly
drawXRemove = getResources().getDrawable(R.drawable.ic_iconX);
drawXRemove.setBounds(0, 0, drawXRemove.getIntrinsicWidth(), drawMarkXRemove.getIntrinsicHeight());
editText.setCompoundDrawables(null, null, drawXRemove, null);
You are setting Compound Drawable in wrong way. use:
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0,drawXRemove, 0);
OR
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0,R.drawable.ic_iconX, 0);
Try using icon directly from drawable as below
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.drawableRight, 0);
Thanks for help, I do it. It's problem when different func call set null for EditText.

Android: how to change dynamically the button icon color?

I have this button in my layout:
<Button
android:id="#+id/followers_right"
android:drawableLeft="#drawable/ic_team"
android:text="#string/visibility_right_followers" />
By default, all my icons are grey. But in some cases, I want to apply a specific color. And for the drawable button, how can I do that programmatically ?
myButton.setColorFilter(getResources().getColor(R.color.tint_selected));
This line doesn't work (build error).
Thanks for your help!
I tried to find a solution myself for this interesting question but I am not there yet. One work around is have the same icon in different colors as Drawable resources,
and change the icon of the button grammatically,
yourButtom.setCompoundDrawablesWithIntrinsicBounds(R.drawable.low, 0, 0, 0);
But the only issue is, if you want huge range of color chages

How to use font-awesome in edittext?

So far I've used the following code to add the icon
EditText email = (EditText) findViewById(R.id.email);
Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
email.setTypeface(font);
But the icon gets added as a value of the edittext field. Whereas I want the icon to appear at the left. I also know about the android:drawableLeft attribute but it requires I drawable resource i.e a jpeg and not a ttf. How can I solve this problem. Thanks
for font awesome in android apps i suggest https://github.com/JoanZapata/android-iconify
but that would not fix your issue as the icon still would be a part of the value.
to avoid this you might want to use https://github.com/DayS/EnhancedEditText which uses the android-iconify library to achieve what you want :)
good luck
Checkout the following Stack Overflow post.
How to use icon from font file as a drawable in Android
Then use it:
mEmailView = findViewById(R.id.login_email);
FontDrawable user = new FontDrawable(this, getString(R.string.fa_fa_user_circle_o), iconFont);
user.sizePx((int)mEmailView.getTextSize());
user.colorRes(R.color.white);
mEmailView.setCompoundDrawablesWithIntrinsicBounds(user, null, null, null);
How can I solve this problem.
Use a PNG or JPEG.
Or, create (or find) a Drawable that renders text in a font, and use setCompoundDrawables() to associate such a drawable with your EditText at runtime.

changing BulletSpan icon in Android

I am using BulletSpan(BulletSpan.STANDARD_GAP_WIDTH)
How can I change its icon from a circle to ✓?
SpannableString s = new SpannableString(text+"\n");
s.setSpan(new BulletSpan(BulletSpan.STANDARD_GAP_WIDTH), 0, text.length(), 0);
I was also trying to style the bullet icon used by Android but didn't find any answers online. I ended up using an alternative approach which may help you further as well.
I didn't use BulletSpan but I used LeadingMarginSpan instead. You can add whatever character you want as icon (such as your tick character) in the text to be "spanned". By setting the first line margin and rest margins correctly you can achieve the same effect as the BulletSpan.

left icon not visible

A have an Activity (minSDK for my app = 14) to which the #android:style/Theme.Holo.Dialog Theme is applied. I need to set an icon, so I used
this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
this.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
R.drawable.share);
I guess the icon is set (because the title moved to the right) but for some reason it is not visible as if it was transparent:
I can't find any logical explanation for this behavior. Any ideas?
P.S. no, my drawable is not the same color as the window's background
If I'm not mistaken then you have to set your icon resource on custom Dialog after calling show on it.
dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.share);
Its still a mystery why does it work and not the other way :)
it is order:
1 ) this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
2) this.setContentView(R.layout.xxx);
if is Dialog (dialog.show();)
3) this.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.share);

Categories

Resources