Faisal Shaikh
Faisal Shaikh

Reputation: 4157

How to change background color of drawable shape in custom adapter

I have a button in my custom listview item for which I am using following drawable xml file:

rounded_corner.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp" android:layout_height="100dp">

    <stroke
        android:width="1dp"
        android:color="#FFFFFF" />

    <solid android:color="#002832" />

    <padding
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners android:radius="5dp" />

</shape>

I have used "#002832" color for that drawable. Now, I want to change the color of drawable file programmatically. How can i do this?

PLEASE STOP MARKING AS DUPLICATE WITHOUT UNDERSTANDING THE QUESTION.

  1. I have checked @Ganesh Pokele SO link anf which totally different.

  2. I have checked @bizzard provided link but could not solve my issue.

Upvotes: 4

Views: 8036

Answers (2)

Er.Prem Singh daksha
Er.Prem Singh daksha

Reputation: 403

Solution

  1. If you want to change drawable color dynamical using programatically so use this code.

This is my xml rectangle_border


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="@color/gray"
    <corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp"
        android:topRightRadius="5dp" android:bottomRightRadius="5dp"/>
      <stroke android:color="@color/colorPrimary" android:width="1dp" />
</shape>

  1. Here i am change dynamical background color
 Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.rectangle_border);
            Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
            DrawableCompat.setTint(wrappedDrawable, Color.parseColor(textcolor));
            //Here set the background in view
            holder.textrectangle_bgLL.setBackgroundResource(R.drawable.rectangle_border);

Upvotes: 0

FabioR
FabioR

Reputation: 696

I described what you want in detail in this post, you might want to check it out, if I understood well your question.

Basically, what you should do is create another drawable with a different color and set it programmatically through yourView.setBackground(Drawable drawable):

another_round_corner.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp" android:layout_height="100dp">

    <stroke
        android:width="1dp"
        android:color="#002832" />

    <solid android:color="#002832" />

    <padding
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners android:radius="5dp" />

</shape>

Then set the background as this drawable whenever you want.

EDIT

As OP does not want to use another drawable, a solution would be to use a color filter like this:

button.getBackground().setColorFilter(Color.rgb(40, 50, 60), PorterDuff.Mode.SRC_ATOP);

You will obtain the desired effect. PorterDuff.Mode.SRC_ATOP will apply the color you want on the background, on top of another color, without mixing them. You have to pass the color in the first argument (the color will come from the server). If it's in hex, just convert it to RGB, in example, or do whatever transformation you need.

You can always change the drawable color programmatically like that, let me know if it works for you.

Let me know if it helps you, and upvote/select as correct answer if it did, cheers.

Upvotes: 12

Related Questions