findViewById<Button>(R.id.supabutton).setOnClickListener{Log.d("BUTTONS","User tapped the Supabutton")}
자바
Buttonbutton=(Button)findViewById(R.id.supabutton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){Log.d("BUTTONS","User tapped the Supabutton");}});
버튼 스타일 지정
버튼 모양(배경 이미지 및 글꼴)은 기기마다 다를 수 있습니다. 여러 제조업체의 기기에서 입력 컨트롤의 기본 스타일이 다른 경우가 많기 때문입니다.
다양한 배경의 개별 버튼을 맞춤설정하려면 드로어블 또는 색상 리소스와 함께 android:background 속성을 지정합니다. 또는 버튼에 스타일을 적용할 수 있습니다. 이 방법은 HTML 스타일과 유사한 방식으로 작동하여 배경, 글꼴, 크기 등 여러 스타일 속성을 정의합니다. 스타일 적용에 관한 자세한 내용은 스타일 및 테마를 참고하세요.
테두리 없는 버튼
유용한 디자인 중 하나는 '테두리 없는' 버튼입니다. 테두리 없는 버튼은 테두리나 배경이 없고 다양한 상태(예: 탭한 경우)에서 여전히 모양이 변경된다는 점을 제외하면 기본 버튼과 비슷합니다.
사용 중지, 마우스 오버 또는 다른 상태의 버튼을 정의하는 방법을 비롯하여 이 XML 구문에 관한 자세한 내용은 StateListDrawable를 참고하세요.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[null,null,["최종 업데이트: 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)."]]