Reputation: 19938
Is there anyway to set alpha on a drawable sitting within an editText as a drawableLeft?
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edittext_middle_bg"
android:id="@+id/birthday_overlay"
android:editable="false"
android:focusable="false"
android:padding="10dp"
android:hint="Birthday"
android:textColorHint="#bbbbbb"
android:drawablePadding="10dp"
android:drawableLeft="@drawable/ic_cake_black_24dp"/>
I got the icon from the material icon library by Google. The icon is where alpha is 1 (so full black). I want to make it slightly grey by making alpha 0.5 for example.
I guess I can use GIMP / Photoshop to change the alpha but would prefer to use android to do this programmatically.
Upvotes: 3
Views: 2991
Reputation: 11
I search many and found this unique - for my research - solution:
I make with a CountDownTimer, work good.
Drawable[] compundDrawables = textview_with_drawableLef_Image.getCompoundDrawables();
Drawable leftCompoundDrawable = compundDrawables[0];
And after in a countdowntimer, I blink the leftCompoundDrawable
setting the:
leftCompoundDrawable.setApha(AlphaNumber);
Work Good, Need a little adjust of countDownInterval and Alpha Value Step, check when alpha is minor to 0 or more to 255 (Alpha in Drawables is from 0 to 255) and change step alpha to + or --, but is easy to adjust for good look.
The CountDownTimer is very usseful in some cases.
Upvotes: 0
Reputation: 1061
Drawable drawable = getResources().getDrawable(R.drawable.yourdrawable);
// set opacity
drawable.setAlpha(10);
//Set it
button.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0);
Upvotes: 1
Reputation: 19938
So this is eventually what I did to achieve my desired outcome.
Drawable[] birthdayDrawable = birthdayOverlay.getCompoundDrawables();
birthdayDrawable[0].setAlpha(128); //set it at 128 so that it has 50% opacity. The opactiy here ranges from 0 to 255.
Upvotes: 4
Reputation: 3118
@SuppressLint("NewApi")
public static void setAlpha(View view, float alpha){
if (Build.VERSION.SDK_INT < 11) {
final AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
animation.setDuration(0);
animation.setFillAfter(true);
view.startAnimation(animation);
}
else
view.setAlpha(alpha);
}
Use this method. Pass the argument as your drawableLeft from the EditText. This works for <= API 16 and > API 16.
Upvotes: 1