How to highlight the item when pressed using BottomNavigationView between activities?

Related searches

I have added Bottom Navigation View for my app but I need the Bottom Navigation View between activities instead of fragment so I have added this code to Java for all my 3 activities.

When I select Second or Third in my phone all things are correct but the problem is the highlight goes to the First item.

I need to highlight the item I press.

I have used fragment and it works perfectly but I am still beginner for using fragment so I am using activities.

The first activity code is:

BottomNavigationView mBottomNavigation;

    mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);

    mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.Nav_Second:
                    Intent Second= new Intent(First.this, Second.class);
                    startActivity(Second);
                    break;
                case R.id.Nav_Third:
                    Intent Third= new Intent(First.this, Third.class);
                    startActivity(Third);
                    break;
            }

            return true;
        }
    });


}}

The second activity is:

BottomNavigationView mBottomNavigation;

    mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);

    mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.Nav_First:
                    Intent First= new Intent(Second.this, First.class);
                    startActivity(First);
                    break;
                case R.id.Nav_Third:
                    Intent Third= new Intent(Second.this, Third.class);
                    startActivity(Third);
                    break;
            }

            return true;
        }
    });

}}

The third activity is:

BottomNavigationView mBottomNavigation;

    mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);

    mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.Nav_First:
                    Intent First= new Intent(Third.this, First.class);
                    startActivity(First);
                    break;
                case R.id.Nav_Second:
                    Intent Second= new Intent(Third.this, Second.class);
                    startActivity(Second);
                    break;
            }

            return true;
        }
    });


}}

The xml are the same for 3 activities:

<android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/BottomNavigator"
        android:background="@color/colorPrimaryDark"
        android:layout_alignParentBottom="true"
        app:itemTextColor="@drawable/item_bg"
        app:itemIconTint="@drawable/item_bg"
        app:menu="@menu/navigate_items">
    </android.support.design.widget.BottomNavigationView>

You can use BottomNavigationView with activities representing tabs. The key is to repeat the navigation view component in each activity, and have the code of each activity control the navigation component: starting the right activity on navigation item clicks and selecting a proper navigation item after the activity has started.

You need to start newly selected activities (tabs) with a delay as to allow the tab switching animation to complete before the new activity replaces the previous one.

My approach was to have all activities representing tabs inherit from the same BaseActivity class implementing the common behavior.

This is the code of an example BaseActivity:

public abstract class BaseActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    protected BottomNavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());

        navigationView = (BottomNavigationView) findViewById(R.id.navigation);
        navigationView.setOnNavigationItemSelectedListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        updateNavigationBarState();
    }

    // Remove inter-activity transition to avoid screen tossing on tapping bottom navigation items
    @Override
    public void onPause() {
        super.onPause();
        overridePendingTransition(0, 0);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        navigationView.postDelayed(() -> {
            int itemId = item.getItemId();
            if (itemId == R.id.navigation_home) {
                startActivity(new Intent(this, HomeActivity.class));
            } else if (itemId == R.id.navigation_dashboard) {
                    startActivity(new Intent(this, DashboardActivity.class));
            } else if (itemId == R.id.navigation_notifications) {
                    startActivity(new Intent(this, NotificationsActivity.class));
            }
            finish();
        }, 300);
        return true;
    }

    private void updateNavigationBarState(){
        int actionId = getNavigationMenuItemId();
        selectBottomNavigationBarItem(actionId);
    }

    void selectBottomNavigationBarItem(int itemId) {
        Menu menu = navigationView.getMenu();
        for (int i = 0, size = menu.size(); i < size; i++) {
            MenuItem item = menu.getItem(i);
            boolean shouldBeChecked = item.getItemId() == itemId;
            if (shouldBeChecked) {
                item.setChecked(true);
                break;
            }
        }
    }

    abstract int getContentViewId();

    abstract int getNavigationMenuItemId();

}

This is my whole example based on the Android Studio's Bottom Navigation Activity template:

https://github.com/ddekanski/BottomNavigationViewBetweenActivities

