2

I have a shape which I need to change the colour of using the below code:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    LockListDataModel locks = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag
    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.list_row, parent, false);
        viewHolder.lockName = (TextView) convertView.findViewById(R.id.lockName);
        viewHolder.colour = (ImageView) convertView.findViewById(R.id.list_image);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }// Populate the data into the template view using the data object

    viewHolder.lockName.setText(locks.lockName);
    viewHolder.colour.setVisibility(View.VISIBLE);
    viewHolder.colour.setBackground(ResourcesCompat.getDrawable(getContext().getResources(), R.drawable.time_profile_shape, null));
    LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(getContext(),R.drawable.time_profile_shape);
    GradientDrawable color = (GradientDrawable)(shape.findDrawableByLayerId(R.id.time_profile_lock_colour));

    color.setColor(Color.parseColor(locks.color));

    // Return the completed view to render on screen

    return convertView;
}

For reference the shape looks like:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:left="2dp" android:top="2dp">
    <shape android:shape="rectangle" android:padding="10dp">
        <solid android:color="@color/colorPrimaryDark"></solid>

        <corners
            android:bottomRightRadius="5dp"
            android:bottomLeftRadius="5dp"
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp"/>
    </shape>
</item>
<item android:right="2dp" android:bottom="2dp" android:id="@+id/time_profile_lock_colour">
    <shape android:shape="rectangle" android:padding="10dp" >
        <solid android:color="@color/backgroundColour"></solid>

        <corners
            android:bottomRightRadius="5dp"
            android:bottomLeftRadius="5dp"
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp"/>
    </shape>
</item>

This is populating a Custom List Adapter for a List View, and is working on all API levels 18(Minimum SDK)+ except 21 & 22 (Lollipop).

API 18 = Works, colour changes

API 19 = Works, colour changes

API 21 = Colour doesn't change

API 22 = Colour doesn't change

API 23 = Works, colour changes

How can I get this working on Lollipop?

Thanks

SammyG
  • 299
  • 1
  • 4
  • 15
  • what are you doing with `LayerDrawable shape` later on? – pskink Apr 27 '16 at 12:55
  • Nothing as such, I have updated to show the full method that is within the Adapter – SammyG Apr 27 '16 at 13:06
  • well, so you should really ask why is it working on API 18, 19 & 23, since you are calling some sort of `Context#getDrawable` and dont "attach" returned `Drawable` to anything – pskink Apr 27 '16 at 13:11
  • What do you mean by "Attached"? The shape is set with `setBackground()` Then I am just modifying the value of the colour from the default, which on the other API's pick up instantly – SammyG Apr 27 '16 at 13:26
  • 1
    you are calling `viewHolder.colour.setBackground` with the different instance of the `Drawable` (i assume that `ResourcesCompat.getDrawable` and `ContextCompat.getDrawable` return different `Drawable` each time they are called) – pskink Apr 27 '16 at 13:30
  • You sir, I thank you! Changed `setBackground` to `viewHolder.colour.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.time_profile_shape));` and seems to be working great. Was a silly mistake! Please post as an answer and I can accept it :) – SammyG Apr 27 '16 at 13:38

0 Answers0