1

How do I launch different activities while the bottom navbar always remains on screen. For example if i click on a tab in the bottom navbar it launches google map view, how do i make it so that the bottom nav bar can still be seen. Very much like the youtube app, the bottom navbar is always there. If i can be pointed in the right direction that would be great i can do the research myself

Tenzin Choklang
  • 504
  • 1
  • 5
  • 21
  • This should be a good place to start. http://droidmentor.com/bottomnavigationview-with-viewpager-android/ What you are looking for is `BottomNavigationView` with `ViewPager` so you can load different `Fragment` – ᴛʜᴇᴘᴀᴛᴇʟ Apr 29 '18 at 04:16

2 Answers2

1

Fragment is a modular section of an Activity that has its own lifecycle, has its own layouts, views.., (sort of a "sub activity") Another obvious advantage of using fragments, UI optimization across different screens, it lets you manage multiple pages in one activity. Now follow the steps below :

  1. Make your main Acitivy extends FragmentActivity :

    public class MainActivity extends FragmentActivity {
    ...
    }
    
  2. Change main activity layout to something like this :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.yourComp.appName.MainActivity">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_above="@id/footer" />
    
        <LinearLayout
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" />
    
    </RelativeLayout>
    
  3. Create a new FragmentPagerAdapter like below, I define 3 pages but you may want to change this, remember you have to create one Fragment instead of each activity you need :

    public class FragmentAdapter extends FragmentPagerAdapter {
        static final int NUMBER_OF_PAGES = 3;
        Context context;
    
        public FragmentAdapter(android.support.v4.app.FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }
    
        @Override
        public int getCount() {
            return NUMBER_OF_PAGES;
        }
    
        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment2();
                case 2:
                    return new Fragment3();
                default:
                    return null;
          }
       }
    }
    
  4. Now go to the Android Studio, click on the File > New > Fragment > Fragment(Blank), and create the fragments the listed in the FragmentAdapter before. This fragments works instead of your activities.

  5. Finally add this lines to your main Activity, OnCreate method, the purpose is to bind the fragments that declared before with viewPager on the main activity layout :

    ragmentAdapter = new FragmentAdapter(((FragmentActivity) this).getSupportFragmentManager(), this);
    ViewPager pager = (ViewPager) findViewById(R.id.body);
    viewPager.setAdapter(fragmentAdapter);
    

Now design your own custom footer view or use the BottomNavigationView. Users can slid between fragments or you can programmatically change pages by using this method :

viewPager.setCurrentItem(n);  // n is fragment number that decleared in adapter 0,1,2

Also, you may want to disable user manual swiping, as describe here.

ucMedia
  • 4,105
  • 4
  • 38
  • 46
-1

If you want to use same bottom nav view then you have to use fragment. With activity you can't do it. this is the best practice.

Yes, you can also use bottom nav with activity, but you have to create nav view all other activity and set listener. ex. like youtube, lets say, 1st activity is home with nav view. then you click "trending", trending activity opened. inside the trending activity you again have to create nav view.