1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.widget;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.RemoteViews.RemoteView;
22 
23 /**
24  * <p>
25  * Displays a button with an image (instead of text) that can be pressed
26  * or clicked by the user. By default, an ImageButton looks like a regular
27  * {@link android.widget.Button}, with the standard button background
28  * that changes color during different button states. The image on the surface
29  * of the button is defined either by the {@code android:src} attribute in the
30  * {@code <ImageButton>} XML element or by the
31  * {@link #setImageResource(int)} method.</p>
32  *
33  * <p>To remove the standard button background image, define your own
34  * background image or set the background color to be transparent.</p>
35  * <p>To indicate the different button states (focused, selected, etc.), you can
36  * define a different image for each state. E.g., a blue image by default, an
37  * orange one for when focused, and a yellow one for when pressed. An easy way to
38  * do this is with an XML drawable "selector." For example:</p>
39  * <pre>
40  * &lt;?xml version="1.0" encoding="utf-8"?&gt;
41  * &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
42  *     &lt;item android:state_pressed="true"
43  *           android:drawable="@drawable/button_pressed" /&gt; &lt;!-- pressed --&gt;
44  *     &lt;item android:state_focused="true"
45  *           android:drawable="@drawable/button_focused" /&gt; &lt;!-- focused --&gt;
46  *     &lt;item android:drawable="@drawable/button_normal" /&gt; &lt;!-- default --&gt;
47  * &lt;/selector&gt;</pre>
48  *
49  * <p>Save the XML file in your project {@code res/drawable/} folder and then
50  * reference it as a drawable for the source of your ImageButton (in the
51  * {@code android:src} attribute). Android will automatically change the image
52  * based on the state of the button and the corresponding images
53  * defined in the XML.</p>
54  *
55  * <p>The order of the {@code <item>} elements is important because they are
56  * evaluated in order. This is why the "normal" button image comes last, because
57  * it will only be applied after {@code android:state_pressed} and {@code
58  * android:state_focused} have both evaluated false.</p>
59  *
60  * <p>See the <a href="{@docRoot}guide/topics/ui/controls/button.html">Buttons</a>
61  * guide.</p>
62  *
63  * <p><strong>XML attributes</strong></p>
64  * <p>
65  * See {@link android.R.styleable#ImageView Button Attributes},
66  * {@link android.R.styleable#View View Attributes}
67  * </p>
68  */
69 @RemoteView
70 public class ImageButton extends ImageView {
ImageButton(Context context)71     public ImageButton(Context context) {
72         this(context, null);
73     }
74 
ImageButton(Context context, AttributeSet attrs)75     public ImageButton(Context context, AttributeSet attrs) {
76         this(context, attrs, com.android.internal.R.attr.imageButtonStyle);
77     }
78 
ImageButton(Context context, AttributeSet attrs, int defStyleAttr)79     public ImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
80         this(context, attrs, defStyleAttr, 0);
81     }
82 
ImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)83     public ImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
84         super(context, attrs, defStyleAttr, defStyleRes);
85         setFocusable(true);
86     }
87 
88     @Override
onSetAlpha(int alpha)89     protected boolean onSetAlpha(int alpha) {
90         return false;
91     }
92 
93     @Override
getAccessibilityClassName()94     public CharSequence getAccessibilityClassName() {
95         return ImageButton.class.getName();
96     }
97 }
98