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