then give project a name, By default this will be used as app name and dont worry you can change it later
Im giving it name NavigationProject you can give any name you like
then click on empty activity and click finish to complete creating the project. Now you have successfully created project.
If you run the project you should get a similar view
Now you are landed on default activity which is mainactivity file which we dont need so we are going to create new activity which is navigation activity and set it as our launching activity.For that navigate to file -> new -> activity -> navigation activity also remember to set it to launcher activity. Click on finish. After gradle build is finished, go to android manifest file and delete the intent filter tag from mainactivity so that it wont launch for the first time when the app is opened.
now you may have noticed that we have multiple files created by android studio. we got,
NavigationActivity.java(Activity file)
activity_navigation.xml(layout file)
content_navigation.xml
app_bar_navigation.xml
nav_header_navigation.xml
Now you may wonder why we need so many files to create a simple navigation drawer. You will find out soon after you follow my instructions.
Now if you run the app you will get screen similar to this
Now Im going to remove the floating action button on the bottom right side. Since we dont need it.
Sow now lets change the data in navigation drawer content like the profile image,name and email
To change data simply navigate to nav_header_navigation_drawer.xml andchange text values and default image.
I have changed the text color to white and changed the default image with a avatar image (You can find lots of images from flaticon.com)
Now lets change the green gradient drawable in background.
Navigate to res->drawable->side_nav_bar.xml
remove the center color attribute and change the start and endcolor to your desire..(need some good gradients? head to uigradients website!!).
final code for gradient file will be something like this
So now we have made our app visually appeal to the users with navigation drawer. But thats not all you have to implement click functions for user to interact with your app. So lets start by interacting with the elements in navigation drawer. For the sake of making this tutorial as small as possible, Im going to remove all menu items except one. You have to edit activity_navigation_drawer_drawer.xml file inside menu folder. final code will be as below
Now If you build the app you will get an error because we are referring these deleted menu items in the java file which is NavigationActivity.java
The onNavigationItemSelected method in this file should be like this.
NavigationDrawerActivity.java
@OverridepublicbooleanonNavigationItemSelected(MenuItem item) {// Handle navigation view item clicks here.int id =item.getItemId();if (id ==R.id.nav_home) {Toast.makeText(this,"clicked home",Toast.LENGTH_SHORT).show(); } DrawerLayout drawer =findViewById(R.id.drawer_layout);drawer.closeDrawer(GravityCompat.START);returntrue;}
When the home button is clicked it will show the toast message.You could add an intent other than toast to navigate to another activity on button click.
We will remove floating action button since we no longer need it.
remove this code snippet from app_bar_navigation_drawer.xml
Also we need to remove its reference in java file. Remove below code from onCreate method.
NavigationDrawerActivity.java
FloatingActionButton fab =findViewById(R.id.fab);fab.setOnClickListener(new View.OnClickListener() { @OverridepublicvoidonClick(View view) {Snackbar.make(view,"Replace with your own action",Snackbar.LENGTH_LONG).setAction("Action",null).show(); }});
Now you are good to go you shouldnt see any error if so dont be scared to ask in comments below. Will also implement click events for menu item. You can see any empty if method in onOptionsItemSelected method. Simply add code inside the if condition so Im adding a simple toast.
NavigationDrawerActivity.java
if (id ==R.id.action_settings) {Toast.makeText(this,"Clicked settings",Toast.LENGTH_SHORT).show();returntrue;}
Final java code should look like below
NavigationDrawerActivity.java
packagecom.techpakka.navigationdrawer;importandroid.os.Bundle;importcom.google.android.material.floatingactionbutton.FloatingActionButton;importcom.google.android.material.snackbar.Snackbar;importandroid.view.View;importandroidx.core.view.GravityCompat;importandroidx.appcompat.app.ActionBarDrawerToggle;importandroid.view.MenuItem;importcom.google.android.material.navigation.NavigationView;importandroidx.drawerlayout.widget.DrawerLayout;importandroidx.appcompat.app.AppCompatActivity;importandroidx.appcompat.widget.Toolbar;importandroid.view.Menu;importandroid.widget.Toast;publicclassNavigationDrawerActivityextendsAppCompatActivityimplementsNavigationView.OnNavigationItemSelectedListener { @OverrideprotectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_navigation_drawer);Toolbar toolbar =findViewById(R.id.toolbar);setSupportActionBar(toolbar);FloatingActionButton fab =findViewById(R.id.fab);fab.setOnClickListener(new View.OnClickListener() { @OverridepublicvoidonClick(View view) {Snackbar.make(view,"Replace with your own action",Snackbar.LENGTH_LONG).setAction("Action",null).show(); } });DrawerLayout drawer =findViewById(R.id.drawer_layout);NavigationView navigationView =findViewById(R.id.nav_view);ActionBarDrawerToggle toggle =newActionBarDrawerToggle(this, drawer, toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);drawer.addDrawerListener(toggle);toggle.syncState();navigationView.setNavigationItemSelectedListener(this); } @OverridepublicvoidonBackPressed() {DrawerLayout drawer =findViewById(R.id.drawer_layout);if (drawer.isDrawerOpen(GravityCompat.START)) {drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.navigation_drawer, menu);returntrue; } @OverridepublicbooleanonOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id =item.getItemId();//noinspection SimplifiableIfStatementif (id ==R.id.action_settings) {Toast.makeText(this,"Clicked settings",Toast.LENGTH_SHORT).show();returntrue; }return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @OverridepublicbooleanonNavigationItemSelected(MenuItem item) {// Handle navigation view item clicks here.int id =item.getItemId();if (id ==R.id.nav_home) {Toast.makeText(this,"clicked home",Toast.LENGTH_SHORT).show(); }DrawerLayout drawer =findViewById(R.id.drawer_layout);drawer.closeDrawer(GravityCompat.START);returntrue; }
If you want to customize navigation drawer further more like loading profile image and details to drawer from server, Stay tuned I will be adding a tutorial soon. Happy coding!