앱 바에서 사용자 작업의 버튼을 추가할 수 있습니다. 이 기능을 사용하면 현재 컨텍스트에 가장 중요한 작업을 앱 상단에 배치할 수 있습니다. 예를 들어 사진 탐색 앱의 경우 사용자가 사진 롤을 볼 때 상단에 공유 및 앨범 만들기 버튼이 표시될 수 있습니다. 사용자가 개별 사진을 볼 때 앱에 자르기 및 필터 버튼이 표시될 수 있습니다.
앱 바의 공간은 한정되어 있습니다. 앱이 선언한 작업이 앱바에 들어갈 수 있는 것보다 더 많은 경우 앱바는 초과 작업을 더보기 메뉴에 보냅니다.
앱은 특정 작업이 앱바가 아니라 더보기 메뉴에 항상 표시되도록 지정할 수도 있습니다.
그림 1. 'Now in Android' 앱의 작업 아이콘입니다.
작업 버튼 추가
모든 작업 버튼과 작업 오버플로에서 사용할 수 있는 다른 항목은 XML 메뉴 리소스에서 정의됩니다. 작업 표시줄에 작업을 추가하려면 프로젝트의 res/menu/ 디렉터리에 새 XML 파일을 만듭니다.
작업 표시줄에 포함할 각 항목의 <item> 요소를 추가합니다. 다음 샘플 메뉴 XML 파일을 참고하세요.
app:showAsAction 속성은 작업을 앱 바에서 버튼으로 표시할지 여부를 지정합니다. app:showAsAction="ifRoom"를 설정하면(위 코드 예의 favorite 작업에서와 같이) 작업은 앱 바에 공간이 있는 경우 버튼으로 표시됩니다. 공간이 충분하지 않으면 초과 작업이 더보기 메뉴로 전송됩니다. app:showAsAction="never"를 설정하면(위 코드 예의 settings 작업에서와 같이) 작업은 앱 바에 표시되지 않고 항상 더보기 메뉴에 나열됩니다.
작업이 앱 바에 표시되면 시스템에서는 이 작업의 아이콘을 작업 버튼으로 사용합니다. Material Icons에서 다양한 유용한 아이콘을 찾아 볼 수 있습니다.
작업에 응답
사용자가 앱 바 항목 중 하나를 선택하면 시스템은 활동의 onOptionsItemSelected() 콜백 메서드를 호출하고 탭된 항목을 나타내는 MenuItem 객체를 전달합니다. onOptionsItemSelected() 구현에서 MenuItem.getItemId() 메서드를 호출하여 어느 항목이 탭되었는지 확인합니다. 반환된 ID는 상응하는 <item> 요소의 android:id 속성에서 선언한 값과 일치합니다.
예를 들어 다음 코드 스니펫은 사용자가 선택한 작업을 확인합니다.
메서드가 사용자의 작업을 인식하지 못하면 슈퍼클래스 메서드를 호출합니다.
Kotlin
overridefunonOptionsItemSelected(item:MenuItem)=when(item.itemId){R.id.action_settings->{// User chooses the "Settings" item. Show the app settings UI.true}R.id.action_favorite->{// User chooses the "Favorite" action. Mark the current item as a// favorite.true}else->{// The user's action isn't recognized.// Invoke the superclass to handle it.super.onOptionsItemSelected(item)}}
Java
@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){switch(item.getItemId()){caseR.id.action_settings:// User chooses the "Settings" item. Show the app settings UI.returntrue;caseR.id.action_favorite:// User chooses the "Favorite" action. Mark the current item as a// favorite.returntrue;default:// The user's action isn't recognized.// Invoke the superclass to handle it.returnsuper.onOptionsItemSelected(item);}}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[null,null,["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Add and handle actions\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Dynamic top app bar →](/develop/ui/compose/components/app-bars-dynamic) \n\nThe app bar lets you add buttons for user actions. This feature lets you put\nthe most important *actions* for the current context at the top of the app.\nFor example, a photo browsing app might show *share* and *create\nalbum* buttons at the top when the user is looking at their photo roll. When\nthe user looks at an individual photo, the app might show *crop* and\n*filter* buttons.\n\nSpace in the app bar is limited. If an app declares more actions than can fit\nin the app bar, the app bar sends the excess actions to an *overflow* menu.\nThe app can also specify that an action always shows in the overflow menu,\ninstead of displaying on the app bar.\n**Figure 1.** An action icon in the \"Now in Android\" app.\n\nAdd action buttons\n------------------\n\nAll action buttons and other items available in the action overflow are\ndefined in an XML\n[menu resource](/guide/topics/resources/menu-resource). To add\nactions to the action bar, create a new XML file in your project's\n`res/menu/` directory.\n\nAdd an\n[`\u003citem\u003e`](/guide/topics/resources/menu-resource#item-element)\nelement for each item you want to include in the action bar, as shown in the\nfollowing sample menu XML file: \n\n```xml\n\u003cmenu xmlns:android=\"http://schemas.android.com/apk/res/android\" \nxmlns:app=\"http://schemas.android.com/apk/res-auto\"\u003e\n\n \u003c!-- \"Mark Favorite\", must appear as action button if possible. --\u003e\n \u003citem\n android:id=\"@+id/action_favorite\"\n android:icon=\"@drawable/ic_favorite_black_48dp\"\n android:title=\"@string/action_favorite\"\n app:showAsAction=\"ifRoom\"/\u003e\n\n \u003c!-- Settings, must always be in the overflow. --\u003e\n \u003citem android:id=\"@+id/action_settings\"\n android:title=\"@string/action_settings\"\n app:showAsAction=\"never\"/\u003e\n\n\u003c/menu\u003e\n```\n\nThe `app:showAsAction` attribute specifies whether the action is\nshown as a button on the app bar. If you set\n`app:showAsAction=\"ifRoom\"`---as in the example code's\n*favorite* action---the action displays as a button if there is room in\nthe app bar for it. If there isn't enough room, excess actions are sent to the\noverflow menu. If you set `app:showAsAction=\"never\"`---as in the\nexample code's *settings* action---the action is always listed in the\noverflow menu and not displayed in the app bar.\n\nThe system uses the action's icon as the action button if the action displays\nin the app bar. You can find many useful icons in\n[Material Icons](https://www.google.com/design/icons/).\n\nRespond to actions\n------------------\n\nWhen the user selects one of the app bar items, the system calls your\nactivity's\n[onOptionsItemSelected()](/reference/android/app/Activity#onOptionsItemSelected(android.view.MenuItem))\ncallback method and passes a\n[MenuItem](/reference/android/view/MenuItem) object\nto indicate which item was tapped. In your implementation of\n`onOptionsItemSelected()`, call the\n[MenuItem.getItemId()](/reference/android/view/MenuItem#getItemId())\nmethod to determine which item was tapped. The ID returned matches the value you\ndeclare in the corresponding `\u003citem\u003e` element's\n`android:id` attribute.\n\nFor example, the following code snippet checks which action the user selects.\nIf the method doesn't recognize the user's action, it invokes the superclass\nmethod: \n\n### Kotlin\n\n```kotlin\noverride fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {\n R.id.action_settings -\u003e {\n // User chooses the \"Settings\" item. Show the app settings UI.\n true\n }\n\n R.id.action_favorite -\u003e {\n // User chooses the \"Favorite\" action. Mark the current item as a\n // favorite.\n true\n }\n\n else -\u003e {\n // The user's action isn't recognized.\n // Invoke the superclass to handle it.\n super.onOptionsItemSelected(item)\n }\n}\n```\n\n### Java\n\n```java\n@Override\npublic boolean onOptionsItemSelected(MenuItem item) {\n switch (item.getItemId()) {\n case R.id.action_settings:\n // User chooses the \"Settings\" item. Show the app settings UI.\n return true;\n\n case R.id.action_favorite:\n // User chooses the \"Favorite\" action. Mark the current item as a\n // favorite.\n return true;\n\n default:\n // The user's action isn't recognized.\n // Invoke the superclass to handle it.\n return super.onOptionsItemSelected(item);\n\n }\n}\n```"]]