Android Navigation Drawer With Activities
Last Updated : Mar 11, 2024
In this article we will show you the solution of android navigation drawer with activities, the most well-liked feature offered by Android is really the navigation drawer, a UI panel that shows the main menu for your app's navigation.
This is additionally one of the key UI components that offers actions that users desire, including altering their user profiles or the settings for an application.
Swiping a finger from the activity's left edge reveals the navigation drawer for the user. By touching the app symbol on the action bar, they can also access it as a home activity.
The drawer icon is displayed in either top-level destinations that employ a DrawerLayout.
An essential widget inside the Android application is the Navigation Drawer. The navigation view typically uses fragments to load various displays in response to user selections.
However, using fragments with navigation might occasionally cause back stack problems.
Step By Step Guide On Android Navigation Drawer With Activities :-
XML
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.androidmads.navdraweractivity.BaseActivity" tools:showIn="@layout/app_bar_main" />
Java
public class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { // Declaration DrawerLayout drawer; FloatingActionButton fab; NavigationView navigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize Widgets Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); fab = (FloatingActionButton) findViewById(R.id.fab); drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { int id = item.getItemId(); if (id == R.id.nav_activity1) { startAnimatedActivity(new Intent(getApplicationContext(), FirstActivity.class)); } else if (id == R.id.nav_activity2) { startAnimatedActivity(new Intent(getApplicationContext(), SecondActivity.class)); } drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; }
- Open Android Studio to begin a new project.
- Choose Navigation Drawer activity and give the project a name of your choosing.
- To start a new project inside Android Studio, select "Complete."
- You will rename its Navigation Drawer activity to "BaseActivity" in this stage. As an illustration, I changed the name of the "MainActivity" to "BaseActivity."
- As demonstrated below, modify the BaseActivity's default coding.
- AppCompatActivity is really the parent of this Activity. As a result, other activities can utilize this BaseActivity as their parent activity to access AppCompatActivity's properties.
- Utilizing the OnNavigationItemSelectedListener, this Activity is implemented. You may therefore manage their navigation of activities here.
Conclusion :-
You can use this simple strategy in place of fragments because it is relatively simple to implement.
By selecting the "hamburger" icon in the navigation drawer, users can access several of the app's screens or features.
Swiping from the left also reveals the drawer; a screen then glides in and displays a variety of objects.
I hope this article on android navigation drawer with activities helps you and the steps and method mentioned above are easy to follow and implement.