본문 바로가기

Android

Toolbar와 ActionBar의 차이점

안드로이드에서는 Toolbar에 대한 동작을 검색하다보면, ActionBar와 Toolbar를 섞어서 사용하는 답변을 많이 볼 수 있다.

이번에 두 가지 위젯의 차이점을 살펴보자.

 

ActionBar와 Toolbar의 차이점

패키지의 차이점

패키지명을 확인해보면 ActionBar는 더 상위 패키지인 android.app에 속한 것을 확인할 수 있다.

실제로 ActionBar는 Activity에 포함되어 있는 기능이다.

 

ActionBar는 Android API level 11부터 지원된 아주 오래된 API이다. 공식문서를 살펴보면,

"A primary toolbar within the activity that may display the activity title, application-level navigation affordances, and other interactive items."

"From your activity, you can retrieve an instance of ActionBar by calling getActionBar()."

와 같은 설명을 찾을 수 있으며,

 

"Beginning with Android L (API level 21), the action bar may be represented by any Toolbar widget within the application layout. The application may signal to the Activity which Toolbar should be treated as the Activity's action bar. Activities that use this feature should use one of the supplied .NoActionBar themes, set the windowActionBar attribute to false or otherwise not request the window feature."

API level 21부터 Toolbar를 이용해 ActionBar를 표현하는 것이 가능하다고 설명되어있다.

 

AppCompatActivity의 공식문서를 확인해보면,

setSupportActionBar(Toolbar) 메소드로 Toolbar 인스턴스를 ActionBar로 사용할 수 있으며, getSupportActionBar() 를 통해서 액션바의 인스턴스를 획득할 수 있다.

 

즉, Toolbar가 더 View에 가까운 로우 레벨 개념이며, ActionBar는 Activity에 내장되어있고, Toolbar View를 사용하여 AppBar를 표현하는 역할이다.

 

Working with the AppBar

Working with the AppBar 구글 공식 문서에 AppBar를 구현할때 참고할 수 있는 레퍼런스를 제공하고 있다.

When using fragments, the app bar can be implemented as an `ActionBar` that is owned by the host activity.
Or a toolbar within your fragment's layout. Ownership of the app bar varies depending on the needs of your app.

AppBar 기능은 ActionBar를 사용해서 구현될 수 있으며, 이때 액션바는 호스트 액티비티에 포함되어있다. 또는 프래그먼트 레이아웃에 직접 Toolbar를 추가해서 구현하는 방법이 있다. 당신의 앱의 요구사항에 적합한 앱바 구현 방법을 선택할 수 있다.

  • 모든 스크린에서 동일한 하나의 Top-sided AppBar가 필요하다면, ActionBar로 구현할 수 있다.
    • 이는 일관된 UI를 제공하고, Up, Option menus 같은 기능을 추가하는 기능을 미리 제공한다.
  • 또는, 여러 스크린에서 각기 다른 애니메이션, 사이즈, 위치 등의 속성을 갖는 AppBar가 필요하다면, ToolBar를 통해 구현할 수 있다.

 

Google I/O 2018 Fragment에 대한 발표자료를 보면,

So in the navigation world, if your toolbar never changes between screens, then this is really handy because this means that you can populate that with whatever actions are relevant for what's in your main content.

네비게이션 과정에서 Toolbar가 변하지 않는 경우라면, Toolbar를 ActionBar에 바인딩하여 사용하는 것이 좋은 방법이다.

Now, the alternative to this is that you can go ahead and `directly manage the menus as toolbar view data`.

So most of the time today, your UIs are such that the toolbar or other sort of menu management is really kind of part of the content.

I mean, this was something that you saw back in a lot of hollow UIs, where the action bar was always Fixed Common Chrome.

이 외의 경우라면 Toolbar를 View Component로써 Activity에 종속적인 특성 없이 그대로 사용하는 것을 추천한다고 한다.

 

What is the AppBar?

위 내용을 정리하면 초기 API는 상단바 제어를 위해 ActionBar를 지원했고, 이는 Activity class 안에 내장된 기능이었다.

안드로이드 버전이 업데이트되면서, 상단바를 위한 독립된 Toolbar가 만들어졌고, Toolbar 인스턴스를 ActionBar에 바인드 시켜서 사용하는 방법과, 액티비티의 ActionBar와 전혀 별개인 Toolbar 그 자체로 사용하는 방법으로 나뉘어지게 되었다.

그리고 간단한 형태의 Toolbar 대신 CollapsingToolbar와 같은 가변적이고 변형된 Toolbar를 포함한 모두를 Material Design에서는 AppBar라고 분류한다.

 

android.material.AppBarLayout 으로 Toolbar를 감싸고 CoordinatorLayout과 함께 사용하면, Toolbar 영역을 펼치고 접을 수 있는 CollapsingToolbar를 사용할 수 있다.

  • Activity에 종속된 AppBar여도 괜찮다, 전통적인 디자인의 기본 기능만 포함된 AppBar를 편리하게 이용하고 싶다.
    • ActionBar를 사용하면 된다.
  • 각 Fragment 화면마다 Toolbar의 생김새나, 기능이 변한다.
    • Toolbar를 사용하면된다.
  • Toolbar의 사이즈가 가변적이고, Scrolling을 더하고 싶다.
    • AppBarLayout을 추가하고 CollapsingToolbar를 사용하면 된다.