0

Need to place icon in center of the oval with solid color. It also needs to allow dynamically change the oval's color.

Could show the icon on top/center of the oval like this by Jérémie Laval:

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="#42000000" />
    <padding android:left="8dp" android:top="8dp"
         android:right="8dp" android:bottom="8dp" />
</shape>

<FrameLayout
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:layout_gravity="center"
   android:background="@drawable/aitemring">
   <ImageView
   android:src="@drawable/coffee"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:scaleType="center" />
</FrameLayout>

The question how to change the color of the oval shape in code (kets say got new color #ff0000, or any non-predefined color)?

lannyf
  • 9,865
  • 12
  • 70
  • 152
  • Looks like there is already a good answer on http://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically – David Kibblewhite May 13 '15 at 14:37
  • thanks for point to it. But it does not apply here ShapeDrawable shapeDrawable = (ShapeDrawable)imageView.getBackground();, changing the image's background does not work. – lannyf May 13 '15 at 14:42
  • Thanks David again! Looked again it should work if take background from the FrameLayout, and change its color. – lannyf May 13 '15 at 15:29

2 Answers2

0

Have you thought about just defining two drawables and swapping them when you need to change the color?

You can do a layer-list to create your shape and add the icon.

mjstam
  • 1,049
  • 1
  • 6
  • 5
  • not how swapping two drawables will help here. the color is random and will be picked up at runtime. – lannyf May 13 '15 at 20:04
0

With David's hint, this works:

<?xml version="1.0" encoding="UTF-8" ?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="#42000000" />
    </shape>


<ImageView
    android:id="+id/theIcon"
    android:src="@drawable/coffee"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="center"
    android:background="@drawable/aitemring" />

GradientDrawable shapeDrawable = (GradientDrawable)theIcon.getBackground();
    shapeDrawable.setColor(0xffff0000);
lannyf
  • 9,865
  • 12
  • 70
  • 152