Now problem starts with just setting the color of a shape.
I created one rectangle shape in drawable
and set it to ImageView
like this
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="0.15"
android:layout_marginRight="10dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_priority"
android:src="@drawable/case_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
EDIT
My case_priority.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#009600" />
<size android:width="30dp"
android:height="30dp" />
</shape>
now I need to change the color of this shape. Actually this color is coming from web service, and I need to change it. So My Question is: How can we change color of this shape programmatically. and set it to imageview. I have gone through few examples in stackoverflow but I am not able to change it to image view.Please Help!!!! Thanks in advance.
EDIT:
May be I was not clear in my question, So Just a refinement to question . Actually I have a ImageView where I set a source which is actually a drawable. By default its green as in my code shown above. Now web service returns the color of this image view which needs to be changed accordingly , so I can't change it from xml during runtime so I need to change it from java file. Since I am using shape and image view, .setBackground() won't help much here. So What can be the probable solution for this. Thanks for answer and sorry for inconvenience.
Thanx for all the answers.It helped me getting to answer.
In My layout I changed android:src="" to android:background="@drawable/case_priority"
And in my java file I n=included this code:->
Resources res = getResources();
final Drawable drawable = res.getDrawable(R.drawable.case_priority);
drawable.setColorFilter(Color.YELLOW, Mode.SRC_ATOP);
ImageView img = (ImageView)findViewById(R.id.iv_priority);
img.setBackgroundDrawable(drawable);
that's it. Hope it will someone.
Have a look here
http://developer.android.com/guide/topics/graphics/2d-graphics.html#shape-drawable.
Instead of creating the Shape
in code you can use inflate
to inflate your XML if you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With