Android Studio Hedgehog | 2023.1.1
Note: For personal reference using DrawerLayout, Toolbar & NavigationView in java
Create new Project (No Activity)
Name TutorDrawerMenu
Package Name com.dedetok.tutordrawermenu
Create Activity -> Empty View Activity
Layout Name activity_main
Laucher Activity checked
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<!-- For apps targeting Android 13 or higher & GMA SDK version 20.3.0 or lower -->
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- apply android:theme="@style/Theme.AppCompat.Light.NoActionBar" at AndroidManifest.xml -->
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
tools:targetApi="31"
>
<!-- android:theme="@style/Theme.AppCompat.Light.NoActionBar" setSupportActionBar(myToolbar); -->
<activity
...
</activity>
<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
<!-- AD App ID: ca-app-pub-0220748109202708~3978959687 -> AndroidManifest.xml -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-0220748109202708~3978959687"/>
</application>
</manifest>
build.gradle.kts (Module: app)
dependencies {
...
implementation("com.google.android.gms:play-services-ads-lite:22.6.0")
...
Resource -> Values -> strings.xml
<resources>
<string name="app_name">Tutorial Drawer Menu</string>
<string name="nav_open">Open</string>
<string name="nav_close">Close</string>
</resources>
Resource -> Menu -> my_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item_1" android:title="Item 1" />
<item android:id="@+id/item_2" android:title="Item 2" />
</menu>
Layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- androidx.drawerlayout.widget.DrawerLayout must be put on top of main layout -->
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:id="@+id/my_drawerlayout"
tools:openDrawer="start"
android:fitsSystemWindows="true"
>
<!-- must have layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- put androidx.appcompat.widget.Toolbar at the first of layout -->
<androidx.appcompat.widget.Toolbar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_toolbar"
/>
<!-- PUT CONTENT HERE -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
/>
<!-- EXAMPLE FOR androidx.fragment.app.FragmentContainerView add
android:layout_weight="1" and android:gravity="top|left" -->
<!-- lock adview position at bottom of layout -->
<!-- Sample AdMob unit ID: ca-app-pub-3940256099942544/6300978111 -->
<!-- AD Unit ID: ca-app-pub-0220748109202708/9185950681 -->
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-0220748109202708/9185950681"
android:layout_gravity="end|center"
/>
</LinearLayout>
<!-- com.google.android.material.navigation.NavigationView must be put at the end of main layout -->
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_navigationview"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/my_menu"
/>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity.java
package com.dedetok.tutordrawermenu;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
DrawerLayout myDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar); // android:theme="@style/Theme.AppCompat.Light.NoActionBar"
myDrawerLayout = findViewById(R.id.my_drawerlayout);
ActionBarDrawerToggle myActionBarDrawerToggle = new ActionBarDrawerToggle(this, myDrawerLayout, myToolbar, R.string.nav_open, R.string.nav_close);
myDrawerLayout.addDrawerListener(myActionBarDrawerToggle);
myActionBarDrawerToggle.syncState();
NavigationView myNavigationView = findViewById(R.id.my_navigationview);
myNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Log.e("dedetok", item.getTitle().toString());
int idItem = item.getItemId();
if (idItem==R.id.item_1) {
// Do something
} else if (idItem==R.id.item_2) {
// Do something
}
if (myDrawerLayout.isDrawerOpen(GravityCompat.START)) {
myDrawerLayout.close();
}
return false;
}
});
}
}
NOTE: Some code may reference to other source code in comment (some has modified to make it run), do not remove urls if you wish to copy paste code and use the code.
No comments:
Post a Comment