Skip to content Skip to sidebar Skip to footer

Android Studio Open Drawer In Fragment

I remove the actionbar from application so I need to create button to open drawer myself button on Activity works fine but when I'm using it in fragment application stop working (w

Solution 1:

That is because you are trying to find drawerLayout in fragment which is not in fragment but in your activity. This is your mistake - DrawerLayout drawer = (DrawerLayout) getView().findViewById(R.id.drawer_layout);

Instead create method in your activity and call that method from your fragment

This will be the method in YourActivity

publicvoidopenDrawer() {
   DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.openDrawer(Gravity.START);
}

Now call this method from your fragment -

((YourActivity) getActivity()).openDrawer();

Finally, your code in fragment will be:-

button.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View v) {
              ((YourActivity) getActivity()).openDrawer();
         }
});

Solution 2:

Declare this in MainActivity where drawer is located as below

public DrawerLayout drawer;

and in onCreate of same activity (MainActivity) as above

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

In Fragment which is attached to your main activity only

button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
           ((MainActivity) getActivity()).drawer.openDrawer(Gravity.START);
        }
    });

Solution 3:

try this

button.setOnClickListener(newView.OnClickListener()
        {
            @OverridepublicvoidonClick(View v)
            {
                DrawerLayoutdrawer= (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
                drawer.openDrawer(Gravity.START);
            }
        });

Solution 4:

Best way to communicate from a fragment to its activity is through an interface. In your fragment include:

private AchievementsFragment.OnFragmentInteractionListener mListener;

publicinterfaceOnFragmentInteractionListener {
        voidopenDrawer();
        voidcloseDrawer();
    }

@OverridepublicvoidonAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            thrownewRuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @OverridepublicvoidonDetach() {
        super.onDetach();
        mListener = null;
    }

and from wherever you want to open the drawer in your fragment, you can call

mListener.openDrawer();

be sure to implement the listener and functions openDrawer and closeDrawer in your activity

publicclassNavDrawerActivityextendsAppCompatActivityimplementsAchievementsFragment.OnFragmentInteractionListener{
       //some code
    }

Solution 5:

In Kotlin it will be

In your activity add method

funopenDrawer() {
        val drawer = findViewById<View>(R.id.drawerLayout) as DrawerLayout
        drawer.openDrawer(Gravity.LEFT, true)
    }

In fragment call method

(activity as MyActivity?)?.openDrawer()

to open drawer, hope it helps someone

Post a Comment for "Android Studio Open Drawer In Fragment"