Reputation: 1217
I am using this PNG in my project (within my resources):
Programmically, I want to achieve this:
The result should look to something like this:
The red color has to be changeable dynamically at runtime. How do I achieve this?
Upvotes: 0
Views: 57
Reputation: 1697
Define a drawable that you can use as your background, for example:
<!-- res/drawable/the_drawable_above.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" android:padding="10dp">
<stroke android:width="1dp"
android:color="@color/colorLightGrey"/>
<solid
android:color="@color/colorAccent"/>
</shape>
For your image view:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/your_image" scale_type="fit_center" android:tint="@android:color/white" android:background="@drawable/the_drawable_above"/>
Upvotes: 0
Reputation: 20644
Add this to your XML file:
<ImageView
android:id="@+id/image_view"
android:layout_width="56dp"
android:layout_height="56dp"
android:background="@drawable/circle"
android:padding="8dp"
android:src="@drawable/bull_image"
app:tint="@android:color/white" />
This is circle.xml
:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@android:color/white"/>
</shape>
And to change the background color dynamically:
imageView.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.your_color))
Upvotes: 2
Reputation: 1737
Something like this shoud do the trick (adjust the cardCornerRadius to your liking):
<androidx.cardview.widget.CardView
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/red"
app:cardCornerRadius="10dp"
android:layout_width="wrap_content"
app:cardElevation="0dp">
<ImageView
android:src="your_image"
android:tint="@color/white" />
</androidx.cardview.widget.CardView>
Upvotes: 0