Skip to content

Commit 11111fb

Browse files
Merge pull request #40 from D4rK7355608/codex/add-more-navigation-lessons
Add Navigation Drawer lesson
2 parents b110d50 + 37bcdc1 commit 11111fb

File tree

9 files changed

+163
-0
lines changed

9 files changed

+163
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@
316316
android:exported="false"
317317
android:label="@string/bottom_navigation"
318318
android:parentActivityName=".ui.screens.android.lessons.navigation.bottomnavigation.BottomNavigationActivity" />
319+
<activity
320+
android:name=".ui.screens.android.lessons.navigation.drawer.NavigationDrawerActivity"
321+
android:exported="false"
322+
android:label="@string/navigation_drawer"
323+
android:parentActivityName=".ui.screens.android.lessons.navigation.drawer.NavigationDrawerActivity" />
319324

320325
<service
321326
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.d4rk.androidtutorials.java.ui.screens.android.lessons.navigation.drawer;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
import android.os.Handler;
6+
import android.view.Gravity;
7+
8+
import androidx.appcompat.app.AppCompatActivity;
9+
10+
import com.d4rk.androidtutorials.java.R;
11+
import com.d4rk.androidtutorials.java.databinding.ActivityNavigationDrawerBinding;
12+
import com.d4rk.androidtutorials.java.ui.screens.android.CodeActivity;
13+
import com.d4rk.androidtutorials.java.utils.EdgeToEdgeDelegate;
14+
15+
public class NavigationDrawerActivity extends AppCompatActivity {
16+
private final Handler handler = new Handler();
17+
private ActivityNavigationDrawerBinding binding;
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
binding = ActivityNavigationDrawerBinding.inflate(getLayoutInflater());
23+
setContentView(binding.getRoot());
24+
25+
EdgeToEdgeDelegate edgeToEdgeDelegate = new EdgeToEdgeDelegate(this);
26+
edgeToEdgeDelegate.applyEdgeToEdge(binding.container);
27+
28+
binding.navigationView.setNavigationItemSelectedListener(item -> {
29+
binding.textView.setText(getString(R.string.selected) + " " + item.getTitle());
30+
binding.drawerLayout.closeDrawer(Gravity.START);
31+
return true;
32+
});
33+
34+
binding.floatingButtonShowSyntax.setOnClickListener(v -> {
35+
Intent intent = new Intent(this, CodeActivity.class);
36+
intent.putExtra("lesson_name", "NavigationDrawer");
37+
startActivity(intent);
38+
});
39+
40+
handler.postDelayed(() -> binding.floatingButtonShowSyntax.shrink(), 5000);
41+
}
42+
}

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/android/repository/LessonRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public Lesson getLesson(String lessonName) {
4242
new Lesson(R.string.web_view, R.raw.text_webview_java, R.raw.text_webview_xml);
4343
case "BottomNavigation" ->
4444
new Lesson(R.string.bottom_navigation, R.raw.text_bottom_navigation_java, R.raw.text_bottom_navigation_xml);
45+
case "NavigationDrawer" ->
46+
new Lesson(R.string.navigation_drawer, R.raw.text_navigation_drawer_java, R.raw.text_navigation_drawer_xml);
4547
default -> throw new IllegalArgumentException("Unknown lesson: " + lessonName);
4648
};
4749
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:id="@+id/drawer_layout"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<androidx.constraintlayout.widget.ConstraintLayout
9+
android:id="@+id/container"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent">
12+
13+
<com.google.android.material.textview.MaterialTextView
14+
android:id="@+id/text_view"
15+
style="@style/TextAppearance.Material3.HeadlineSmall"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:text="@string/home"
19+
app:layout_constraintTop_toTopOf="parent"
20+
app:layout_constraintStart_toStartOf="parent"
21+
app:layout_constraintEnd_toEndOf="parent"
22+
app:layout_constraintBottom_toBottomOf="parent" />
23+
24+
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
25+
android:id="@+id/floating_button_show_syntax"
26+
style="@style/Widget.Material3.ExtendedFloatingActionButton.Icon.Surface"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:layout_margin="24dp"
30+
android:contentDescription="@string/tooltip_show_code"
31+
android:text="@string/show_code"
32+
android:tooltipText="@string/tooltip_show_code"
33+
app:icon="@drawable/ic_code"
34+
app:layout_constraintBottom_toBottomOf="parent"
35+
app:layout_constraintEnd_toEndOf="parent" />
36+
37+
</androidx.constraintlayout.widget.ConstraintLayout>
38+
39+
<com.google.android.material.navigation.NavigationView
40+
android:id="@+id/navigation_view"
41+
android:layout_width="wrap_content"
42+
android:layout_height="match_parent"
43+
android:layout_gravity="start"
44+
app:menu="@menu/drawer_lesson_menu" />
45+
46+
</androidx.drawerlayout.widget.DrawerLayout>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item
4+
android:id="@+id/navigation_home"
5+
android:icon="@drawable/ic_home"
6+
android:title="@string/home" />
7+
<item
8+
android:id="@+id/navigation_dashboard"
9+
android:icon="@drawable/ic_android"
10+
android:title="@string/dashboard" />
11+
<item
12+
android:id="@+id/navigation_notifications"
13+
android:icon="@drawable/ic_notification_important"
14+
android:title="@string/notifications" />
15+
</menu>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Import the necessary classes and libraries
2+
import android.os.Bundle;
3+
import androidx.appcompat.app.AppCompatActivity;
4+
import androidx.drawerlayout.widget.DrawerLayout;
5+
import com.google.android.material.navigation.NavigationView;
6+
7+
public class MainActivity extends AppCompatActivity {
8+
private ActivityMainBinding binding;
9+
10+
@Override
11+
protected void onCreate(Bundle savedInstanceState) {
12+
super.onCreate(savedInstanceState);
13+
14+
// Inflate the layout and set it as the content view for this activity
15+
binding = ActivityMainBinding.inflate(getLayoutInflater());
16+
setContentView(binding.getRoot());
17+
18+
// Update the text when a navigation item is selected
19+
binding.navigationView.setNavigationItemSelectedListener(item -> {
20+
binding.textView.setText(item.getTitle());
21+
binding.drawerLayout.closeDrawer(Gravity.START);
22+
return true;
23+
});
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<androidx.drawerlayout.widget.DrawerLayout
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
<TextView
7+
android:id="@+id/text_view"
8+
android:layout_width="wrap_content"
9+
android:layout_height="wrap_content"
10+
android:text="@string/home" />
11+
<com.google.android.material.navigation.NavigationView
12+
android:id="@+id/navigation_view"
13+
android:layout_width="wrap_content"
14+
android:layout_height="match_parent"
15+
android:layout_gravity="start"
16+
app:menu="@menu/drawer_lesson_menu" />
17+
</androidx.drawerlayout.widget.DrawerLayout>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
<string name="unlabeled">Unlabeled</string>
245245
<string name="navigation">Navigation</string>
246246
<string name="bottom_navigation">Bottom Navigation</string>
247+
<string name="navigation_drawer">Navigation Drawer</string>
247248
<string name="dashboard">Dashboard</string>
248249
<string name="code_font">Code font</string>
249250
<string name="language">Language</string>
@@ -360,6 +361,7 @@
360361
<string name="summary_preference_android_studio_simple_notifications">Learn how to use notifications in your Android app with this lesson. Discover how to create a notification channel and builder, and how to show notifications to your users. Explore the different options available for customizing your notifications.</string>
361362
<string name="summary_preference_android_studio_inbox_notifications">Learn how to use inbox style notifications in your Android app with this lesson. Discover how to create a notification channel and builder, and how to set the style of your notifications to an InboxStyle with multiple lines of text and a summary text. Explore the different options available for customizing your inbox style notifications.</string>
362363
<string name="summary_preference_android_studio_bottom_navigation">A bottom navigation bar lets you quickly switch between top-level views in your app.</string>
364+
<string name="summary_preference_android_studio_navigation_drawer">A navigation drawer slides in from the side and displays the app's main navigation options.</string>
363365
<string name="summary_alert_dialog_close">Are you sure you want to exit?</string>
364366
<string name="summary_alert_dialog_message">This will be the message you will see on screen!</string>
365367
<string name="summary_alert_dialog_require_restart">To take effect please restart the app!</string>

app/src/main/res/xml/preferences_android_studio.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,14 @@
346346
android:targetClass="com.d4rk.androidtutorials.java.ui.screens.android.lessons.navigation.bottomnavigation.BottomNavigationActivity"
347347
android:targetPackage="com.d4rk.androidtutorials.java" />
348348
</androidx.preference.Preference>
349+
<androidx.preference.Preference
350+
app:icon="@drawable/ic_menu"
351+
app:summary="@string/summary_preference_android_studio_navigation_drawer"
352+
app:title="@string/navigation_drawer">
353+
<intent
354+
android:action="android.intent.action.VIEW"
355+
android:targetClass="com.d4rk.androidtutorials.java.ui.screens.android.lessons.navigation.drawer.NavigationDrawerActivity"
356+
android:targetPackage="com.d4rk.androidtutorials.java" />
357+
</androidx.preference.Preference>
349358
</androidx.preference.PreferenceCategory>
350359
</androidx.preference.PreferenceScreen>

0 commit comments

Comments
 (0)