본문 바로가기

개발/android

안드로이드 버튼 그룹을만들고 싶을 때 MaterialButtonToggleGroup 을 쓰자

반응형

Bootstrap이나 기타 UI를 보면 버튼 그룹이 있다

안드로이드에서 써보려고 하면 뭔가 되게 심플하지 않게 별로라서 

커스텀에 커스텀을 더하게 되는데

 

아래와 같이 패키지 추가하면 라디오버튼 대신 나름 쓸만한 버튼 그룹을 쓸 수 있다 

첫번째는 icon을 입력한 것, 두번째는 그냥 텍스트 outlineButton, 세번째는 UnelevatedButton Style

원하는 대로 선택해서 세팅해서 쓰면 끝

singleSelection 로 몇개 선택 가능한지 설정 하면 되고

다만 현재 버전까지는 orientation이 horizontal로만 되는 것 같다 

implementation 'com.google.android.material:material:1.1.0-alpha10'

 

<com.google.android.material.button.MaterialButtonToggleGroup
                    android:id="@+id/btnsCategory"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minHeight="@dimen/height"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/tvTitle"
                    android:layout_margin="@dimen/padding_big"
                    app:singleSelection="true"
                    android:gravity="center"
                    app:checkedButton="@id/btnCategoryA"
                    >
                    <com.google.android.material.button.MaterialButton
                        android:id="@+id/btnCategoryA"
                        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/height"
                        android:text="@string/talk"
                        app:icon="@drawable/icon_talk"
                        app:iconSize="@dimen/icon_sm"
                        />

                    <com.google.android.material.button.MaterialButton
                        android:id="@+id/btnCategoryB"
                        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/height"
                        android:text="@string/photo"
                        app:icon="@drawable/icon_photo"
                        app:iconSize="@dimen/icon_sm"
                        />

                    <com.google.android.material.button.MaterialButton
                        android:id="@+id/btnCategoryC"
						style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/height"
                        android:text="@string/mostlike"
                        app:icon="@drawable/icon_player"
                        app:iconSize="@dimen/icon_sm"
                        />
                </com.google.android.material.button.MaterialButtonToggleGroup>

 

 

참고

https://developer.android.com/reference/com/google/android/material/button/MaterialButtonToggleGroup

 

MaterialButtonToggleGroup  |  Android Developers

From class android.view.ViewGroup void addChildrenForAccessibility(ArrayList arg0) void addFocusables(ArrayList arg0, int arg1, int arg2) void addKeyboardNavigationClusters(Collection arg0, int arg1) boolean addStatesFromChildren() void addTouchables(Array

developer.android.com

https://material.io/develop/android/components/material-button/

 

Material Button - Material Components for Android

Material Button Material Button is a customizable button component with updated visual styles. This button component has several built-in styles to support different levels of emphasis, as typically any UI will contain a few different buttons to indicate d

material.io

https://medium.com/over-engineering/hands-on-with-material-components-for-android-buttons-76fa1a92ec0a

 

Hands-on with Material Components for Android: Buttons

Part 4 of a series covering practical usage of Material Components for Android

medium.com

 

반응형