findViewById<Button>(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");}});
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],null,["# Add buttons to your app\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Button →](/develop/ui/compose/components/button) \n\nA button consists of text or an icon, or both, that communicates what action occurs when the user\ntaps it.\n| **Note:** For a better UI and user experience, see the [Material Design button](https://m3.material.io/components/all-buttons) documentation.\n\nYou can create a button in your layout in one of three ways, depending on\nwhether you want a button with text, an icon, or both: \n\n```xml\n \n \u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:paddingLeft=\"16dp\"\n android:paddingRight=\"16dp\"\n android:orientation=\"vertical\" \u003e\n \n \u003cButton\n android:id=\"@+id/supabutton\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"I'm a button\" /\u003e\n \n \u003cImageButton\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:contentDescription=\"A tiny Android icon\"\n android:src=\"@drawable/baseline_android_24\"\n app:tint=\"#ff0000\" /\u003e\n \n \u003cButton\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:drawableStart=\"@drawable/baseline_android_24\"\n android:drawablePadding=\"4dp\"\n android:drawableTint=\"#ff0000\"\n android:text=\"I'm a button with an icon\" /\u003e\n \u003c/LinearLayout\u003e\n```\n\nThe previous code generates something like this:\n**Figure 1.** Three styles of buttons.\n\nRespond to click events\n-----------------------\n\nWhen the user taps a button, the\n[Button](/reference/android/widget/Button) object receives an\non-click event.\n\nTo declare the event handler programmatically, create an\n[View.OnClickListener](/reference/android/view/View.OnClickListener)\nobject and assign it to the button by calling\n[setOnClickListener(View.OnClickListener)](/reference/android/view/View#setOnClickListener(android.view.View.OnClickListener)),\nas in the following example: \n\n### Kotlin\n\n```kotlin\nfindViewById\u003cButton\u003e(R.id.supabutton)\n .setOnClickListener {\n Log.d(\"BUTTONS\", \"User tapped the Supabutton\")\n }\n```\n\n### Java\n\n```java\nButton button = (Button) findViewById(R.id.supabutton);\nbutton.setOnClickListener(new View.OnClickListener() {\n public void onClick(View v) {\n Log.d(\"BUTTONS\", \"User tapped the Supabutton\");\n }\n});\n```\n\nStyle your button\n-----------------\n\nThe appearance of your button---the background image and font---varies between devices,\nbecause devices by different manufacturers often have different default styles for input\ncontrols.\n\nTo customize individual buttons with a different background, specify the\n[android:background](/reference/android/R.attr#background) attribute\nwith a drawable or color resource. Alternatively, you can apply a *style* for the button,\nwhich works in similarly to HTML styles to define multiple style properties such as the background,\nfont, and size. For more information about applying styles, see\n[Styles and themes](/guide/topics/ui/themes).\n\n### Borderless button\n\nOne design that can be useful is a \"borderless\" button. Borderless buttons resemble basic buttons\nexcept that they have no borders or background but still change appearance during different states,\nsuch as when tapped.\n\nTo create a borderless button, apply the\n[borderlessButtonStyle](/reference/android/R.attr#borderlessButtonStyle)\nstyle to the button, as in the following example: \n\n```xml\n\u003cButton\n android:id=\"@+id/supabutton\"\n style=\"?android:attr/borderlessButtonStyle\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"I'm a button\" /\u003e\n```\n\n### Custom background\n\nIf you want to truly redefine the appearance of your button, you can specify a custom background.\nInstead of supplying a simple bitmap or color, however, your background must be a state list\nresource that changes appearance depending on the button's current state.\n\nYou can define the state list in an XML file that defines three images or colors to use for the\ndifferent button states.\n\nTo create a state list drawable for your button background, do the following:\n\n1. 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](/guide/topics/graphics/2d-graphics#nine-patch) bitmaps.\n2. 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`.\n3. Create a new XML file in the `res/drawable/` directory. Name it something like `button_custom.xml`. Insert XML like the following: \n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cselector xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003citem android:drawable=\"@drawable/button_pressed\"\n android:state_pressed=\"true\" /\u003e\n \u003citem android:drawable=\"@drawable/button_focused\"\n android:state_focused=\"true\" /\u003e\n \u003citem android:drawable=\"@drawable/button_default\" /\u003e\n \u003c/selector\u003e\n ```\n\n This defines a single drawable resource that changes its image based on the current state of\n the button.\n - The first `\u003citem\u003e` defines the bitmap to use when the button is tapped (activated).\n - The second `\u003citem\u003e` defines the bitmap to use when the button is focused, such as when the button is highlighted using the trackball or directional pad.\n - The third `\u003citem\u003e` defines the bitmap to use when the button is in the default state, neither tapped nor focused.\n\n | **Note:** The order of the `\u003citem\u003e` elements is important. When this drawable is referenced, the `\u003citem\u003e` elements are traversed in order to determine which one is appropriate for the current button state. Because the default bitmap is last, it is only applied when the conditions `android:state_pressed` and `android:state_focused` are evaluated as false.\n\n This XML file represents a single drawable resource. When referenced by a `Button` for\n its background, the image displayed changes based on the button's state.\n4. Apply the drawable XML file as the button background: \n\n ```xml\n \u003cButton\n android:id=\"@+id/button_send\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"@string/button_send\"\n android:onClick=\"sendMessage\"\n android:background=\"@drawable/button_custom\" /\u003e\n ```\n\nFor more information about this XML syntax, including how to define a button that is disabled,\nhovered, or in another state, read about\n[StateListDrawable](/guide/topics/resources/drawable-resource#StateList)."]]