1

I have an imageButton with an icon:

        <ImageButton
            android:src="@drawable/ic_action_favorite"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/discussionActionBtn"/>

Currently, the icon's default color is gray. Is there some way to programmatically make the icon appear in another color ?

The best I can think of so far is to create another asset with the desired color, but I was wondering if there was another way (maybe using filters ?)

Running Turtle
  • 12,360
  • 20
  • 55
  • 73
  • There are posibilyties, but i'd totally not reccomend it since this would have to be done _every time_ a user clicked the button and even if it's not mutch work to do, this would sum up to a lot of wasted processing time. On an phone you have to save resources, and making a small png of a few KB and saving it is much more efficent than creating a colored image every time someone clicked a button. – Gumbo Sep 01 '14 at 20:50
  • you can take a look at this: http://stackoverflow.com/questions/11376516/change-drawable-color-programmatically. the problem with this approach though, is that when you set a ColorFilter to a Drawable, the changes will persist throughout all your Views that use the drawable. – chjarder Sep 02 '14 at 06:35
  • You could use a transparent icon and just change the background color of the button. That will show a color change but is a lot easier and faster. – Gravitoid Sep 02 '14 at 22:07

3 Answers3

0
<Button
android:background="@android:color/white"
android:textColor="@android:color/black"
/>

Or through coding btnSample.setBackgroundColor(Color.WHITE); btnSample.setTextColor(Color.BLACK);

Prasanth Louis
  • 4,658
  • 2
  • 34
  • 47
0

If you want to do it programmatically then you could use a small class like this which gives a nice border to your ImageButton.

       public class BorderDrawable extends BitmapDrawable {
        private static final int BORDER_WIDTH = 9;
        private final int[] GRADIENT_COLORS = {Color.GRAY,Color.BLUE, Color.GREEN, Color.RED};
        Paint borderPaint;

        public BorderDrawable(Resources res, Bitmap bitmap) {
            super(res, bitmap);
            this.borderPaint = new Paint();
            borderPaint.setStrokeWidth(BORDER_WIDTH);
            borderPaint.setStyle(Paint.Style.STROKE);

            // set border gradient
            Shader shader = new LinearGradient(0, 0, 0, getBounds().bottom, GRADIENT_COLORS, null,  Shader.TileMode.CLAMP);
            borderPaint.setShader(shader);

            // or the border color
            // borderPaint.setColor(Color.GREEN);

        }

        @Override
        public void draw(Canvas canvas) {
            super.draw(canvas);
            // draw
            canvas.drawRect(getBounds(), borderPaint);
        }
    }

And you can use it something like this.

Bitmap bmp = /*your bitmap*/;
BorderDrawable drawable = new BorderDrawable(getResources(), bmp);
discussionActionBtn.setImageDrawable(drawable);

Hope i am not off the topic.I guess this could help you.

Sash_KP
  • 5,551
  • 2
  • 25
  • 34
0
Drawable img = mContext.getDrawable(R.drawable.ic_icon);
img.setTint(Color.parseColor(myColor));
btnOne.setBackground(img);