0

I have the following shape drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <solid
                android:color="#000000"
                />
            <size
                android:width="100dp"
                android:height="100dp"
                />
        </shape>
    </item>
</layer-list>

And it's called like this from my main layout:

<ImageView
    android:id="@+id/circle_bg"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:background="@drawable/circle_bg" />

I have an adapter that's used to populate the layout with other data, but one of these data is a hex color code. Here's an example of how I populate the layout's main content:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Post post = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_layout, parent, false);
    }

    TextView content = (TextView) convertView.findViewById(R.id.content);
    content.setText(article.getContent());

    return convertView;
}

Which populates this part of the layout:

<TextView
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    />

How can I, programmatically, do the same for the shape so that I can change its color?

user
  • 445
  • 1
  • 4
  • 8
  • [Set android shape color programmatically](http://stackoverflow.com/a/17823537/1160282) – SilentKiller Aug 19 '15 at 04:27
  • 1
    @SilentKiller I'm not dealing with gradients though. – user Aug 19 '15 at 04:29
  • There are other answers on the same question which describe how to change color with [ShapeDrawable](http://stackoverflow.com/a/17825210/1160282) checked that too ? – SilentKiller Aug 19 '15 at 04:31
  • 1
    @bpA Again, I'm not using a gradient. All of those answers relate to a gradient shape. – user Aug 19 '15 at 04:36
  • why are you using `layer-list` with **ONE** layer? are you calling `addLayer` / `setDrawable` later on? – pskink Aug 19 '15 at 05:54

2 Answers2

0

You can use ShapeDrawable to access the shape in your code and then change the color of the Shape in your getView method as:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

Post post = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_layout, parent, false);
    }

    ImageView content = (ImageView)convertView.findViewById(R.id.content);
    ShapeDrawable ovalShape = (ShapeDrawable)content.getBackGround();
    ovalShape.getPaint().setColor(Color.parseColor("hexcolor"));
    return convertView;
}

P.S. - This is a pseudocode

0
@Override
public View getView(int position, View convertView, ViewGroup parent) {

Post post = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_layout, parent, false);
    }

    ImageView content = (ImageView)convertView.findViewById(R.id.content);
    LayerDrawable ovalShape = (LayerDrawable)content.getBackGround();
    ovalShape.setColorFilter(Color.parseColor("hexcolor"), PorterDuff.Mode.SRC);
    return convertView;
}
  • Rather than filling the whole background, is it possible to fill only the shape? I plan to have an image within the shape later on and your method would mean that even the image would be overlapped by the color. – user Aug 19 '15 at 05:34
  • Did you try with that requirement you try first and if does not work then give me your code I will check – Narendra Kothamire Aug 19 '15 at 05:41