Search code examples
androidandroid-fragmentsfindviewbyid

findViewById in different layout (cannot find BottomNavigationView)


I'm doing an app so when you click on a login button, it will then display a screen with a bottom menu. But I'm having problems with setting the listener to the items in the menu. The problem is when I try to use findViewById to find my BottomNavigationView it will look for it in my activity_main instead of the fragment (welcome_fragment) where I have put it, because I only want it to show when someone has logged in. And therefore I will get null as the BottomNavigationView.

I have tried setting setContentView(R.layout.welcome_fragment) in my method "welcomePage", which does make it so that so that it will find it and it won't be null. However this results in a lot of other errors such as " No view found for id 0x7f080032 (com.example.MyAPP:id/container) for fragment WelcomePage{9547e9f #1 id=0x7f080032}", so I don't believe it to be the right way to go about it.

Here is my MainActivity:

    public class MainActivity extends AppCompatActivity {

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


        FragmentManager fm = getFragmentManager();
        Login loggingIn= new Login();
        fm.beginTransaction().replace(R.id.container, loggingIn).commit();
    }

     //When button is pressed we get into this function
     public void finishProcess(){

        WelcomePage wp = new WelcomePage();
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().addToBackStack(null);
        fm.beginTransaction().replace(R.id.container, wp).commit();


        try {
         //Here is where I get null, because we are looking in activity_main
         BottomNavigationView bottomNav=findViewById(R.id.bottomnavigate);
         bottomNavigation.setOnNavigationItemSelectedListener(menuListener);}
            catch (Exception e) {
                System.out.println(e);
            }
        }}

    //Have tried this one and it works fine
    private BottomNavigationView.OnNavigationItemSelectedListener menuListener 
     = new BottomNavigationView.OnNavigationItemSelectedListener() {...}
}

Here is my WelcomeFragment:

    public class WelcomePage extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
        return inflater.inflate(R.layout.welcome_fragment, container, false)}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Here is my layout to the welcome_fragment:


<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WelcomePage"
    android:background="@drawable/welcomegradient">

    <FrameLayout
        android:id="@+id/showing_page"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomnavigate">
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomnavigate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/mymenu"
        android:background="?android:attr/windowBackground" />


</RelativeLayout>

As I mentioned earlier the problem is that findViewById can't find the BottomNavigationView because it is looking in the activity_main instead of welcome_fragment and I don't want to put my menu in activity_main because I only want to display it when a user has logged in. How do I fix this problem?


Solution

  • Try this, it should work. WelcomePage

    public class WelcomePage extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.welcome_fragment, container, false);
    
            //Have tried this one and it works fine
            BottomNavigationView.OnNavigationItemSelectedListener menuListener 
            = new BottomNavigationView.OnNavigationItemSelectedListener() {...}
    
            BottomNavigationView bottomNav = view.findViewById(R.id.bottomnavigate);
            bottomNav.setOnNavigationItemSelectedListener(menuListener);
    
            return view;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    }
    

    MainActivity

       public class MainActivity extends AppCompatActivity {
    
       private FragmentManager fm;
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
    
           fm = getSupportFragmentManager();
           Login loggingIn= new Login();
           fm.beginTransaction().replace(R.id.container, loggingIn).commit();
       }
    
       //When button is pressed we get into this function
       public void finishProcess() {
    
            WelcomePage wp = new WelcomePage();
            fm.beginTransaction().addToBackStack(null);
            fm.beginTransaction().replace(R.id.container, wp).commit();
        }
    }