So, I've found the way to resolve it - create shapes/selector programmaticaly.
Now, I only got rounded_shape.xml with just a shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="@dimen/rounded_radius" />
<stroke android:width="1dip" />
And created static class for creating full selectors for the button:
public class DrawableUtils {
/**
* Blend {@code color1} and {@code color2} using the given ratio.
*
* @param ratio
* of which to blend. 1.0 will return {@code color1}, 0.5 will give an even blend, 0.0 will return
* {@code color2}.
*/
private static int blendColors(int color1, int color2, float ratio) {
final float inverseRation = 1f - ratio;
float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
return Color.rgb((int) r, (int) g, (int) b);
}
/**
* Create selector with specified {@code mainColor} (setting darkened {@code mainColor} for state_pressed)
*
* @param mainColor
* color of the view
*/
public static StateListDrawable getSelector(Context c, int mainColor) {
StateListDrawable selector = new StateListDrawable();
/*
* CAREFUL! We NEED to use 2 different objects instead of using 1 with different setColor(...) calls
*/
GradientDrawable stateSimple, statePressed;
// Getting pre-created shape
statePressed = (GradientDrawable) c.getResources().getDrawable(R.drawable.rounded_shape);
stateSimple = (GradientDrawable) c.getResources().getDrawable(R.drawable.rounded_shape);
// Setting color for state_pressed
statePressed.setColor(blendColors(mainColor, c.getResources().getColor(R.color.tinted_black), 0.5f));
selector.addState(new int[] { android.R.attr.state_pressed }, statePressed);
selector.addState(new int[] { -android.R.attr.state_enabled }, statePressed); // "-" is for "false"
// Setting color for main state
stateSimple.setColor(mainColor);
selector.addState(new int[] { android.R.attr.state_enabled }, stateSimple);
return selector;
}
}
Finaly, calling this from the GridView adapter:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.findViewById(R.id.rounded).setBackgroundDrawable(DrawableUtils.getSelector(mC, mColors[position]));
} else {
view.findViewById(R.id.rounded).setBackground(DrawableUtils.getSelector(mC, mColors[position]));
}