Alex K
Alex K

Reputation: 81

Android how to call Navigation Drawer from fragment

In my fragment I have material search bar with navigation button(humbugger). How can I call Navigation Drawer which is in main activity with that humbugger button in my fragment Do not get how to handle it inside DictionaryFragment

MainActivity:

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    LinearLayout content = (LinearLayout) findViewById(R.id.content);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
            drawer,
            R.string.nav_open_drawer,
            R.string.nav_close_drawer){

    };
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

DictionaryFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

materialSearchBar = (MaterialSearchBar) RootView.findViewById(R.id.search_bar);

...

materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {

        @Override
        public void onButtonClicked(int buttonCode) {
             //***HOW TO HANDLE IT HERE?***
            //switch (buttonCode) {
              //  case MaterialSearchBar.BUTTON_NAVIGATION:
              //      drawer.openDrawer(Gravity.START);
              //      break;}
        }
    });
       //return RootView;
}

layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/searchBarDividerColor"
tools:context="com.hfad.catchat.DictionaryFragment">

<com.mancj.materialsearchbar.MaterialSearchBar
    android:id="@+id/search_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:mt_hint="Search"
    app:mt_navIconEnabled="true"
    app:mt_speechMode="false" />

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_search"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

</android.support.v7.widget.RecyclerView>
</LinearLayout>

Upvotes: 0

Views: 145

Answers (1)

h_malik
h_malik

Reputation: 167

This is what you can try where you want to toggle navigation drawer in your fragment, this way you will have to write a method in activity to do whatever you want to do from your fragment, be sure it is a public method:

    ((MainActivity)getContext()).toggleDrawer();

in your MainActivity:

    public void toggleDrawer(){
    //do your stuff
    }

Other way is callback aka interface (the preferred one), pass that as a parameter in fragment's constructor and consume that where you want to change drawer's state. Like inside your activity:

    Callback callback = new Callback(){
    @Override
    public void onDrawerStateChanged(){
      //do your stuff
    }};
    new DictionaryFragment(callback);

And inside your fragment you will have to write a constructor to accept that callback and save in a local variable :

    public DictionaryFragment() {
    }


    @SuppressLint("ValidFragment")
    public DictionaryFragment(Callback callback) {
    this.callback = callback;
    }

And use it like :

    callback.onDrawerStateChanged();

You can also pass parameters to MainActivity both ways.

Upvotes: 1

Related Questions