Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tint a nine-patch image?

Tags:

android

I've learned how to tint an image when it's pressed by adding an OnTouchListener in which I call setColorFilter on the image.

I've learned how to use nine-patch images as the background for Buttons and ImageButtons.

The problem is, nine-path images don't stretch if they're the src for an ImageButton, and the tint has no effect on the background image.

How can I tint a nine-patch ImageButton when it's pressed? Any suggestions?

like image 383
Chad Schultz Avatar asked Jan 18 '23 12:01

Chad Schultz


1 Answers

I know it's an old question but it seems that it's the only one on this topic.

You can tint nine patch images by creating an xml resource file containing a nine patch node and a tint node. Example :

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/nine_patch_image"
        android:tint="@android:color/black"/>

You may then reference this file in your background attributes :

<LinearLayout
    android:id="@+id/some_id"
    android:background="@drawable/xml_filename">

Hope this will help someone

like image 133
Nicolas Gomez Avatar answered Jan 29 '23 00:01

Nicolas Gomez