-1

In my android app, I need to set background inside an image resource. On attempting to do that, the spaces around the image takes the background as well, but I want the image background to be only inside the image.

<ImageView
                android:id="@+id/pen"
                android:layout_width="16dp"
                android:layout_height="16dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5px"
                android:src="@drawable/pencil"
                android:background="#000"
                android:layout_toRightOf="@id/upcount"/>

I have tried to do it programmatically as well but getting the same result

pen.setBackgroundColor(Color.rgb(100, 100, 50));

this is sample of what I try to achieve. let the background be only on the pencil

enter image description here

Sleek
  • 135
  • 2
  • 2
  • 9

1 Answers1

0

This is called tint in Android. You can use android:tint attribute. When you set tint on Image, then image color is changed.

    <android.support.v7.widget.AppCompatImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_foreground"
        android:tint="@color/colorAccent"/>

Use AppCompatImageView for better support of tint on older versions of Android.

You can set it programmatically by this answer.

imageView.setColorFilter(Color.argb(255, 255, 255, 255));

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • You got down-vote perhaps because your question was not clear. So please use always clear language and read question before posting, that your requirement is clear. – Khemraj Sharma Sep 10 '18 at 11:57