0% found this document useful (0 votes)
18 views7 pages

Mad Assignment 8

Uploaded by

swkqmfzf8r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Mad Assignment 8

Uploaded by

swkqmfzf8r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MAD ASSIGNMENT 08

Name:Aniket Joshi

Roll No:09

DIV: N

Prn: 12310510

.Implement an application that makes use of navigation drawer.

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

<!-- the root view must be the DrawerLayout -->

<androidx.drawerlayout.widget.DrawerLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

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="Navigation Drawer"

android:textSize="18sp" />

</LinearLayout>

<!-- this the navigation view which draws and shows the navigation
drawer -->

<!-- include the menu created in the menu folder -->

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

android:layout_width="wrap_content"

android:layout_height="match_parent"
android:layout_gravity="start"

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

</androidx.drawerlayout.widget.DrawerLayout>

MainActivity.java
package com.example.assignment_8;

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;

public ActionBarDrawerToggle;
@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// drawer layout instance to toggle the menu icon to open drawer and back
button to close drawer

drawerLayout = findViewById(R.id.my_drawer_layout);

actionBarDrawerToggle = new ActionBarDrawerToggle(this,


drawerLayout, R.string.nav_open, R.string.nav_close);

// pass the Open and Close toggle for the drawer layout
listener to toggle the button

drawerLayout.addDrawerListener(actionBarDrawerToggle);

actionBarDrawerToggle.syncState();

// to make the Navigation drawer icon always appear on the


action bar

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

} // override the onOptionsItemSelected() function to implement


the item click listener callback to open and close the navigation drawer
when the icon is clicked

@Override

public boolean onOptionsItemSelected(@NonNull MenuItem item) {

if (actionBarDrawerToggle.onOptionsItemSelected(item)) {

return true;
}

return super.onOptionsItemSelected(item);

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

<menu xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

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>

strings.xml
<resources>

<string name="app_name">Assignment_8</string>

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

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

</resources>

OUTPUT:

You might also like