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