0

how to show my navigation drawer in all activities

After creating a new project in the android studio and have a navigation drawer activity, I run it and it did not show in my all activities. ( I did not add any code, just create activity and run it). I want to know how to show navigation drawer in all activities, is there any way ?

Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
Shakib Saha
  • 3
  • 1
  • 3
  • In navigation Drawer Activity Page Use a Fragment – Raj Nov 25 '17 at 06:26
  • Follow here, you can successfully implement this [https://stackoverflow.com/questions/19451715/same-navigation-drawer-in-different-activities](https://stackoverflow.com/questions/19451715/same-navigation-drawer-in-different-activities) – Shambhu Nov 25 '17 at 07:14

1 Answers1

0

You can bind Navigation drawer with one activity. Navigation drawer will display in only activity in which you have created. if you want to view navigation drawer in all application, you can add one frame layout and add fragments there for application functionality. That app will be a single activity with multiple fragments.

Following is code to add fragment. :

 Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.content_frame, fragment)
               .commit();

For more details: Check this link. https://developer.android.com/training/implementing-navigation/nav-drawer.html

EDIT

Without Fragment

You can create one BaseActivity, all activity will be extended BaseActivity. You need to bind your navigation drawer with BaseActivity. This way, you can achieve what you want.

BaseActivity.Java

public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DrawerLayout fullView = (DrawerLayout) 
    getLayoutInflater().inflate(R.layout.activity_base, null);
    FrameLayout activityContainer = (FrameLayout) 
    fullView.findViewById(R.id.activity_content);
    getLayoutInflater().inflate(layoutResID, activityContainer, true);
    super.setContentView(fullView);


}

}

Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39