Skip to content

Instantly share code, notes, and snippets.

@ckurtm
Created October 29, 2015 08:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckurtm/8e185ad9b4df9c0f98d1 to your computer and use it in GitHub Desktop.
Save ckurtm/8e185ad9b4df9c0f98d1 to your computer and use it in GitHub Desktop.
Dynamically change color of a drawable using TintDrawables
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.peirra.notification.InfoScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/container"
android:gravity="center"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image1"
android:src="@drawable/ic_android" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image2"
android:src="@drawable/ic_android" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image3"
android:src="@drawable/ic_android" />
</LinearLayout>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Color"
android:id="@+id/button"
android:layout_below="@+id/container"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
</RelativeLayout>
�PNG

IHDRTTk�9IDATx^��o�Tǿ�M�&�
�8�^�J#$�8iD���@�p�O�D%Z�Z ���"��%�?���Vj����=rB��i��y��e7�]�ڻh|��;_���<�;�Qd�M͠@ �U�� ��
U�� ��
U�� ��
U�� ��
�?�=�8^�<0��5�:�y�,��<�&"��Ʉo;�T��9���V��7 �d�Z�Ǘ@N|�����E;0Lh :99ypm��om���>�I�!+��x2��D�������6?�g
�&������ss�& ��Q�$����OT<�ԡ2�����z�oZ6ob0��%�o|��}��,f2�€6�
��v*")�zL����\(�$�Q�;����&�F�CX����_2�d
/��ty�c
�kd��<
^%����M^Yt�\>N}�PG���c_�T���Ԟ�z��J?2�(�6E�Λ�c���m�f��s��x5s��.D~8�X���<w�c^y<f�1"|��g��m�a �(k�z�Yf�i�a��J^�x����v��^�(�kQN��%�����ԑR��k����DXJ�nVm��T��~,��D��R��͋��$0Lhr:��a��L$.� �w����R}V]�@m�|��I�� QP�i/�su�@�d�?�y�E��ю";��?;�3K��(f1h'6W�#�sƲ�m��1x% k7�<�\��1��ϡ@ ƀڮ;�� 1�@_�� ��i��_@�Lj�U�{�u��~�j+|�NGAP{�z��
�v$D�6�é�Y=� ���:�L�ȓ��S&���87�K _�E���_��c�w*��r�*�QP�
u���Tg=3>����v�t�)4�F���t��P']���j�߫+��_k;��̔W�u�
t[)j�
�Ny!(�L�JI u
TJ*S�RRB�������P�@���2*%%�)P!(�L�JI u
TJ*S�RRB����z�v� �CuM�0�GKȽ���9�`���r�m��Ir��d����"V{b]�qķ�n]^zs�]'y��ToH��S����1����Y� /�Jò��G~�i6yt�@��F���x�@���=�׷��CbF���5I}�8�ʄ[+ �dOnG�@G�����k�i\�7>���X��B_�f��$V�*��/y�O���m<z N%�) AIe
TJJ�S�BPR�-l�~yl����LW�c����&�Ƕ��*l�m$ُ���@��%�����j�ch��<�,��¯��$k>
4+��x�@ 0l��@ 0l��@ 0l��@ 0l�j迲�5����IEND�B`�
<resources>
<string name="app_name">Tint</string>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
package com.peirra.tint;
import android.graphics.Color;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.util.Random;
public class TintActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tint);
final ImageView image1 = (ImageView) findViewById(R.id.image1);
final ImageView image2 = (ImageView) findViewById(R.id.image2);
final ImageView image3 = (ImageView) findViewById(R.id.image3);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setImageColor(image1,randomColor());
setImageColor(image2,randomColor());
setImageColor(image3,randomColor());
}
});
}
public static void setImageColor(ImageView image,int color){
DrawableCompat.setTint(image.getDrawable(), color);
}
public int randomColor() {
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment