AdvNative Week 2 - Navigation
AdvNative Week 2 - Navigation
WEEK 2
The Navigation component is designed for apps that have one main
activity with multiple fragment destinations.
These graphs are typically inflated from an Android resource, but, like
views, they can also be constructed or combined programmatically
or for the case of dynamic navigation structure.
WHY NAVIGATION?
Some additional benefits:
• Fragment transactions are handled automatically
• Back and Up buttons are handled correctly
• Adding default animations and transitions are trivial
• Implements Material Design navigation UI pattern (bottom nav,
drawers) with ease
• Navigation can be visualized in Android Studio, allowing easy
understanding and editing
• Passing information is easy and safe with help of SafeArgs plugin
TUTORIAL
1. Create new project, name it
as “AdvNRPWeek2”
Ex. Adv12345Week2
textInputLayout
● Hint = “Player Name”
● hintAnimationEnabled = true
● hintEnabled = true
STEP 2: LAYOUT
Open fragment_game.xml, add a ConstraintLayout and arrange UI
like image below
btnBack
● Text = “back”
● Icon = use vector assets icon back
arrow
STEP 3: NAVIGATION GRAPH
Navigation graph is a resource file. XML based, that represent how
fragment navigate to other destination.
plugins {
id 'com.android.application' Sadly, this handy feature is
id 'kotlin-android' now deprecated…
id 'kotlin-android-extensions'
}
This config enables the androidx feature to refer a view object from
kotlin codes by its ID. Without this, you need to use findViewById()
mechanism
INSTALLING NAVIGATION JETPACK
Still in same gradle file, locate dependencies, then add these (use the
autocomplete to avoid errors):
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
INSTALLING NAVIGATION SAFEARGS
Now open build.gradle (Project) file. At the topmost line (after the
comment, but before plugins block), copy this block (get it from here).
Don't forget to “sync now”
buildscript {
repositories {
google()
}
dependencies {
val nav_version = "2.7.1"
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
}
}
plugins { . . . }
INSTALLING NAVIGATION SAFEARGS
Go back to the other build.gradle file (the module one). Add this plugin:
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("androidx.navigation.safeargs.kotlin")
}
Sync your Gradle (make sure your machine is connected to the Internet)
SAFEARGS
SafeArgs is a gradle plugin that allows you to enter information into the
navigation graph about the arguments that you want to pass.
It then generates code for you that handles the tedious bits of creating a
Bundle for those arguments and pulling those arguments out of the
Bundle on the other side.
STEP 3: NAVIGATION GRAPH
Now we need to create navigation graph to
define navigation in the app.
Click on mainFragment
change label property to
“Main Screen”
IMPORTANT!
Every time you make a change on
the navigation graph, you might
need to rebuild the project (build
> rebuild project)
Next step to set up the default “soft” android back button that is
usually located on the top left corner of the app.
This back button is used to access the previous item on the back
stack.
STEP 6: ANDROID BACK BUTTON
Open MainActivity.kt, create navController variable
KotlinDevelopment.com
STEP 6: ANDROID BACK BUTTON
Open MainActivity.kt, create navController variable
And then setup the action bar with soft button functionality
STEP 6: ANDROID BACK BUTTON
Next setup the action of this soft button
btnStart.setOnClickListener {
val txtName = view.findViewById<TextView>(R.id.txtName)
val playerName = txtName.text.toString()
val action = MainFragmentDirections.actionGameFragment(playerName)
Navigation.findNavController(it).navigate(action)
}
Retrieve the player name data If you have multiple arguments, you may use
from EditText, and then use comma in action fragment constructor. Ie.
actionGameFragment(playerName, playerScore)
the action
STEP 7b: CATCH ARGUMENTS
On the game frament side, we can receive the argument. Write following
codes
The Logic
Game will randomize two number. Player’s
need to solve the addition equation.
Create ResultFragment
The Layout
Use image on the left as references
The Logic
Display player score in “Your score” textview
Good luck
BONUS: MATERIAL DESIGN 3
We can apply material design 3, a latest design system that helps you
crafting beautiful and useful UI.
Next, we will use available template for app colors and theme. Open this
website: https://m3.material.io/theme-builder#/custom
BONUS: MATERIAL DESIGN 3
Play around with Primary, Secondary, Tertiary, and Neutral color. Next,
click the export button on top right, choose Android Views (XML) to
download the zip files that contains:
Colors.xml
Themes.xml
Themes.xml (night)
BONUS: MATERIAL DESIGN 3
Open the colors.xml, and copy paste all resources color into the res >
values > color.xml
BONUS: MATERIAL DESIGN 3
Open the theme.xml, and copy paste only all the items tag into the res >
values > themes > themes.xml
Open the theme.xml (night), and copy paste only all the items tag into
the res > values > themes > themes.xml (night)
Make sure all Buttons already use Material3 style. Rebuild and run the
project.
THANKS!
DO YOU HAVE ANY QUESTION?