1 /*
2  * Copyright (C) 2006 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.LayoutInflater;
23 import android.view.MotionEvent;
24 import android.view.PointerIcon;
25 import android.widget.RemoteViews.RemoteView;
26 
27 
28 /**
29  * A user interface element the user can tap or click to perform an action.
30  *
31  * <p>To display a button in an activity, add a button to the activity's layout XML file:</p>
32  *
33  * <pre>
34  * &lt;Button
35  *     android:id="@+id/button_id"
36  *     android:layout_height="wrap_content"
37  *     android:layout_width="wrap_content"
38  *     android:text="@string/self_destruct" /&gt;</pre>
39  *
40  * <p>To specify an action when the button is pressed, set a click
41  * listener on the button object in the corresponding activity code:</p>
42  *
43  * <pre>
44  * public class MyActivity extends Activity {
45  *     protected void onCreate(Bundle savedInstanceState) {
46  *         super.onCreate(savedInstanceState);
47  *
48  *         setContentView(R.layout.content_layout_id);
49  *
50  *         final Button button = findViewById(R.id.button_id);
51  *         button.setOnClickListener(new View.OnClickListener() {
52  *             public void onClick(View v) {
53  *                 // Code here executes on main thread after user presses button
54  *             }
55  *         });
56  *     }
57  * }</pre>
58  *
59  * <p>The above snippet creates an instance of {@link android.view.View.OnClickListener} and wires
60  * the listener to the button using
61  * {@link #setOnClickListener setOnClickListener(View.OnClickListener)}.
62  * As a result, the system executes the code you write in {@code onClick(View)} after the
63  * user presses the button.</p>
64  *
65  * <p class="note">The system executes the code in {@code onClick} on the
66  * <a href="{@docRoot}guide/components/processes-and-threads.html#Threads">main thread</a>.
67  * This means your onClick code must execute quickly to avoid delaying your app's response
68  * to further user actions.  See
69  * <a href="{@docRoot}training/articles/perf-anr.html">Keeping Your App Responsive</a>
70  * for more details.</p>
71  *
72  * <p>Every button is styled using the system's default button background, which is often
73  * different from one version of the platform to another. If you are not satisfied with the
74  * default button style, you can customize it. For more details and code samples, see the
75  * <a href="{@docRoot}guide/topics/ui/controls/button.html#Style">Styling Your Button</a>
76  * guide.</p>
77  *
78  * <p>For all XML style attributes available on Button see
79  * {@link android.R.styleable#Button Button Attributes},
80  * {@link android.R.styleable#TextView TextView Attributes},
81  * {@link android.R.styleable#View View Attributes}.  See the
82  * <a href="{@docRoot}guide/topics/ui/themes.html#ApplyingStyles">Styles and Themes</a>
83  * guide to learn how to implement and organize overrides to style-related attributes.</p>
84  */
85 @RemoteView
86 public class Button extends TextView {
87 
88     /**
89      * Simple constructor to use when creating a button from code.
90      *
91      * @param context The Context the Button is running in, through which it can
92      *        access the current theme, resources, etc.
93      *
94      * @see #Button(Context, AttributeSet)
95      */
Button(Context context)96     public Button(Context context) {
97         this(context, null);
98     }
99 
100     /**
101      * {@link LayoutInflater} calls this constructor when inflating a Button from XML.
102      * The attributes defined by the current theme's
103      * {@link android.R.attr#buttonStyle android:buttonStyle}
104      * override base view attributes.
105      *
106      * You typically do not call this constructor to create your own button instance in code.
107      * However, you must override this constructor when
108      * <a href="{@docRoot}training/custom-views/index.html">creating custom views</a>.
109      *
110      * @param context The Context the view is running in, through which it can
111      *        access the current theme, resources, etc.
112      * @param attrs The attributes of the XML Button tag being used to inflate the view.
113      *
114      * @see #Button(Context, AttributeSet, int)
115      * @see android.view.View#View(Context, AttributeSet)
116      */
Button(Context context, AttributeSet attrs)117     public Button(Context context, AttributeSet attrs) {
118         this(context, attrs, com.android.internal.R.attr.buttonStyle);
119     }
120 
121     /**
122      * This constructor allows a Button subclass to use its own class-specific base style from a
123      * theme attribute when inflating. The attributes defined by the current theme's
124      * {@code defStyleAttr} override base view attributes.
125      *
126      * <p>For Button's base view attributes see
127      * {@link android.R.styleable#Button Button Attributes},
128      * {@link android.R.styleable#TextView TextView Attributes},
129      * {@link android.R.styleable#View View Attributes}.
130      *
131      * @param context The Context the Button is running in, through which it can
132      *        access the current theme, resources, etc.
133      * @param attrs The attributes of the XML Button tag that is inflating the view.
134      * @param defStyleAttr The resource identifier of an attribute in the current theme
135      *        whose value is the the resource id of a style. The specified style’s
136      *        attribute values serve as default values for the button. Set this parameter
137      *        to 0 to avoid use of default values.
138      * @see #Button(Context, AttributeSet, int, int)
139      * @see android.view.View#View(Context, AttributeSet, int)
140      */
Button(Context context, AttributeSet attrs, int defStyleAttr)141     public Button(Context context, AttributeSet attrs, int defStyleAttr) {
142         this(context, attrs, defStyleAttr, 0);
143     }
144 
145     /**
146      * This constructor allows a Button subclass to use its own class-specific base style from
147      * either a theme attribute or style resource when inflating. To see how the final value of a
148      * particular attribute is resolved based on your inputs to this constructor, see
149      * {@link android.view.View#View(Context, AttributeSet, int, int)}.
150      *
151      * @param context The Context the Button is running in, through which it can
152      *        access the current theme, resources, etc.
153      * @param attrs The attributes of the XML Button tag that is inflating the view.
154      * @param defStyleAttr The resource identifier of an attribute in the current theme
155      *        whose value is the the resource id of a style. The specified style’s
156      *        attribute values serve as default values for the button. Set this parameter
157      *        to 0 to avoid use of default values.
158      * @param defStyleRes The identifier of a style resource that
159      *        supplies default values for the button, used only if
160      *        defStyleAttr is 0 or cannot be found in the theme.
161      *        Set this parameter to 0 to avoid use of default values.
162      *
163      * @see #Button(Context, AttributeSet, int)
164      * @see android.view.View#View(Context, AttributeSet, int, int)
165      */
Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)166     public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
167         super(context, attrs, defStyleAttr, defStyleRes);
168     }
169 
170     @Override
getAccessibilityClassName()171     public CharSequence getAccessibilityClassName() {
172         return Button.class.getName();
173     }
174 
175     @Override
onResolvePointerIcon(MotionEvent event, int pointerIndex)176     public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
177         if (getPointerIcon() == null && isClickable() && isEnabled()
178                 && event.isFromSource(InputDevice.SOURCE_MOUSE)) {
179             return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
180         }
181         return super.onResolvePointerIcon(event, pointerIndex);
182     }
183 }
184