blob: 634cbe323d862c974d81f007cabb5e880ab64fb0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.widget;
18
19import android.content.Context;
20import android.util.AttributeSet;
Jeff Sharkey67f9d502017-08-05 13:49:13 -060021import android.view.LayoutInflater;
Vladislav Kaznacheev2a848ff2016-09-23 10:16:16 -070022import android.view.MotionEvent;
23import android.view.PointerIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.widget.RemoteViews.RemoteView;
25
26
27/**
Joe Fernandez8e342c72017-04-24 11:59:54 -070028 * 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>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 *
Scott Main6ec3d4f2010-09-22 18:55:31 -070042 * <pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 * public class MyActivity extends Activity {
Joe Fernandez8e342c72017-04-24 11:59:54 -070044 * protected void onCreate(Bundle savedInstanceState) {
45 * super.onCreate(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 *
47 * setContentView(R.layout.content_layout_id);
48 *
Alan Viverette51efddb2017-04-05 10:00:01 -040049 * final Button button = findViewById(R.id.button_id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 * button.setOnClickListener(new View.OnClickListener() {
51 * public void onClick(View v) {
Joe Fernandez8e342c72017-04-24 11:59:54 -070052 * // Code here executes on main thread after user presses button
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 * }
54 * });
55 * }
Scott Main6ec3d4f2010-09-22 18:55:31 -070056 * }</pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 *
Jeff Sharkey67f9d502017-08-05 13:49:13 -060058 * <p>The above snippet creates an instance of {@link android.view.View.OnClickListener} and wires
Joe Fernandez8e342c72017-04-24 11:59:54 -070059 * 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>
Scott Main6ec3d4f2010-09-22 18:55:31 -070063 *
Joe Fernandez8e342c72017-04-24 11:59:54 -070064 * <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>
Scott Main6ec3d4f2010-09-22 18:55:31 -070070 *
Joe Fernandez8e342c72017-04-24 11:59:54 -070071 * <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>
Scott Main4c359b72012-07-24 15:51:27 -070075 * guide.</p>
Scott Main41ec6532010-08-19 16:57:07 -070076 *
Joe Fernandez8e342c72017-04-24 11:59:54 -070077 * <p>For all XML style attributes available on Button see
78 * {@link android.R.styleable#Button Button Attributes},
Scott Main6ec3d4f2010-09-22 18:55:31 -070079 * {@link android.R.styleable#TextView TextView Attributes},
Joe Fernandez8e342c72017-04-24 11:59:54 -070080 * {@link android.R.styleable#View View Attributes}. See the
Jeff Sharkey67f9d502017-08-05 13:49:13 -060081 * <a href="{@docRoot}guide/topics/ui/themes.html#ApplyingStyles">Styles and Themes</a>
Joe Fernandez8e342c72017-04-24 11:59:54 -070082 * guide to learn how to implement and organize overrides to style-related attributes.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 */
84@RemoteView
85public class Button extends TextView {
Joe Fernandez8e342c72017-04-24 11:59:54 -070086
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 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 public Button(Context context) {
96 this(context, null);
97 }
98
Joe Fernandez8e342c72017-04-24 11:59:54 -070099 /**
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 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 public Button(Context context, AttributeSet attrs) {
117 this(context, attrs, com.android.internal.R.attr.buttonStyle);
118 }
119
Joe Fernandez8e342c72017-04-24 11:59:54 -0700120 /**
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 */
Alan Viverette617feb92013-09-09 18:09:13 -0700140 public Button(Context context, AttributeSet attrs, int defStyleAttr) {
141 this(context, attrs, defStyleAttr, 0);
142 }
143
Joe Fernandez8e342c72017-04-24 11:59:54 -0700144 /**
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 */
Alan Viverette617feb92013-09-09 18:09:13 -0700165 public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
166 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800168
169 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800170 public CharSequence getAccessibilityClassName() {
171 return Button.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800172 }
Vladislav Kaznacheev2a848ff2016-09-23 10:16:16 -0700173
174 @Override
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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181}