Skip to content

Instantly share code, notes, and snippets.

@9re
Created December 7, 2011 11:14
Show Gist options
  • Save 9re/1442442 to your computer and use it in GitHub Desktop.
Save 9re/1442442 to your computer and use it in GitHub Desktop.
usage of StateListDrawable
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
public class StateListDrawableTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("click!");
button.setBackgroundDrawable(getStateListDrawable());
addContentView(button, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
private static Drawable getStateListDrawable() {
StateListDrawable drawable = new StateListDrawable();
int pressed = android.R.attr.state_pressed;
drawable.addState(new int[]{pressed}, new ColorDrawable(Color.RED));
drawable.addState(new int[]{-pressed}, new ColorDrawable(Color.BLUE));
return drawable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment