Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of intern Android drawables?

I've tried to put the intern icons of Android (anrdoid.R.drawable.bla) into an ImageButton and I wanted to change the color of Icon (not the Background!), but it doesn't work like I want to.

Here is what I've tried:

my ImageButton from the Layout:

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_marginTop="100dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/background"
    android:src="@android:drawable/ic_lock_silent_mode" />

what I've tried in my Activity:

Drawable myIcon = getResources().getDrawable( android.R.drawable.ic_lock_silent_mode); 
    ColorFilter filter = new LightingColorFilter( R.color.blue, R.color.blue);
    myIcon.setColorFilter(filter);

No matter what Values I've tried for the LightingColorFilter it always gives the same result. The icon inside the Imagebutton gets dark. But thats not what I wnated. I just wanted to apply a color from my colors.xml, it somehow doesnt work out.

Is this even the right direction I'm going? Or is there another opertunity (And I don't mean coloring myself in Photoshop and putting them into the drawables folder)

like image 368
user2043332 Avatar asked Jan 17 '26 18:01

user2043332


1 Answers

this worked for me, for example it will paint in dark gray:

ImageButton imgBtn = (ImageButton) findViewById(R.id.imageButton11); // image button
            imgBtn.setColorFilter(getResources().getColor(android.R.color.darker_gray), Mode.SRC_ATOP);
like image 108
Marko Niciforovic Avatar answered Jan 20 '26 08:01

Marko Niciforovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!