AutoSkill Android Navigation Drawer Implementation
Implement a reusable navigation drawer (side menu) in Android activities, ensuring the ActionBar toggle opens the menu and navigation items trigger the correct actions.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/android-navigation-drawer-implementation" ~/.claude/skills/ecnu-icalk-autoskill-android-navigation-drawer-implementation && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8/android-navigation-drawer-implementation/SKILL.mdsource content
Android Navigation Drawer Implementation
Implement a reusable navigation drawer (side menu) in Android activities, ensuring the ActionBar toggle opens the menu and navigation items trigger the correct actions.
Prompt
Role & Objective
Act as an Android Developer. Implement a navigation drawer (side menu) in an Android Activity class using
DrawerLayout, NavigationView, and ActionBarDrawerToggle.
Operational Rules & Constraints
- Variables: Declare
,DrawerLayout drawerLayout
, andNavigationView navigationView
as class fields.ActionBarDrawerToggle drawerToggle - Layout: Ensure the XML layout contains a
with idDrawerLayout
and adrawer_layout
with idNavigationView
.nav_view - Initialization: In
, callonCreate
with the correct layout and then call a configuration method (e.g.,setContentView
).menuConfig - Configuration Method:
- Initialize views using
.findViewById - Instantiate
.ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close) - Add the toggle as a drawer listener:
.drawerLayout.addDrawerListener(drawerToggle) - Sync the toggle state:
.drawerToggle.syncState() - Enable the home button on the ActionBar:
.getSupportActionBar().setDisplayHomeAsUpEnabled(true) - Set a
on theNavigationItemSelectedListener
.NavigationView - Inside the listener, check item IDs (e.g.,
,R.id.home
) and perform actions (e.g., show Toast, start Intent).R.id.leaderboard - Close the drawer after selection:
.drawerLayout.closeDrawer(GravityCompat.START)
- Initialize views using
- Option Handling: Override
. CheckonOptionsItemSelected
first. If it returns true, return true. Otherwise, calldrawerToggle.onOptionsItemSelected(item)
.super.onOptionsItemSelected(item)
Anti-Patterns
- Do not override
to callonSupportNavigateUp
when using a drawer toggle, as this prevents the drawer from opening and causes the button to act as a back button.onBackPressed - Do not call
separately if it conflicts with the drawer toggle setup.setHomeButtonEnabled(true) - Ensure
is called afterfindViewById
to avoidsetContentView
.NullPointerException
Triggers
- How can I use this side menu in another class
- Implement navigation drawer in activity
- Menu button goes back instead of opening drawer
- Setup ActionBarDrawerToggle
- Copy menu configuration to new activity