Reference: Griffiths and Griffiths, Head First Android Development, O'Reilly, 2021, pp. 219 to 292.
| View/Widget | Namespace | Attribute | Value |
|---|---|---|---|
| LinearLayout | android | layout_width | match_parent |
| LinearLayout | android | layout_height | match_parent |
| LinearLayout | android | orientation | vertical |
| LinearLayout | android | gravity | center_horizontal |
| LinearLayout | tools | context | .WelcomeFragment |
| TextView | android | layout_width | match_parent |
| TextView | android | layout_height | wrap_content |
| TextView | android | text | Welcome to the Secret Message\n app! Use this app to encrypt a\n secret message. Click on the\n Start button to begin. |
| TextView | android | layout_marginTop | 32dp |
| TextView | android | textSize | 20sp |
| Button | android | id | @+id/btn_start |
| Button | android | layout_width | wrap_content |
| Button | android | layout_height | wrap_content |
| Button | android | layout_marginTop | 32dp |
| Button | android | text | Start |
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for WelcomeFragment
val view = inflater.inflate(R.layout.fragment_welcome,
container, false)
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
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/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="it372.secretmessage.WelcomeFragment" />
Also set the padding to 16dp and set the context:tools:context=".MainActivity"
| View/Widget | Namespace | Attribute | Value |
|---|---|---|---|
| LinearLayout | android | layout_width | match_parent |
| LinearLayout | android | layout_height | match_parent |
| LinearLayout | android | orientation | vertical |
| LinearLayout | android | padding | 16dp |
| LinearLayout | android | gravity | center_vertical |
| LinearLayout | tools | context | .MessageFragment |
| EditText | android | id | @+id/edt_message |
| EditText | android | layout_width | match_parent |
| EditText | android | layout_height | wrap_content |
| EditText | android | textSize | 20sp |
| EditText | android | hint | Please enter secret message. |
| EditText | android | inputType | textMultiLine |
| Button | android | id | @+id/next |
| Button | android | layout_width | wrap_content |
| Button | android | layout_height | wrap_content |
| Button | android | layout_marginTop | 32dp |
| Button | android | text | Next |
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for WelcomeFragment
val view = inflater.inflate(R.layout.fragment_welcome,
container, false)
}
ext.nav_version = "2.4.1"to the buildscript section at the beginning of the Gradle project files:
buildscript {
ext.nav_version = "2.4.1"
}
In the module Gradle module (app) file,
in the dependencies section change the lineimplementation "androidx.navigation:navigation-fragment-ktx:2.4.1"to
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"Now click the Sync Project with Gradle Files button (
).
This downloads and adds the Navigation component.
)
and select fragment_welcome. The WelcomeFragment layout
should appear in the nav_graph.xml Design Window.
<?xml version="1.0" encoding="utf-8"?>
<navigation
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/nav_graph"
app:startDestination="@id/welcomeFragment">
<fragment
android:id="@+id/welcomeFragment"
android:name="it372.secretmessage2.WelcomeFragment"
android:label="fragment_welcome"
tools:layout="@layout/fragment_welcome" >
<action
android:id="@+id/action_welcomeFragment_to_messageFragment"
app:destination="@id/messageFragment" />
</fragment>
<fragment
android:id="@+id/messageFragment"
android:name="it372.secretmessage2.MessageFragment"
android:label="fragment_message"
tools:layout="@layout/fragment_message" />
</navigation>
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
tools:context=".MainActivity" />
// WelcomeFragment is a derived class of Fragment.
class WelcomeFragment : Fragment( ) {
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_welcome,
container, false)
// Create Button object and add OnClickListener to button.
val startButton = view.findViewById<Button>(R.id.btn_start)
startButton.setOnClickListener {
// Code that runs when the button is clicked
view.findNavController( ).
navigate(R.id.action_welcomeFragment_to_messageFragment)
}
return view
}
| View/Widget | Namespace | Attribute | Value |
|---|---|---|---|
| LinearLayout | android | layout_width | match_parent |
| LinearLayout | android | layout_height | match_parent |
| LinearLayout | android | orientation | vertical |
| LinearLayout | android | padding | 16dp |
| LinearLayout | android | gravity | center_vertical |
| LinearLayout | tools | context | .MessageFragment |
| TextView | android | layout_width | match_parent |
| TextView | android | layout_height | wrap_content |
| TextView | android | layout_marginTop | 20sp |
| TextView | android | textSize | 20sp |
| TextView | android | text | Here is your encrypted message. |
| TextView | android | inputType | textMultiLine |
| TextView | android | id | @+id/txt_encrypted_message |
| TextView | android | layout_width | match_parent |
| TextView | android | layout_height | wrap_content |
| TextView | android | layout_marginTop | 20dp |
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for WelcomeFragment
val view = inflater.inflate(R.layout.fragment_welcome,
container, false)
}
<?xml version="1.0" encoding="utf-8"?>
<navigation
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/nav_graph"
app:startDestination="@id/welcomeFragment">
<fragment
android:id="@+id/welcomeFragment"
android:name="it372.secretmessage2.WelcomeFragment"
android:label="fragment_welcome"
tools:layout="@layout/fragment_welcome">
<action
android:id="@+id/action_welcomeFragment_to_messageFragment"
app:destination="@id/messageFragment" />
</fragment>
<fragment
android:id="@+id/messageFragment"
android:name="it372.secretmessage2.MessageFragment"
android:label="fragment_message"
tools:layout="@layout/fragment_message">
<action
android:id="@+id/action_messageFragment_to_encryptFragment"
app:destination="@id/encryptFragment" />
</fragment>
<fragment
android:id="@+id/encryptFragment"
android:name="it372.secretmessage2.EncryptFragment"
android:label="fragment_encrypt"
tools:layout="@layout/fragment_encrypt" />
</navigation>
// SecretMessage Example
// Source code file: MessageFragment.kt
package it372.secretmessage2
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.navigation.findNavController
class MessageFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_message,
container, false)
val nextButton = view.findViewById<Button>(R.id.btn_next);
nextButton.setOnClickListener {
view.findNavController( ).
navigate(R.id.action_messageFragment_to_encryptFragment)
}
return view
}
}
buildscript {
ext.nav_version = "2.4.2"
dependencies {
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
}
}
Add this line to the plugins section of the module build.gradle file:
plugins {
id 'android:navigation.safeargs'
}
<argument
android:name="message"
app:argType="string">
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText;
import androidx.navigation.findNavController
class MessageFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_message,
container, false)
val nextButton = view.findViewById<Button>(R.id.btn_next)
val messageView = view.findViewById<EditText>(R.id.edt_message)
nextButton.setOnClickListener {
val message = messageView.text.toString( )
val action = MessageFragmentDirections.
actionMessageFragmentToEncryptFragment(message)
view.findNavController( ).navigate(action)
}
return view
}
package it372.secretmessage2
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
class EncryptFragment : Fragment( ) {
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_encrypt,
container, false)
val message = EncryptFragmentArgs.fromBundle(
requireArguments( )).message
val encryptedView = view.findViewById<TextView>(
R.id.txt_encrypted_message)
encryptedView.text = message.reversed( )
return view;
}
}