简体   繁体   中英

Labels in Bottom Navigation View in Android

I have a bottom navigation bar in my android project. I have 4 labels in it and only the first one 'Notes', gets labeled. To those labels, I have kept an 'Intent' to each one.

I have 4 layouts:

  1. NavigationNotes
  2. NavigationNotebooks
  3. NavigationPremium
  4. Navigation Settings

When a person opens the app, they go to the NavigationNotes layout, but when the person selects Notebooks, the NavigationNotebooks opens up, but the Notes label is still selected.

Whenever I select a label, that label's text should also appear, but it won't.

The code for bottom_nav_menu.xml is:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_notes"
    android:enabled="true"
    android:icon="@drawable/bottom_notes"
    android:title="Notes"
    app:showAsAction="ifRoom" />

<item
    android:id="@+id/action_notebooks"
    android:enabled="true"
    android:icon="@drawable/bottom_notebook"
    android:title="Notebooks"
    app:showAsAction="ifRoom" />

<item
    android:id="@+id/action_premium"
    android:enabled="true"
    android:icon="@drawable/bottom_premium"
    android:title="Premium"
    app:showAsAction="ifRoom" />

<item
    android:id="@+id/action_settings"
    android:enabled="true"
    android:icon="@drawable/bottom_settings"
    android:title="Settings"
    app:showAsAction="ifRoom" />
</menu>

The code for my NavigationNotes.java is:

package com.Notely.Notes;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.Notely.SplashScreenandAccounts.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

public class NavigationNotes extends AppCompatActivity {

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

    BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(new 
    BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_notes:
                    Intent navigationNotes = new Intent(NavigationNotes.this, NavigationNotes.class);
                    startActivity(navigationNotes);
                    break;
                case R.id.action_notebooks:
                    Intent navigationNotebooks = new Intent(NavigationNotes.this, 
                    NavigationNotebooks.class);
                    startActivity(navigationNotebooks);
                    break;
                case R.id.action_premium:
                    Intent navigationPremium = new Intent(NavigationNotes.this, 
                    NavigationPremium.class);
                    startActivity(navigationPremium);
                    break;
                case R.id.action_settings:
                    Intent navigationSettings = new Intent(NavigationNotes.this, 
                    NavigationSettings.class);
                    startActivity(navigationSettings);
                    break;
            }
            return true;
        }
    });
}
}

I see that you're using multiple activities. I suggest you use the navigation components. What you are wanting to do becomes much easier. Check the docs: https://developer.android.com/guide/navigation/navigation-ui

    bottomNavigationView.enableAnimation(boolean);
    bottomNavigationView.enableItemShiftingMode(boolean);
    bottomNavigationView.enableShiftingMode(boolean);
    bottomNavigationView.enableItemShiftingMode(boolean);
    bottomNavigationView.enableShiftingMode(boolean);

You should to have similar functions!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM