The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.util.AttributeSet; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 21 | import android.view.accessibility.AccessibilityEvent; |
| 22 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.widget.RemoteViews.RemoteView; |
| 24 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | /** |
| 26 | * <p> |
Scott Main | 84971ae0 | 2009-05-18 16:05:52 -0700 | [diff] [blame] | 27 | * Displays a button with an image (instead of text) that can be pressed |
| 28 | * or clicked by the user. By default, an ImageButton looks like a regular |
| 29 | * {@link android.widget.Button}, with the standard button background |
| 30 | * that changes color during different button states. The image on the surface |
| 31 | * of the button is defined either by the {@code android:src} attribute in the |
Ben Dodson | 4e8620f | 2010-08-25 10:55:47 -0700 | [diff] [blame] | 32 | * {@code <ImageButton>} XML element or by the |
Scott Main | 84971ae0 | 2009-05-18 16:05:52 -0700 | [diff] [blame] | 33 | * {@link #setImageResource(int)} method.</p> |
| 34 | * |
| 35 | * <p>To remove the standard button background image, define your own |
| 36 | * background image or set the background color to be transparent.</p> |
| 37 | * <p>To indicate the different button states (focused, selected, etc.), you can |
| 38 | * define a different image for each state. E.g., a blue image by default, an |
| 39 | * orange one for when focused, and a yellow one for when pressed. An easy way to |
| 40 | * do this is with an XML drawable "selector." For example:</p> |
| 41 | * <pre> |
| 42 | * <?xml version="1.0" encoding="utf-8"?> |
| 43 | * <selector xmlns:android="http://schemas.android.com/apk/res/android"> |
Scott Main | 84971ae0 | 2009-05-18 16:05:52 -0700 | [diff] [blame] | 44 | * <item android:state_pressed="true" |
| 45 | * android:drawable="@drawable/button_pressed" /> <!-- pressed --> |
| 46 | * <item android:state_focused="true" |
| 47 | * android:drawable="@drawable/button_focused" /> <!-- focused --> |
Scott Main | aa3b596 | 2010-01-08 12:21:00 -0800 | [diff] [blame] | 48 | * <item android:drawable="@drawable/button_normal" /> <!-- default --> |
Scott Main | 84971ae0 | 2009-05-18 16:05:52 -0700 | [diff] [blame] | 49 | * </selector></pre> |
| 50 | * |
| 51 | * <p>Save the XML file in your project {@code res/drawable/} folder and then |
| 52 | * reference it as a drawable for the source of your ImageButton (in the |
| 53 | * {@code android:src} attribute). Android will automatically change the image |
| 54 | * based on the state of the button and the corresponding images |
| 55 | * defined in the XML.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | * |
Ben Dodson | 4e8620f | 2010-08-25 10:55:47 -0700 | [diff] [blame] | 57 | * <p>The order of the {@code <item>} elements is important because they are |
Scott Main | aa3b596 | 2010-01-08 12:21:00 -0800 | [diff] [blame] | 58 | * evaluated in order. This is why the "normal" button image comes last, because |
| 59 | * it will only be applied after {@code android:state_pressed} and {@code |
| 60 | * android:state_focused} have both evaluated false.</p> |
| 61 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 62 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/button.html">Buttons</a> |
| 63 | * guide.</p> |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 64 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | * <p><strong>XML attributes</strong></p> |
| 66 | * <p> |
| 67 | * See {@link android.R.styleable#ImageView Button Attributes}, |
| 68 | * {@link android.R.styleable#View View Attributes} |
| 69 | * </p> |
| 70 | */ |
| 71 | @RemoteView |
| 72 | public class ImageButton extends ImageView { |
| 73 | public ImageButton(Context context) { |
| 74 | this(context, null); |
| 75 | } |
| 76 | |
| 77 | public ImageButton(Context context, AttributeSet attrs) { |
| 78 | this(context, attrs, com.android.internal.R.attr.imageButtonStyle); |
| 79 | } |
| 80 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 81 | public ImageButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 82 | this(context, attrs, defStyleAttr, 0); |
| 83 | } |
| 84 | |
| 85 | public ImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 86 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | setFocusable(true); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected boolean onSetAlpha(int alpha) { |
| 92 | return false; |
| 93 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 94 | |
| 95 | @Override |
| 96 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 97 | super.onInitializeAccessibilityEvent(event); |
| 98 | event.setClassName(ImageButton.class.getName()); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 103 | super.onInitializeAccessibilityNodeInfo(info); |
| 104 | info.setClassName(ImageButton.class.getName()); |
| 105 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | } |