0

I am a newbie to Android development. I am learning UI designing as of now. I want a solution where there is a bottom bar with 5 options linked directly to 5 different activities. I got Java solutions from other stack overflow answers (How to change activity on bottom navigation button click? ) - 2nd Answer by sushil, but it has no activity - XML files in it for me to understand.

Bottom bar like this:

Bottom bar like this

Activity to be loaded based on bottom bar:

Activity to be loaded based on bottom bar

tuomastik
  • 4,559
  • 5
  • 36
  • 48

3 Answers3

0
bottomNavigationView.setOnNavigationItemSelectedListener(
    new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_favorites:
                     Intent intent1 = new Intent(this, AnActivity.class);
                     startActivity(intent1);
                     break;
                case R.id.action_schedules:
                  Intent intent2 = new Intent(this, AnotherActivity.class);
                  startActivity(intent2);
                  break;
                case R.id.action_music:
                   Intent intent3 = new Intent(this, AnotherActivity.class);
                startActivity(intent3);
                break;
            }
            return true;
        }
    });
Akshay Chopde
  • 670
  • 4
  • 10
0

As per your attached image, you should used Fragment instead of Activity for 5 different MenuItem or options to achieve your desired output.

1. Create 5 different Fragments for 5 different MenuItem. For example: MatchingFragment, WatchListFragment, RatesFragment, DealsFragment and ListingFragment.

2. Add OnNavigationItemSelectedListener to your NavigationView and change Fragment as per your selected MenuItem. Use below code to change Fragment:

// Set action to perform when any menu-item is selected.
bottomNavigationView.setOnNavigationItemSelectedListener(
    new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            // Change Fragment
            selectFragment(item);
            return false;
        }
    });

/**
 * Perform action when any item is selected.
 *
 * @param item Item that is selected.
 */
protected void selectFragment(MenuItem item) {

    item.setChecked(true);

    switch (item.getItemId()) {
        case R.id.action_matching:
            // Action to perform when Matching Menu item is selected.
            pushFragment(new MatchingFragment());
            break;
        case R.id.action_watch_list:
            // Action to perform when WatchList Menu item is selected.
            pushFragment(new WatchListFragment());
            break;
        case R.id.action_rates:
            // Action to perform when Rates Menu item is selected.
            pushFragment(new RatesFragment());
            break;
        case R.id.action_deals:
            // Action to perform when Deals Menu item is selected.
            pushFragment(new DealsFragment());
            break;
        case R.id.action_listing:
            // Action to perform when Listing Menu item is selected.
            pushFragment(new ListingFragment());
            break;
    }
}

/**
 * Method to push any fragment into given id.
 *
 * @param fragment An instance of Fragment to show into the given id.
 */
protected void pushFragment(Fragment fragment) {
    if (fragment == null)
        return;

    FragmentManager fragmentManager = getFragmentManager();
    if (fragmentManager != null) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (ft != null) {
            ft.replace(R.id.rootLayout, fragment);
            ft.commit();
        }
    }
}

Here is the complete Tutorial: Android Bottom Navigation View Tutorial With Example

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • yes bro. but i want to do on activity like show in image and link i was provided. – chaitanya paronawala Jul 05 '17 at 10:25
  • Its not possible with activity. First of all you have to know `When to use BottomNavigationView`. See the usage and design guideline provided by google. https://material.io/guidelines/components/bottom-navigation.html# – Ferdous Ahamed Jul 05 '17 at 10:29
  • bro see the link i will provide in question one person done with activity – chaitanya paronawala Jul 05 '17 at 10:48
  • You can open new activity its TRUE but for this case you have to add 5 `BottomNavigationView` to 5 different activity and you have to select `MenuItem` programmatically from `onCreate()` method when activity first opened. When you select another `MenuItem` then you should finish all previous activity and start new activity for selected MenuItem. Its not logical and not user friendly i think. Read the design guideline again and googling about BottomNavigationView tutorial and you will find the proper use of it. Hope you understand. – Ferdous Ahamed Jul 05 '17 at 10:57
  • ok bro ya that code has not showing proper details. but can u help me for fragment becoz i have all activity code how can i convert that code in fragment? – chaitanya paronawala Jul 05 '17 at 11:47
  • Check this >> https://stackoverflow.com/questions/21205732/converting-activity-into-fragment and https://stackoverflow.com/questions/35785049/converting-an-activity-to-a-fragment – Ferdous Ahamed Jul 05 '17 at 11:50
  • in my activity i m using many adapter. so that all adapter change activity to fragment else adapter as same it is.??? – chaitanya paronawala Jul 05 '17 at 12:38
  • No need to change adapter. 1. Just change `onCreate()` to `onCreateView()` and inflate layout to view and return. 2. Use `getActivity()` as context. – Ferdous Ahamed Jul 05 '17 at 12:42
  • If my answer seems useful, then please mark it as correct answer. – Ferdous Ahamed Jul 05 '17 at 14:12
  • @FerdousAhamed It is absolutely possible with the activities, you declare 5 activities, their layouts and each layout has bottom bar with respective icon selected based on the activity. – Aalap Patel Aug 06 '18 at 14:17
0

Base Activity :

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.bottom_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_analysis) {
            startActivity(new Intent(getApplicationContext(), AnalysisActivity.class));
        } else if (itemId == R.id.navigation_dashboard) {
            startActivity(new Intent(getApplicationContext(), DashboardActivity.class));
        } else if (itemId == R.id.navigation_profile) {
            startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
        }
        finish();
    }, 100);
    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();

}

Make sure to use getApplicationContext in onNavigationSelected().

Dashboard Activity:

public class DashboardActivity extends BaseActivity {

@Override
int getContentViewId() {
    return R.layout.activity_dashboard ;
}

@Override
int getNavigationMenuItemId() {
    return R.id.navigation_dashboard;
}  }

Similarly - make the other activities just like this.

XML PART

bottom_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.BottomNavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"
/>

navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/navigation_analysis"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/title_analysis" />

<item
    android:id="@+id/navigation_dashboard"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="@string/title_dashboard" />

<item
    android:id="@+id/navigation_profile"
    android:icon="@drawable/ic_notifications_black_24dp"
    android:title="@string/title_profile" />

</menu>

activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/frame_dashboard">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_dashboard"/>
</FrameLayout>

<include
    layout="@layout/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    />


</LinearLayout>

Similarly - make other xml files just like this. This should hopefully help

Matthias Seifert
  • 2,033
  • 3
  • 29
  • 41
shellym
  • 546
  • 1
  • 5
  • 11