1

I have a bunch of drawables (icons) defined similar to this (from Google's Open Source Material Icons):

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M20,4H4c-1.11,0 -1.99,0.89 -1.99,2L2,18c0,1.11 0.89,2 2,2h16c1.11,0 2,-0.89 2,-2V6c0,-1.11 -0.89,-2 -2,-2zm0,14H4v-6h16v6zm0,-10H4V6h16v2z"/>
</vector>

I need to change the color via styling. It is used in code as follows:

            <ImageView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:contentDescription="@string/cd.icon_amount"
                android:src="@drawable/ic_local_atm_24dp"/>

But how do you change the color in the ImageView via an external style file?

For example, how do I apply the below XML snippet?

<style name="icon">
    <item name="color">@color/grey</item>
</style>

Update 1: I wanted to change the color in XML via a style section in an external file than the shape file. I do not want it done programmatically.

noobsy
  • 11
  • 3

2 Answers2

0

This answer is from here

ImageView b = new ImageView(new ContextThemeWrapper(this, R.style.ButtonText), null, 0);

Use a ContextThemeWrapper

AND

Use the 3-arguments constructor (won't work without this)

Community
  • 1
  • 1
Inducesmile
  • 2,475
  • 1
  • 17
  • 19
  • Sorry, if it wasn't clear, but I wanted to style it in XML, but not in the actual shape file itself. Thank for the help though. – noobsy Mar 08 '16 at 05:55
0

If you want to do this programmatically you can do this :

First add an id to your ImageView as follows :

 <ImageView
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:id="@+id/ivTest"
            android:contentDescription="@string/cd.icon_amount"
            android:src="@drawable/ic_local_atm_24dp"/>

Then follow those steps :

ImageView iv = (ImageView)findViewById(R.id.ivTest);
GradientDrawable bgShape = (GradientDrawable)iv.getDrawable();
bgShape.setColor(ContextCompat.getColor(this, R.color.color));
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Sorry, if it wasn't clear, but I wanted to style it in XML, but not in the actual shape file itself. Thank for the help though. – noobsy Mar 08 '16 at 05:55
  • So you mean from the XML with the imageview change the color of shape? – Skizo-ozᴉʞS ツ Mar 08 '16 at 06:04
  • Something like that yeah. Basically I want to be able to change the color in style file at a later date without editing the shape file. – noobsy Mar 08 '16 at 06:07
  • Have you tried my answer tho? I'm not sure that what you want its possible... I'll study it – Skizo-ozᴉʞS ツ Mar 08 '16 at 06:10
  • The problem with your approach is that it doesn't separate the formatting from the code which was the intention, Otherwise, I would just change it directly in the drawable XML file. I appreciate you helping me! :) – noobsy Mar 08 '16 at 20:08