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.view.InputDevice; 22 import android.view.MotionEvent; 23 import android.view.PointerIcon; 24 import android.widget.RemoteViews.RemoteView; 25 26 /** 27 * <p> 28 * Displays a button with an image (instead of text) that can be pressed 29 * or clicked by the user. By default, an ImageButton looks like a regular 30 * {@link android.widget.Button}, with the standard button background 31 * that changes color during different button states. The image on the surface 32 * of the button is defined either by the {@code android:src} attribute in the 33 * {@code <ImageButton>} XML element or by the 34 * {@link #setImageResource(int)} method.</p> 35 * 36 * <p>To remove the standard button background image, define your own 37 * background image or set the background color to be transparent.</p> 38 * <p>To indicate the different button states (focused, selected, etc.), you can 39 * define a different image for each state. E.g., a blue image by default, an 40 * orange one for when focused, and a yellow one for when pressed. An easy way to 41 * do this is with an XML drawable "selector." For example:</p> 42 * <pre> 43 * <?xml version="1.0" encoding="utf-8"?> 44 * <selector xmlns:android="http://schemas.android.com/apk/res/android"> 45 * <item android:state_pressed="true" 46 * android:drawable="@drawable/button_pressed" /> <!-- pressed --> 47 * <item android:state_focused="true" 48 * android:drawable="@drawable/button_focused" /> <!-- focused --> 49 * <item android:drawable="@drawable/button_normal" /> <!-- default --> 50 * </selector></pre> 51 * 52 * <p>Save the XML file in your project {@code res/drawable/} folder and then 53 * reference it as a drawable for the source of your ImageButton (in the 54 * {@code android:src} attribute). Android will automatically change the image 55 * based on the state of the button and the corresponding images 56 * defined in the XML.</p> 57 * 58 * <p>The order of the {@code <item>} elements is important because they are 59 * evaluated in order. This is why the "normal" button image comes last, because 60 * it will only be applied after {@code android:state_pressed} and {@code 61 * android:state_focused} have both evaluated false.</p> 62 * 63 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/button.html">Buttons</a> 64 * guide.</p> 65 * 66 * <p><strong>XML attributes</strong></p> 67 * <p> 68 * See {@link android.R.styleable#ImageView Button Attributes}, 69 * {@link android.R.styleable#View View Attributes} 70 * </p> 71 */ 72 @RemoteView 73 public class ImageButton extends ImageView { ImageButton(Context context)74 public ImageButton(Context context) { 75 this(context, null); 76 } 77 ImageButton(Context context, AttributeSet attrs)78 public ImageButton(Context context, AttributeSet attrs) { 79 this(context, attrs, com.android.internal.R.attr.imageButtonStyle); 80 } 81 ImageButton(Context context, AttributeSet attrs, int defStyleAttr)82 public ImageButton(Context context, AttributeSet attrs, int defStyleAttr) { 83 this(context, attrs, defStyleAttr, 0); 84 } 85 ImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)86 public ImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 87 super(context, attrs, defStyleAttr, defStyleRes); 88 setFocusable(true); 89 } 90 91 @Override onSetAlpha(int alpha)92 protected boolean onSetAlpha(int alpha) { 93 return false; 94 } 95 96 @Override getAccessibilityClassName()97 public CharSequence getAccessibilityClassName() { 98 return ImageButton.class.getName(); 99 } 100 101 @Override onResolvePointerIcon(MotionEvent event, int pointerIndex)102 public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) { 103 if (getPointerIcon() == null && isClickable() && isEnabled() 104 && event.isFromSource(InputDevice.SOURCE_MOUSE)) { 105 return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND); 106 } 107 return super.onResolvePointerIcon(event, pointerIndex); 108 } 109 } 110