├── LICENSE
├── README.md
└── net
└── shikii
└── widgets
└── SAutoBgButton.java
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (C) 2011 by BJ Basañes, http://shikii.net
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## SAutoBgButton for Android
2 |
3 | A subclass of the Button widget that automatically darkens the button's background drawable when pressed, and sets it to transparent when disabled. This is inspired by iOS' automatic handling of custom background images for buttons.
4 |
5 | See the blog post [here](http://shikii.net/blog/android-button-background-image-pressedhighlighted-and-disabled-states-without-using-multiple-images/)
6 |
7 | ### Usage
8 |
9 | 1. Copy and paste the `net` folder into your Android project's `src` folder.
10 | 2. Replace your button declarations to use `net.shikii.widgets.SAutoBgButton` instead of just `Button`:
11 |
12 | From this:
13 |
14 |
18 |
19 | To this:
20 |
21 |
25 |
26 | Here's a sample output using this custom button:
27 |
28 | 
29 |
--------------------------------------------------------------------------------
/net/shikii/widgets/SAutoBgButton.java:
--------------------------------------------------------------------------------
1 | package net.shikii.widgets;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.graphics.ColorFilter;
6 | import android.graphics.LightingColorFilter;
7 | import android.graphics.drawable.Drawable;
8 | import android.graphics.drawable.LayerDrawable;
9 | import android.util.AttributeSet;
10 | import android.widget.Button;
11 |
12 | /**
13 | * Applies a pressed state color filter or disabled state alpha for the button's background
14 | * drawable.
15 | *
16 | * @author shiki
17 | */
18 | public class SAutoBgButton extends Button {
19 |
20 | public SAutoBgButton(Context context) {
21 | super(context);
22 | }
23 |
24 | public SAutoBgButton(Context context, AttributeSet attrs) {
25 | super(context, attrs);
26 | }
27 |
28 | public SAutoBgButton(Context context, AttributeSet attrs, int defStyle) {
29 | super(context, attrs, defStyle);
30 | }
31 |
32 | @Override
33 | public void setBackgroundDrawable(Drawable d) {
34 | // Replace the original background drawable (e.g. image) with a LayerDrawable that
35 | // contains the original drawable.
36 | SAutoBgButtonBackgroundDrawable layer = new SAutoBgButtonBackgroundDrawable(d);
37 | super.setBackgroundDrawable(layer);
38 | }
39 |
40 | /**
41 | * The stateful LayerDrawable used by this button.
42 | */
43 | protected class SAutoBgButtonBackgroundDrawable extends LayerDrawable {
44 |
45 | // The color filter to apply when the button is pressed
46 | protected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1);
47 | // Alpha value when the button is disabled
48 | protected int _disabledAlpha = 100;
49 | // Alpha value when the button is enabled
50 | protected int _fullAlpha = 255;
51 |
52 | public SAutoBgButtonBackgroundDrawable(Drawable d) {
53 | super(new Drawable[] { d });
54 | }
55 |
56 | @Override
57 | protected boolean onStateChange(int[] states) {
58 | boolean enabled = false;
59 | boolean pressed = false;
60 |
61 | for (int state : states) {
62 | if (state == android.R.attr.state_enabled)
63 | enabled = true;
64 | else if (state == android.R.attr.state_pressed)
65 | pressed = true;
66 | }
67 |
68 | mutate();
69 | if (enabled && pressed) {
70 | setColorFilter(_pressedFilter);
71 | } else if (!enabled) {
72 | setColorFilter(null);
73 | setAlpha(_disabledAlpha);
74 | } else {
75 | setColorFilter(null);
76 | setAlpha(_fullAlpha);
77 | }
78 |
79 | invalidateSelf();
80 |
81 | return super.onStateChange(states);
82 | }
83 |
84 | @Override
85 | public boolean isStateful() {
86 | return true;
87 | }
88 | }
89 |
90 | }
91 |
--------------------------------------------------------------------------------