java - How to highlight the item when pressed using , You can use BottomNavigationView with activities representing tabs. The key is to repeat the navigation view component in each activity, and have the code of� In this blog, we have learned how to add the bottom menu using BottomNavigationView. Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap. It should be used when the application has three to five top-level destinations. How to implement. Step 1: Add the below layout,

To anyone still looking for this, @javazian's answer of extending each activity is real overkill in my opinion.

A much more succinct solution is to retrieve the nav menu and manually check the relevant menu item.

Note in the example below, INSERT_INDEX_HERE needs to be replaced with the index of the menu item, (e.g. the menu item on the far left would have index 0).

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

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    // Ensure correct menu item is selected (where the magic happens)
    Menu menu = navigation.getMenu();
    MenuItem menuItem = menu.getItem(INSERT_INDEX_HERE);
    menuItem.setChecked(true);
}

How to highlight the item when pressed using , I have added Bottom Navigation View for my app but I need the Bottom Navigation View between activities instead of fragment so I have added this code to Java� You can add this Bottom Navigation bar using the BottomNavigationView widget from the design support library. In this tutorial, we will create an app with Bottom Navigation Bar and Fragments displayed for each menu action.

How to highlight the item when pressed using , but I need the Bottom Navigation View between activities instead of fragment How to highlight the item when pressed using BottomNavigationView between� Sets whether the menu items horizontally translate on selection when the combined item widths fill up the screen. void: setItemIconSize(int iconSize) Set the size to provide for the menu item icons. void: setItemIconSizeRes(int iconSizeRes) Set the size to provide for the menu item icons using a resource ID. void

You can use setSelectedItemId in each activity when it starts.

bottomNavigationView.setSelectedItemId(R.id.menu_item_id);

where R.id.menu_item_id located in menu/navigate_items

Fragment Navigation Drawer, Each item when clicked will switch the relevant fragment into the activity's In order to slide our navigation drawer over the ActionBar, we need to use the new Toolbar see the navigation drawer and be able to select between your fragments. Questions: I am using the new android.support.design.widget.BottomNavigationView from the support library. How can I set the current selection from code? I realized, that the selection is changed back to the first item, after rotating the screen. Of course it would also help, if someone could tell me, how to “save” the current state of the

Bottom navigation, Make your activity extend android.support.v7.app.AppCompatActivity . public class MainActivity extends AppCompatActivity { } III. Declare navigation items in � I've currently been implementing my BottomNavigationView which as 4 menu items by adding in the xml code to each of the 4 activities for the different menu items. Each of the 4 activities also have the same BottomNavigationView. However, each time I press a different button on the bottomNavigation

BottomNavigationView makes it easy for users to explore and switch between top-level views in a single tap.You can find basic details about BottomNavigationView in my previous post. In this post, you will learn how to make swipe tab with viewpager using android BottomNavigationView.

The labelVisibilityMode attribute can be used to adjust the behavior of the text labels for each navigation item. There are four visibility modes: LABEL_VISIBILITY_AUTO (default): The label behaves as “labeled” when there are 3 items or less, or “selected” when there are 4 items or more

Comments
  • Unless I'm missing something, this is a good scenario to use Fragments (and it works perfectly), so why not stick to that?
  • because fragment are little difficult than main activities ...
  • how can i make application extend with AppCompatActivity
  • Try looking at this example
  • I'm glad to hear it. You should consider adding an answer so that it may help others.
  • I have seen you code how to maintain states of fragments inside activities when i press tab 1 i scroll down then I press tab 2 and press tab1 it scroll me up ?
  • Instead of using overridePendingTransition(0, 0); in onPause(), Use <item name="android:windowAnimationStyle">@null</item> in the theme. See the complete solution here - stackoverflow.com/questions/6972295/…
  • Good answer but any way to not recreate the activity if it already created. For example with this approach if I am like two/more level deep within a particular activity and I select another activity and them come back to the previously selected activity, the startActivity call will recreate it and hence will bring me back to the top level of that activity