Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of shape set in imageview

Tags:

android

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.

like image 893
Android Avatar asked May 16 '12 12:05

Android


2 Answers

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.

like image 136
Android Avatar answered Oct 02 '22 19:10

Android


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.

like image 37
techiServices Avatar answered Oct 02 '22 20:10

techiServices