Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the color of a 9patch image Android ?

I don't really understand how to create a 9patch image, but I found an image which is working on my fragment. The problem is that the color outside the border is not the color of the background. I tried changing the color of the pixels from the image to the color of the background but the resulting image doesn't work anymore. This is the image which is working but has the wrong color:

http://i.stack.imgur.com/cJBfV.png

How can I change the color of the pixels that are outside the border, or how can I create a new 9patch image that looks like that ?

like image 283
Bogdan Daniel Avatar asked May 30 '15 21:05

Bogdan Daniel


2 Answers

You can use DrawableCompat with supprot v4.

The next code shows how you can change Toast color.
As you know, toast background is a 9patch Drawable named toast_frame.9.png (you can find it in your sdk dir).

Toast toast = Toast.makeText(this, content, Toast.LENGTH_SHORT);
toastView.findViewById(android.R.id.message);
View toastView = toast.getView();
Drawable toastBg = toastView.getBackground();
Drawable drawable = tintDrawable(toastBg, ColorStateList.valueOf(Color.RED));
toastView.setBackground(drawable);
toast.setView(toastView);
toast.show();

public  Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, colors);
    return wrappedDrawable;
}

If you like to learn more, click this:
http://www.race604.com/tint-drawable

like image 200
didikee Avatar answered Nov 17 '22 20:11

didikee


You can edit the 9 patch using the Draw 9-patch tool that is provided with the Android SDK. However as your image already includes the 9 patch stretchable area you can just edit the colors in an image editor, such as GIMP or Photoshop. Ensure you rename your image to use the .9.png extension to allow it to be recognized as a 9 patch image.

http://developer.android.com/tools/help/draw9patch.html

like image 36
BrentM Avatar answered Nov 17 '22 20:11

BrentM