In this lab, you will learn how to use Fragments and create Navigation in your apps.
Remember:Create an empty activity project.
The goal is to create two fragments. In the first fragment you should have an EditText, a SeekBar and a Button. In the second fragment a TextView. Follow the tutorial from the book in chapter 30.1-30.5 to create two fragments (this assumes you have read Chapter 29). In 30.2 you do NOT need to use ebookfrenzy.com as the Company Domain (use whatever you want), but make sure your own package path on the top of your classes. Use 21 as the minimum Android sdk.
Read 30.7 up until page 318 (skip 30.6) so you can understand the listeners for the Seekbar.
we will follow the steps provided in the Getting Started Guide you had to read for today (guide) but we will adapt it to create our content.
Add the dependencies in your build.gridle Module (see the guide).
Create a navigation graph following the guide or the class video. Create a new destination, when it asks you for a fragment add the ToolbarFragment as your starting destination.
Go back to your main_activity.xml and add a FragmentContainerView - see the guide on "Adding a NavHostFragment in XML".
Using the navigation graph create a new destination, add here the TextFragment. See the guide in "Add destinations to the navigation graph", "Designate a screen as the start destination".
Connect your two fragments in the navigation graph, following the guide on "Connect destinations". Notice the name of your action that connects the two fragments in the navigation graph (mine is called action_toolbarFragment_to_textFragment).
In your fragments, in their onCreate method, instead of returning the View, keep it in a variable so you can edit the content and return it at the end.
In the first fragment add a listener to your Button. Inside it call Navigation.findNavController(view).navigate(R.id.action_toolbarFragment_to_textFragment) - you can now move between fragments !!!
Add the required declarations in build.gridle as suggested in the guide "Ensure type-safety by using Safe Args".
In your button listener on the ToolbarFragment create a Bundle (let's call it bun). You can use the methods putString and putInt to store the text and size of the font.
You can pass the bundle to the TextFragment throuth the Navigation Navigation.findNavController(view).navigate(R.id.action_toolbarFragment_to_textFragment, bun)
Use what you know from past lessons to read the bundle in the TextFragment on onCreate(). Android studio has already some placeholders for you - see the method getArguments() that helps you read the bundle.
In the TextFragment you can use the TextEdit methods setTextSize() and setTextSize() to change the size of your text based on the SeekBar from the ToolbarFragment.
Create a new project that is a BasicActivity, a BottomNavigation or a NavigationDrawer project and look at their structure. You should be able to see Bindings instead of "findViewBy" and a navigation model. In some you may see Fragments and their ViewModels.
Optional: Try to recreate some of your past exercises to one of these activities, such as the Image viewer app from Lab3-Ex3.
Optional: If you want to go even further and look at how ViewModels work, here is a starting point: ViewModels
Follow the tutorial from the book in chapter 30 (this assumes you have read Chapter 29). Run the program with each addition to see how your interfaces changes. See the notes bellow on issues with the tutorial.
Notes on issues in the book tutorial:
Dans ce laboratoire, vous apprendrez à utiliser Fragments et Navigation.
Rappel :Créez un projet d'activité vide (Empty Activity).
Le but est de créer deux fragments, dans le premier fragment, vous devriez avoir un EditText, une SeekBar et un bouton. Dans le second fragment, un TextView. Suivez le tutoriel du livre au chapitre 30.1-30.5 pour créer deux fragments (cela suppose que vous ayez lu le chapitre 29). Dans 30.2, vous n'avez PAS besoin d'utiliser ebookfrenzy.com comme domaine de la société (utilisez ce que vous voulez), mais assurez-vous que votre propre chemin de package dans vos classes. Utilisez 21 comme sdk Android minimum.
Lisez 30.7 jusqu'à la page 318 (sautez 30.6) afin de comprendre les écouteurs (listeners) pour la Seekbar.
Nous allons suivre les étapes fournies dans le Starting Guide que vous avez dû lire pour aujourd'hui (guide) mais nous allons l'adapter pour créer notre contenu.
Ajoutez les dépendances dans votre module build.gridle (voir le guide).
Créez un navigation graph en suivant le guide ou le video. Créez une nouvelle destination, quand il vous demande un fragment, ajoutez le ToolbarFragment comme Start Destination.
Retournez dans votre main_activity.xml et ajoutez un FragmentContainerView - voir le guide sur "Adding a NavHostFragment in XML".
En utilisant le navigation graph, créez une nouvelle destination, ajoutez ici le TextFragment. Voir le guide dans "Add destinations to the navigation graph", "Designate a screen as the start destination".
Connectez vos deux fragments dans le graphe de navigation, en suivant le guide dans "Connect destinations". Remarquez le nom de votre action qui relie les deux fragments dans le graphe de navigation (la mienne dans le video s'appelle action_toolbarFragment_to_textFragment).
Dans vos fragments, dans leur méthode onCreate, au lieu de renvoyer la View, gardez-la dans une variable pour pouvoir modifier le contenu et la renvoyer à la fin.
Dans le premier fragment, ajoutez un écouteur à votre bouton. À l'intérieur de celui-ci, appelez Navigation.findNavController(view).navigate(R.id.action_toolbarFragment_to_textFragment) - vous pouvez maintenant vous déplacer entre les fragments ! !!
Ajoutez les déclarations requises dans build.gridle comme suggéré dans le guide "Ensure type-safety by using Safe Args".
Dans votre Listener de bouton sur le ToolbarFragment, créez un Bundle (appelons-le bun). Vous pouvez utiliser les méthodes putString et putInt pour stocker le texte et la taille de la police / font.
Vous pouvez passer le bundle au TextFragment par la navigation Navigation.findNavController(view).navigate(R.id.action_toolbarFragment_to_textFragment, bun)
Utilisez ce que vous savez des leçons passées pour lire le bundle dans le TextFragment onCreate(). Android studio a déjà quelques placeholders pour vous - voir la méthode getArguments() qui vous aide à lire le bundle.
Dans le TextFragment, vous pouvez utiliser les méthodes TextEdit setTextSize() et setTextSize() pour modifier la taille de votre texte en fonction de la SeekBar de ToolbarFragment.
Créez un nouveau projet qui est un projet BasicActivity, un BottomNavigation ou un NavigationDrawer et regardez leur structure. Vous devriez pouvoir voir des Bindings à la place de "findViewBy" et un modèle de navigation. Dans certains, vous pouvez voir des Fragments et leurs ViewModels.
Optionnel : Essayez de recréer certains de vos exercices passés à l'une de ces activités, comme l'application Image viewer du Lab3-Ex3.
Optionnel : Si vous voulez aller encore plus loin, regardez comment fonctionnent les ViewModels, voici un point de départ : ViewModels
Suivez le tutoriel du livre au chapitre 30 (cela suppose que vous ayez lu le chapitre 29). Exécutez le programme avec chaque ajout pour voir comment vos interfaces changent. Voir les notes ci-dessous sur les problèmes avec le tutoriel.
Notes sur les problèmes du tutoriel du livre :