質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

1670閲覧

bottomnavでselectorの使い方を知りたい

myokiya

総合スコア8

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2020/06/20 16:19

編集2020/06/21 10:54

イメージ説明

bottomnavigationで「マイページ」を開いた際に、マイページの部分が青く光るようにしたいのですが、グレーのままになってしまいます。

調べた所、「drawableにselectorを作って、state_pressed=trueにする」と書かれているものがありましたので、やってみましたがうまくいきませんでした。

不足している情報等がありましたら、指摘していただけると幸いです。よろしくお願いします。

bottom_nav.xml

kotlin

1<?xml version="1.0" encoding="utf-8"?> 2<menu xmlns:android="http://schemas.android.com/apk/res/android"> 3 4 <item 5 android:id="@+id/originFriendActivity" 6 android:icon="@drawable/ic_action_friend" 7 android:title="@string/bottomnav_friend" /> 8 <item 9 android:id="@+id/originEventActivity" 10 android:icon="@drawable/ic_action_event" 11 android:title="@string/bottomnav_event" /> 12 <item 13 android:id="@+id/originMyPageActivity" 14 android:icon="@drawable/ic_action_mypage" 15 android:title="@string/bottomnav_mypage" /> 16 17</menu>

activity_origin_my_page.xml

kotlin

1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/originmypage_relativelayout" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 app:background="@color/colorPrimary" 9 tools:context=".originMyPage.OriginMyPageActivity"> 10 11 12 <com.google.android.material.bottomnavigation.BottomNavigationView 13 android:id="@+id/btm_nav" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:layout_alignParentStart="true" 17 android:layout_alignParentBottom="true" 18 android:layout_marginStart="-2dp" 19 android:layout_marginBottom="-7dp" 20 app:itemBackground="@color/whiteColor" 21 app:menu="@menu/bottom_nav" /> 22 23 <LinearLayout 24 android:id="@+id/originmypage_linearlayout" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:fitsSystemWindows="true" 28 android:orientation="vertical"> 29 30 <androidx.appcompat.widget.Toolbar 31 android:id="@+id/toolbar" 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content" 34 android:background="?attr/colorPrimary" 35 android:minHeight="?attr/actionBarSize" 36 android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 37 app:titleTextColor="@android:color/white" /> 38 39 <FrameLayout 40 android:id="@+id/originmypage_framelayout" 41 android:layout_width="match_parent" 42 android:layout_height="match_parent"> 43 44 </FrameLayout> 45 46 </LinearLayout> 47 48 49</RelativeLayout> 50 51

selector.xml

kotlin

1<?xml version="1.0" encoding="utf-8"?> 2<selector xmlns:android="http://schemas.android.com/apk/res/android"> 3 4<item android:drawable="@drawable/ic_action_friend" 5 android:state_pressed="true" /> 6<item android:drawable="@drawable/ic_action_friend" /> 7 8 <item android:drawable="@drawable/ic_action_event" 9 android:state_pressed="true" /> 10 <item android:drawable="@drawable/ic_action_event" /> 11 12 13 <item android:drawable="@drawable/ic_action_mypage" 14 android:state_pressed="true" /> 15 <item android:drawable="@drawable/ic_action_mypage" /> 16 17</selector>

(補足)

https://gyazo.com/1af373aad44d1ebd5cc1b1d83963a346

OnNavigationItemSelectedListenerの一番下をtrueに返した所、画面遷移後にもう一度ボタンを押せば光るようにはなったのですが、遷移と同時に光ってはくれませんでした。

OriginMyPageActivity.kt

kotlin

1 private val mOnNavigationItemSelectedListener = 2 BottomNavigationView.OnNavigationItemSelectedListener { 3 item -> 4 when (item.itemId){ 5 R.id.originFriendActivity -> { 6 val intent = Intent(this, OriginFriendActivity::class.java) 7 startActivity(intent) 8 return@OnNavigationItemSelectedListener true 9 } 10 R.id.originEventActivity -> { 11 12 } 13 R.id.originMyPageActivity ->{ 14 val intent = Intent(this, OriginMyPageActivity::class.java) 15 startActivity(intent) 16 return@OnNavigationItemSelectedListener true 17 } 18 19 } 20 true 21 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

まず、色が変わらない点についてですが、BottomNavigationのアイテムが選択されていないのではないでしょうか?BottomNavigationView.OnNavigationItemSelectedListenerの実装でtrueを返すようにしないと、タップしたアイテムが選択されません。falseを返すようになっていないか確認してみてください。

BottomNavigationのアイテムが正しく選択されていれば、アプリのテーマ内で設定したprimaryColorがアイテムの選択色になっているはずです。これを任意の色に変更したい場合は、BottomNavigationViewのapp:itemIconTintを利用してselectorのXMLを設定します。

  • res/color/bottom_navigation_tint.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<selector xmlns:android="http://schemas.android.com/apk/res/android"> 3 <!-- 選択時 --> 4 <item android:color="..." android:state_selected="true"/> 5 <!-- 通常時 --> 6 <item android:color="..."/> 7</selector>
  • layout内

xml

1<com.google.android.material.bottomnavigation.BottomNavigationView 2 ... 3 app:itemIconTint="@color/bottom_navigation_tint" 4 app:itemTextColor="@color/bottom_navigation_tint" 5 />

参考: ドキュメント
https://material.io/develop/android/components/bottom-navigation/

投稿2020/06/21 07:36

編集2020/06/21 07:37
kakajika

総合スコア3131

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

myokiya

2020/06/21 08:32

丁寧なご回答ありがとうございます。 ご指摘いただいた点を反映したのですが、解決することができませんでした。 補足に書きました通り、OnNavigationItemSelectedListenerも入っておりました。 また、MVP構成でFragmentではなく、Activityの部分にbottomnavを記入していることも理由の1つではないかと思っているのですが、そこは関係ないでしょうか? よろしくお願いいたします。
kakajika

2020/06/21 08:39

OnNavigationItemSelectedListenerでtrueを返すようにしないと、アイテムは選択されません。ご提示のコードの "R.id.originMyPageActivity ->" の箇所は何も返していないので、一番下でfalseを返してしまっていますよね。そちらを修正してみてください。
myokiya

2020/06/21 11:30

一番最後の部分をtrueに変更したのですが、あってますでしょうか? 現状を再び補足に記入いたしましたので、ご確認お願いいたします。
kakajika

2020/06/22 13:44

trueを返す点については補足のコードで問題ないと思います。ただ、アイテムの選択時に新しいActivityへ遷移するようになっていますね。だとしたら、遷移先のActivityでもBottomNavigationViewのアイテムを選択するように実装しないといけないはずです。 BottomNavigationViewは基本的にはActivityを起動するのではなく、コンテンツ部分のFragmentを切り替えるように利用するものですので、そのように実装した方がお望みの挙動を実現しやすいと思います。
myokiya

2020/06/29 10:06

遅くなってしまい申し訳ありません。 自分で調べてみたのですが、「遷移先のactivityでアイテムの選択をする」の具体的な方法が分かりませんので、教えていただけないでしょうか? 自分なりには、onoptionsitemselectedやonnavdestinationselectedの使用かと考えました。 そうなのですね、全く知りませんでした。 この問題の解決ができ次第、fragmentへの切り替えも仲間と話してみます!
kakajika

2020/07/11 02:58

返信遅くなりすみません。 > 自分で調べてみたのですが、「遷移先のactivityでアイテムの選択をする」の具体的な方法が分かりませんので、教えていただけないでしょうか? 遷移先のActivityでBottomNavigationViewに対してsetSelectedItemIdメソッドを呼んであげればOKです。
myokiya

2020/07/12 08:20

なんとかできました… 詳らかにありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問