Hey, today I am going to discuss Drawer Layout. This means you will get knowledge about how to add drawer layout with an event listener. The navigation drawer can be implemented by using the Drawer Layout widget. In the layout of your activity, you should define the DrawerLayout element.
This is also known as the navigation drawer. This is a layout that displays the app’s main navigation options on the left edge of the right edge of the screen. Let's set up a basic navigation drawer based on the following steps with DrawerLayout.
What is the Drawer Layout?
As we discuss drawer layout acts as a top-level container for window content that allows for interactive drawer views to be pulled out from one or both vertical edges of the window. You can use it in your app either left edge or right edge. Mostly drawer layout is used on the left edge.
Create a Drawer Layout
Adding a navigation drawer, into the app is very simple. This is the top-level layout of the activity. You need to add this layout to your XML file. This XML file can be your activity file or fragment file.
Main XML Layouts
Let's take an example for the navigation drawer. First of all, add this XML snippet to your XML file.
Create a header layout for the navigation drawer now. This XML design will show on the top of the navigation drawer. You can customize with your own design patterns.
A most important segment of the navigation drawer is menu design. There are many two ways to design your menu, the first one is by using XML and second is dynamic via code. Sample for navigation menu like this by using XML.
Now, these are the code snippets where you handle your drawer layout with an event listener. You can access your drawer layout activity as well as your drawer layout menu. You just need to create your drawer layout object in your activity to access all features of the drawer layout.
Final code for drawer layout with an event listener
package com.codingissue.example;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.util.Log;
import android.view.View;
import androidx.core.view.GravityCompat;
import androidx.appcompat.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
publicclassMainActivityextendsAppCompatActivityimplementsNavigationView.OnNavigationItemSelectedListener {
privateStringTAG="MainActivity";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbartoolbar= findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButtonfab= findViewById(R.id.fab);
fab.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayoutdrawer= findViewById(R.id.drawer_layout);
NavigationViewnavigationView= findViewById(R.id.nav_view);
ActionBarDrawerToggletoggle=newActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
drawer.addDrawerListener(newDrawerLayout.DrawerListener() {
@OverridepublicvoidonDrawerSlide(View drawerView, float slideOffset) {
//Log.i(TAG, "onDrawerSlide");
}
@OverridepublicvoidonDrawerOpened(View drawerView) {
Log.i(TAG, "onDrawerOpened");
}
@OverridepublicvoidonDrawerClosed(View drawerView) {
Log.i(TAG, "onDrawerClosed");
}
@OverridepublicvoidonDrawerStateChanged(int newState) {
//Log.i(TAG, "onDrawerStateChanged");
}
});
}
@OverridepublicvoidonBackPressed() {
DrawerLayoutdrawer= 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.main, 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.intid= item.getItemId();
//noinspection SimplifiableIfStatementif (id == R.id.action_settings) {
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")@OverridepublicbooleanonNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.intid= item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
} elseif (id == R.id.nav_gallery) {
} elseif (id == R.id.nav_slideshow) {
} elseif (id == R.id.nav_tools) {
} elseif (id == R.id.nav_share) {
} elseif (id == R.id.nav_send) {
}
DrawerLayoutdrawer= findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
returntrue;
}
}
Conclusion
In the above post, we learn about the navigation drawer layout with an event listener. We also learned drawer layout can be adjusted on the left edge and right edge of the screen. We can detect navigation item click as well as navigation movement.
No comments:
Post a Comment