Home Android Studio - Navigating Activities
Reply: 1

Android Studio - Navigating Activities

user8606632
1#
user8606632 Published in 2017-09-14 03:26:03Z

I can't seem to figure this one out. I have MainActivity and created SecondActivity and ThirdActivity that I want to be able to navigate to.

I'm using BottomNavigation in my MainActivity to navigated between activities:

public class MainActivity extends AppCompatActivity {

protected BottomNavigationView navigationView;

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

BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_menuItem1:
                    Intent intent = new Intent(MainActivity.this, MainActivity.class);
                    startActivity(intent);
                    return true;
                case R.id.navigation_menuItem2:
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    startActivity(intent);
                    return true;
                case R.id.navigation_menuItem3:
                    Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
                    startActivity(intent);
                    return true;
            }
            return false;
        }

    };

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

Any clue why it's not switching pages/activities?

EDIT: Added these lines to make it work:

protected BottomNavigationView navigationView;

AND

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

Thanks for the help!

Orvenito
2#
Orvenito Reply to 2017-09-14 04:24:13Z

just in case you missed something, make sure you didn't forget something:

Create a BottomNavigationView in xml of your layout:

<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

Create a file here navigation.xml in menu resource folder. This file is used for providing the MenuItems in BottomNavigationView

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

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

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

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


</menu>

Now lets set the listener for the Click Events OnNavigationItemSelectedListener and OnNavigationItemReselectedListener on Menu Items:

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.navigation_menuItem1:
                    return true;

                case R.id.navigation_menuItem2:
                    return true;

                case R.id.navigation_menuItem3:
                    return true;


            }
            return true;
        }

    };

    private BottomNavigationView.OnNavigationItemReselectedListener mOnNavigationItemReselectedListener = new BottomNavigationView.OnNavigationItemReselectedListener() {
        @Override
        public void onNavigationItemReselected(@NonNull MenuItem item) {

            switch (item.getItemId()) {

                case R.id.navigation_menuItem1:
                    Log.d(TAG, "navigation_menuItem1 Reselected ===");
                    break;

                case R.id.navigation_menuItem2:
                    Log.d(TAG, "navigation_menuItem2 Reselected ===");
                    break;

                case R.id.navigation_menuItem3:
                    Log.d(TAG, "navigation_menuItem3 Reselected ===");
                    break;

            }

        }
    };

bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

bottomNavigationView.setOnNavigationItemReselectedListener(mOnNavigationItemReselectedListener);

EDIT Add this to your onCreate()

BottomNavigationView bottomNavigationView;
bottomNavigationView = findViewById(R.id.navigation);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304504 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO