The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Jeff Sharkey | 67f9d50 | 2017-08-05 13:49:13 -0600 | [diff] [blame] | 21 | import android.view.LayoutInflater; |
Vladislav Kaznacheev | 2a848ff | 2016-09-23 10:16:16 -0700 | [diff] [blame] | 22 | import android.view.MotionEvent; |
| 23 | import android.view.PointerIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.widget.RemoteViews.RemoteView; |
| 25 | |
| 26 | |
| 27 | /** |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 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 | * <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" /></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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | * |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 42 | * <pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | * public class MyActivity extends Activity { |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 44 | * protected void onCreate(Bundle savedInstanceState) { |
| 45 | * super.onCreate(savedInstanceState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | * |
| 47 | * setContentView(R.layout.content_layout_id); |
| 48 | * |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 49 | * final Button button = findViewById(R.id.button_id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * button.setOnClickListener(new View.OnClickListener() { |
| 51 | * public void onClick(View v) { |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 52 | * // Code here executes on main thread after user presses button |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | * } |
| 54 | * }); |
| 55 | * } |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 56 | * }</pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | * |
Jeff Sharkey | 67f9d50 | 2017-08-05 13:49:13 -0600 | [diff] [blame] | 58 | * <p>The above snippet creates an instance of {@link android.view.View.OnClickListener} and wires |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 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> |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 63 | * |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 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> |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 70 | * |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 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> |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 75 | * guide.</p> |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 76 | * |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 77 | * <p>For all XML style attributes available on Button see |
| 78 | * {@link android.R.styleable#Button Button Attributes}, |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 79 | * {@link android.R.styleable#TextView TextView Attributes}, |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 80 | * {@link android.R.styleable#View View Attributes}. See the |
Jeff Sharkey | 67f9d50 | 2017-08-05 13:49:13 -0600 | [diff] [blame] | 81 | * <a href="{@docRoot}guide/topics/ui/themes.html#ApplyingStyles">Styles and Themes</a> |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 82 | * guide to learn how to implement and organize overrides to style-related attributes.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | */ |
| 84 | @RemoteView |
| 85 | public class Button extends TextView { |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 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 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | public Button(Context context) { |
| 96 | this(context, null); |
| 97 | } |
| 98 | |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 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 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | public Button(Context context, AttributeSet attrs) { |
| 117 | this(context, attrs, com.android.internal.R.attr.buttonStyle); |
| 118 | } |
| 119 | |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 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 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 140 | public Button(Context context, AttributeSet attrs, int defStyleAttr) { |
| 141 | this(context, attrs, defStyleAttr, 0); |
| 142 | } |
| 143 | |
Joe Fernandez | 8e342c7 | 2017-04-24 11:59:54 -0700 | [diff] [blame] | 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 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 165 | public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 166 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 168 | |
| 169 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 170 | public CharSequence getAccessibilityClassName() { |
| 171 | return Button.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 172 | } |
Vladislav Kaznacheev | 2a848ff | 2016-09-23 10:16:16 -0700 | [diff] [blame] | 173 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |