I tried playing with BottomNavigationView a little ①

android.support.design.widget.BottomNavigationView is one of the official libraries.
Bottom navigation

■ Change the color of the icon and text (when selected or not selected)

1. Create a color directory in res

2. Create buttom_navigation.xml in the color directory and write the following contents

res/color/buttom_navigation.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#FFF" />
    <item android:state_pressed="true" android:color="#FFF" />
    <item android:color="#969696" />
</selector>

3. Specify for buttom navigation view

res/layout/main.xml


...
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@color/themecolor"
        app:itemIconTint="@color/buttom_navigation"
        app:itemTextColor="@color/buttom_navigation"
        app:menu="@menu/navigation" />
...

reference: Customization of BottomNavigationView

■ Continue to display characters on unselected tabs

main.java


...

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        
        // no-hide menu-text
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) navigation.getChildAt(0);
        for (int i = 0; i < menuView.getChildCount(); i++) {
            BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i);
            itemView.setShiftingMode(false);
            itemView.setChecked(false); //Called to reflect the state of View
        }
    }

■ Fix the width and margin of all tabs

In the default state, if there are four tabs (items), the width of the selected tab will be greatly expanded and the others will be narrowed. I want to fix all four widths.

reference: Android android.support.design.widget.BottomNavigationView with 4 items not equally width [duplicate]

BottomNavigationViewHelper.java


public class BottomNavigationViewHelper {
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }
}

main.java


...
        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        BottomNavigationViewHelper.disableShiftMode(navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
...

Recommended Posts

I tried playing with BottomNavigationView a little ①
I tried to break a block with java (1)
I tried DI with Ruby
I tried to decorate the simple calendar a little
[Rails] I tried playing with the comment send button
I tried OCR processing a PDF file with Java
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
[Rails] I tried to create a mini app with FullCalendar
I tried to create a padrino development environment with Docker
I tried OCR processing a PDF file with Java part2
I tried using JOOQ with Gradle
I tried morphological analysis with MeCab
I made a GUI with Swing
I tried to interact with Java
I tried UDP communication with Java
I tried GraphQL with Spring Boot
I tried Flyway with Spring Boot
I tried customizing slim with Scaffold
I tried printing a form with Spring MVC and JasperReports 1/3 (JasperReports settings)
I tried printing a form with Spring MVC and JasperReports 3/3 (Spring MVC control)
I tried running a letter of credit transaction application with Corda 1
I tried deploying a Docker container on Lambda with Serverless Framework
I tried to make a group function (bulletin board) with Rails
I tried using Realm with Swift UI
I tried to get started with WebAssembly
I tried using Scalar DL with Docker
I tried using OnlineConverter with SpringBoot + JODConverter
Speed improvement tips with a little tech
I tried time-saving management learning with Studyplus.
I made a risky die with Ruby
I tried using OpenCV with Java + Tomcat
I tried Lazy Initialization with Spring Boot 2.2.0
I made a rock-paper-scissors app with kotlin
A little addictive story with def initialize
I made a rock-paper-scissors app with android
I tried to implement ModanShogi with Kinx
I tried embedding a formula in Javadoc
I tried learning Java with a series that beginners can understand clearly
I tried printing a form with Spring MVC and JasperReports 2/3 (form template creation)
[iOS] I tried to make a processing application like Instagram with Swift
I tried to make a Web API that connects to DB with Quarkus
I tried to build a Firebase application development environment with Docker in 2020
I tried running the route search engine (OSRM) easily with a container
[Personal memo] I learned a little about modifiers
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
I got a little more familiar with kaminari. Use kaminari_themes, generate config file
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to make Basic authentication with Java
I tried to manage struts configuration with Coggle
I tried to implement a function equivalent to Felica Lite with HCE-F of Android
I tried to manage login information with JMX
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
java I tried to break a simple block
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I made a gender selection column with enum