본문 바로가기

Android Studio

[Android Studio] Bottom Navigation View에서 icon 클릭시 이미지 바꾸는 방법

이미지를 2개 준비 해줍니다. 클릭 안됐을 때 보여줄 이미지 한개랑 클릭 됐을때 보여줄 이미지 한개 준비 해줍니다.

저는 ic_friends_uncheck.xml (SVG 이미지 입니다.), ic_friends_check.xml(이것또한 SVG 이미지) 2개 준비 했습니다.

 

시작 !!!

drawable -> New -> Drawable Resource File 파일 생성

 

파일 이름을 지어줍니다.

 

위에 이미지의 빨강색 동그라미를 클릭하면 xml 코드를 작성할 수 있습니다.

 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_friends_uncheck" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_friends_check" android:state_checked="true"/>
</selector>

위에 코드 5줄을 입력 해줍니다.

 

android:icon="@drawable/selector_tab_friends"

menu 폴더에 만든 bottom_navigation_main은 Bottom Navigation View를 구성할 때 필요한 xml 파일 입니다. 해당 item에 위에 코드를 입력 해줍니다.

 

끝!!