What Is an App Drawer on Android

Navigation Drawer in Android

The navigation drawer is the most common feature offered by android and the navigation drawer is a UI panel that shows your app's main navigation menu. It is also one of the important UI elements, which provides actions preferable to the users like example changing user profile, changing settings of the application, etc. In this article, it has been discussed step by step to implement the navigation drawer in android.

The navigation drawer slides in from the left and contains the navigation destinations for the app.

The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout. Have a look at the following image to get an idea about the Navigation drawer.

Navigation Drawer in Android



Steps to Implement Navigation Drawer in Android

Step 1: Create an Android Studio Project

  • Create an empty activity android studio project.
  • Refer to Android | How to Create/Start a New Project in Android Studio? on how to create an empty activity android studio project. Note that we are going to implement this project using theJava language.

Step 2: Adding a dependency to the project

  • In this discussion, we are going to use the Material design Navigation drawer.
  • So add the following Material design dependency to the app-level Gradle file.

implementation 'com.google.android.material:material:1.3.0-alpha03'

  • Refer to the following image if unable to locate the app-level Gradle file invokes the dependency (under project hierarchy view).
  • After invoking the dependency click on the " Sync Now " button. Make sure the system is connected to the network so that Android Studio downloads the required files.

Step 3: Creating a menu in the menu folder

  • Create the menu folder under the res folder. To implement the menu.
  • Refer to the following video to create the layout to implement the menu.

  • Invoke the following code in the navigation_menu.xml

XML

<? xml version = "1.0" encoding = "utf-8" ?>

tools:ignore = "HardcodedText" >

< item

android:id = "@+id/nav_account"

android:title = "My Account" />

< item

android:id = "@+id/nav_settings"

android:title = "Settings" />

< item

android:id = "@+id/nav_logout"

android:title = "Logout" />

</ menu >


Step 4: Working with the activity_main.xml file

  • Invoke the following code in the activity_main.xml to set up the basic things required for the Navigation Drawer.

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< androidx.drawerlayout.widget.DrawerLayout

android:id = "@+id/my_drawer_layout"

android:layout_width = "match_parent"

android:layout_height = "match_parent"

tools:context = ".MainActivity"

tools:ignore = "HardcodedText" >

< LinearLayout

android:layout_width = "match_parent"

android:layout_height = "match_parent" >

< TextView

android:layout_width = "match_parent"

android:layout_height = "wrap_content"

android:layout_marginTop = "128dp"

android:gravity = "center"

android:text = "GeeksforGeeks"

android:textSize = "18sp" />

</ LinearLayout >

< com.google.android.material.navigation.NavigationView

android:layout_width = "wrap_content"

android:layout_height = "match_parent"

android:layout_gravity = "start"

app:menu = "@menu/navigation_menu" />

</ androidx.drawerlayout.widget.DrawerLayout >


Output UI:

  • One thing to be noticed is that the menu drawer icon is still not appeared on the action bar. We need to set the icon and its open-close functionality programmatically.

Step 5: Include the Open Close strings in the string.xml

  • Invoke the following code in the styles.xml file.

XML

< resources >

< string name = "app_name" >Navigation Drawer</ string >

< string name = "nav_open" >Open</ string >

< string name = "nav_close" >Close</ string >

</ resources >

Step 6: Working with the MainActivity.java file

  • Invoke the following code in the MainActivity.java file to show the menu icon on the action bar and implement the open-close functionality of the navigation drawer.
  • Comments are added inside the code for better understanding.

Java

import androidx.annotation.NonNull;

import androidx.appcompat.app.ActionBarDrawerToggle;

import androidx.appcompat.app.AppCompatActivity;

import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;

import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

public DrawerLayout drawerLayout;

public ActionBarDrawerToggle actionBarDrawerToggle;

@Override

protected void onCreate(Bundle savedInstanceState) {

super .onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

drawerLayout = findViewById(R.id.my_drawer_layout);

actionBarDrawerToggle = new ActionBarDrawerToggle( this , drawerLayout, R.string.nav_open, R.string.nav_close);

drawerLayout.addDrawerListener(actionBarDrawerToggle);

actionBarDrawerToggle.syncState();

getSupportActionBar().setDisplayHomeAsUpEnabled( true );

}

@Override

public boolean onOptionsItemSelected( @NonNull MenuItem item) {

if (actionBarDrawerToggle.onOptionsItemSelected(item)) {

return true ;

}

return super .onOptionsItemSelected(item);

}

}

Output: Run on Emulator


What Is an App Drawer on Android

Source: https://www.geeksforgeeks.org/navigation-drawer-in-android/

Komentar

More Articles

164.68 L27 15 : Kumpulan 111.90.l50.204 Japanese 164.68.l27.15 111.90150 ... - Anda akan melihan berbagai macam fitur di sajikan oleh situs tersebut, dan bukan hanya satu atau dua yang di sajikan pada halan situs.

On a Table if I Create a Line Does Will It Continue a Forumula

حاسبة جميلة - الة حاسبة جميلة وأنيقة : كلام جميل فيس بوك , وصور بوستات فيس بوك جميلة.

111.90 L.150.204 Viral / 111 90 L 150 204 Viral Variante Virus Covid Variante Virus Puo Essere Anche Apa Bila Anda Akan Melihat Sebuah Kumpulan Video Nada Tentu Bisa Menggunakan 111 90 150 204 Nonton Karena Banyak

Andai Kakak Ku Tidak Menggodaku Waktu Itu : Andai Saja Kakak Tidak Menggodaku : Jika kalian penasaran dengan apa yang terjadi pada hal yang viral ini, maka kalian harus simak pembahasan yang akan admin bagikan sampai akhir ya sob.

Локомотив Крылья Советов : Локомотив — Крылья Советов. Прогноз и ставка за 1.75 / 02 ... - Результаты матча, счет 3:1, обзор по минутам, видео.

Jack Grealish - Villa Star Jack Grealish Reveals He 'Almost Died' From ... - Jack grealish (born 10 september 1995) is a professional footballer who plays for premier league club aston villa as a midfielder.

How To Make Home Made Pork Rinds : How to Make Chicharrones (Fried Pork Skin) at Home | Fried ... : How to make pork skins.

New Cabinet Ministers - How many of the new Cabinet Ministers can you name?

Gambar Sketsa Tatto : Inspirasi Tato Sketsa Tato Joker Keren / 20 gambar sketsa kumpulan gambar sketsa bunga sketsa tato, polres karawang membuat sketsa tato simple sumber.




banner