Issue
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.
I have checked @Ganesh Pokele SO link anf which totally different.
I have checked @bizzard provided link but could not solve my issue.
Solution
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.
Answered By - FabioR
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.