0

I want to implement an interface having BottomNavigationView where clicking on the top search bar opens the next interface shown in figure 2. I have implemented BottomNavigationView. But unable to do that above said.

enter image description here

enter image description here

Here is the code:

public class HomeActivity extends AppCompatActivity {

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

        BottomNavigationView bottomNavigationView = findViewById(R.id.home_bottom_nav_view);
        bottomNavigationView.setSelectedItemId(R.id.home_recent_menu_id);
        

        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                Fragment selectedFragment = new RecentFragment();

                switch (item.getItemId()) {

                    case R.id.home_contact_menu_id:
                        selectedFragment = new ContactFragment();
                        break;

                    case R.id.home_recent_menu_id:
                        selectedFragment = new RecentFragment();
                        break;

                    case R.id.home_status_menu_id:
                        selectedFragment = new StatusFragment();
                        break;

                }

                getSupportFragmentManager().beginTransaction().replace(R.id.home_fragment_container_lyt_id, selectedFragment).commit();
                return true;

            }
        });


    }


}

Home Activity XML code

    <?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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.SearchView
        android:id="@+id/home_search_view_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <FrameLayout
        android:id="@+id/home_fragment_container_lyt_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/home_search_view_id"
        android:layout_above="@+id/home_bottom_nav_view"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/home_bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu"
        android:layout_alignParentBottom="true"
        />


</RelativeLayout>
Bhaskar Jyoti Dutta
  • 1,740
  • 2
  • 15
  • 30

1 Answers1

2
  1. create a BottomNavigationFragment which is going to contain ViewPager2 with BottomNavigationView and that view pager will contain [favourite, recent and contacts ] fragments

  2. create a SearchFragment which will be used for searching purpose

  3. create a navigation graph which will contain [BottomNavigationFragment and SearchFragment] when user clicks on search view navigate him to SearchFragment

you can also set animation using Navigation Component lib

//adapter for ViewPager2 vp2 in BottomNavigationFragment enter image description here

lets say your SearchView is in favourite fragment onClick or onFocus of that

findNavController().navigate(R.id.toSearchFragment)

you will land in Search fragment

Abhijith mogaveera
  • 918
  • 10
  • 16