19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.widget;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.AttributeSet;
212a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheevimport android.view.MotionEvent;
222a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheevimport android.view.PointerIcon;
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.widget.RemoteViews.RemoteView;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>
2784971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * Displays a button with an image (instead of text) that can be pressed
2884971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * or clicked by the user. By default, an ImageButton looks like a regular
2984971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * {@link android.widget.Button}, with the standard button background
3084971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * that changes color during different button states. The image on the surface
3184971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * of the button is defined either by the {@code android:src} attribute in the
324e8620f868e2490782ebb960404140ea9482c91dBen Dodson * {@code <ImageButton>} XML element or by the
3384971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * {@link #setImageResource(int)} method.</p>
3484971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main *
3584971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * <p>To remove the standard button background image, define your own
3684971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * background image or set the background color to be transparent.</p>
3784971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * <p>To indicate the different button states (focused, selected, etc.), you can
3884971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * define a different image for each state. E.g., a blue image by default, an
3984971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * orange one for when focused, and a yellow one for when pressed. An easy way to
4084971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * do this is with an XML drawable "selector." For example:</p>
4184971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * <pre>
4284971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * &lt;?xml version="1.0" encoding="utf-8"?&gt;
4384971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
4484971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main *     &lt;item android:state_pressed="true"
4584971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main *           android:drawable="@drawable/button_pressed" /&gt; &lt;!-- pressed --&gt;
4684971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main *     &lt;item android:state_focused="true"
4784971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main *           android:drawable="@drawable/button_focused" /&gt; &lt;!-- focused --&gt;
48aa3b5961c0142fe2fe87a5e4f67a12b3728f1beaScott Main *     &lt;item android:drawable="@drawable/button_normal" /&gt; &lt;!-- default --&gt;
4984971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * &lt;/selector&gt;</pre>
5084971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main *
5184971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * <p>Save the XML file in your project {@code res/drawable/} folder and then
5284971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * reference it as a drawable for the source of your ImageButton (in the
5384971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * {@code android:src} attribute). Android will automatically change the image
5484971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * based on the state of the button and the corresponding images
5584971ae0109bdfae79f3c9d15edf400fa5129f3dScott Main * defined in the XML.</p>
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
574e8620f868e2490782ebb960404140ea9482c91dBen Dodson * <p>The order of the {@code <item>} elements is important because they are
58aa3b5961c0142fe2fe87a5e4f67a12b3728f1beaScott Main * evaluated in order. This is why the "normal" button image comes last, because
59aa3b5961c0142fe2fe87a5e4f67a12b3728f1beaScott Main * it will only be applied after {@code android:state_pressed} and {@code
60aa3b5961c0142fe2fe87a5e4f67a12b3728f1beaScott Main * android:state_focused} have both evaluated false.</p>
61aa3b5961c0142fe2fe87a5e4f67a12b3728f1beaScott Main *
624c359b76f9a030f92a302ba74a528faa170bad4eScott Main * <p>See the <a href="{@docRoot}guide/topics/ui/controls/button.html">Buttons</a>
634c359b76f9a030f92a302ba74a528faa170bad4eScott Main * guide.</p>
6441ec65355bd6ded652769725b276d47c54a0d913Scott Main *
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p><strong>XML attributes</strong></p>
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See {@link android.R.styleable#ImageView Button Attributes},
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link android.R.styleable#View View Attributes}
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * </p>
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project@RemoteView
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class ImageButton extends ImageView {
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public ImageButton(Context context) {
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this(context, null);
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public ImageButton(Context context, AttributeSet attrs) {
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this(context, attrs, com.android.internal.R.attr.imageButtonStyle);
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
81617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    public ImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
82617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette        this(context, attrs, defStyleAttr, 0);
83617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    }
84617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette
85617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    public ImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
86617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette        super(context, attrs, defStyleAttr, defStyleRes);
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        setFocusable(true);
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected boolean onSetAlpha(int alpha) {
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
948a78fd4d9572dff95432fcc4ba0e87563415b728Svetoslav Ganov
958a78fd4d9572dff95432fcc4ba0e87563415b728Svetoslav Ganov    @Override
96a7bb6fbeab933326d58aa806d8194b7b13239d34Dianne Hackborn    public CharSequence getAccessibilityClassName() {
97a7bb6fbeab933326d58aa806d8194b7b13239d34Dianne Hackborn        return ImageButton.class.getName();
988a78fd4d9572dff95432fcc4ba0e87563415b728Svetoslav Ganov    }
992a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheev
1002a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheev    @Override
1012a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheev    public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
1022a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheev        if (getPointerIcon() == null && isClickable() && isEnabled()) {
1032a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheev            return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
1042a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheev        }
1052a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheev        return super.onResolvePointerIcon(event, pointerIndex);
1062a848ff65548f1a7eb00f909ee07c2ef8ac39caaVladislav Kaznacheev    }
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
108