findViewById(R.id.supabutton).setOnClickListener{Log.d("BUTTONS","User tapped the Supabutton")}
Java
Buttonbutton=(Button)findViewById(R.id.supabutton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){Log.d("BUTTONS","User tapped the Supabutton");}});
Style your button
The appearance of your button—the background image and font—varies between devices,
because devices by different manufacturers often have different default styles for input
controls.
To customize individual buttons with a different background, specify the
android:background attribute
with a drawable or color resource. Alternatively, you can apply a style for the button,
which works in similarly to HTML styles to define multiple style properties such as the background,
font, and size. For more information about applying styles, see
Styles and themes.
Borderless button
One design that can be useful is a "borderless" button. Borderless buttons resemble basic buttons
except that they have no borders or background but still change appearance during different states,
such as when tapped.
To create a borderless button, apply the
borderlessButtonStyle
style to the button, as in the following example:
If you want to truly redefine the appearance of your button, you can specify a custom background.
Instead of supplying a simple bitmap or color, however, your background must be a state list
resource that changes appearance depending on the button's current state.
You can define the state list in an XML file that defines three images or colors to use for the
different button states.
To create a state list drawable for your button background, do the following:
Create three bitmaps for the button background that represent the default, tapped, and focused
button states. To ensure that your images fit buttons of various sizes, create the bitmaps as
nine-patch bitmaps.
Place the bitmaps into your project's res/drawable/ directory. Name each bitmap
to reflect the button state it represents, such as button_default.9.png,
button_pressed.9.png, and button_focused.9.png.
Create a new XML file in the res/drawable/ directory. Name it something like
button_custom.xml. Insert XML like the following:
This defines a single drawable resource that changes its image based on the current state of
the button.
The first defines the bitmap to use when the button is tapped
(activated).
The second defines the bitmap to use when the button is focused, such
as when the button is highlighted using the trackball or directional pad.
The third defines the bitmap to use when the button is in the default
state, neither tapped nor focused.
This XML file represents a single drawable resource. When referenced by a Button for
its background, the image displayed changes based on the button's state.
Apply the drawable XML file as the button background:
For more information about this XML syntax, including how to define a button that is disabled,
hovered, or in another state, read about
StateListDrawable.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[],[]]