Hi UI Framework Gurus! I'm trying to create a drawable list in code to act as the selector for a button. Because of the special requirements of my application it needs to happen all in code so I can't use the XML or inflated layouts, which is too bad because I've seen that approach working. What I was attempting to do was make a multi-layer drawable, the inside of a button with a solid color, then the outside, using a gradient and a brush stroke.
What I'm finding though is that when I apply the layer to the state list,
the image shrinks. I'm confused as to what is going on since both images
seem to work as I expected outside of the layer. Maybe I need to
explicitly set the drawable bounds somehow? I'm not sure but I tried a
couple different ways with no luck. I am attaching a small amount of code
that demonstrates the issue, and a couple screen shots. It would be
awesome if someone could chime in.
Thanks!
public class Main extends Activity {
private StateListDrawable mStateListDrawable = new StateListDrawable();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LinearLayout main = (LinearLayout) findViewById(R.id.
main_layout);
ViewTreeObserver vto = main.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View v = findViewById(R.id.one_big_button);
float[] rounded = new float[] {12,12,12,12,12,12,12,12};
Shader shader = new LinearGradient(0,0,v.getWidth(),v.getHeight(),
new int[] {0xFF006600, 0xFF00ff00, 0xFF00ff00, 0 },
null, Shader.TileMode.CLAMP);
ShapeDrawable defaultDrawable = new
ShapeDrawable(newRoundRectShape(rounded,
null, null));
defaultDrawable.getPaint().setColor(0xff00ff00);
ShapeDrawable pressedDrawableOutline = new
ShapeDrawable(newRoundRectShape(rounded,
null, null));
pressedDrawableOutline.getPaint().setShader(shader);
pressedDrawableOutline.getPaint().setStyle(Style.STROKE);
pressedDrawableOutline.getPaint().setStrokeWidth(3);
ShapeDrawable pressedDrawable = new
ShapeDrawable(newRoundRectShape(rounded,
null, null));
pressedDrawable.getPaint().setColor(0xff006600);
Drawable[] drawables = new Drawable[2];
drawables[0]= pressedDrawableOutline;
drawables[1]= pressedDrawable;
LayerDrawable layers = new LayerDrawable(drawables);
//when i apply the list it breaks
mStateListDrawable.addState(new int[] {android.R.attr.state_pressed},
layers);
//but if i do it like this it's okay...
//mStateListDrawable.addState(new int[]
{android.R.attr.state_pressed}, pressedDrawable);
mStateListDrawable.addState(new int[0], defaultDrawable);
mStateListDrawable.setBounds(0, 0, v.getWidth(), v.getHeight());
v.setBackgroundDrawable(mStateListDrawable);
}
});
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en<<attachment: ok.png>>
<<attachment: no.png>>

