Join Chiu-Ki Chan for an in-depth discussion in this video RecipeAdapter: OnClickListener, part of Effective Android Testing for Mobile Developers.
- [Instructor] Let's create "RecipeActivity". Right click on the top package name in the Java directory on the project tree. Select "New", "Java Class". We will call it, "RecipeActivity." In the package, append ".ui.recipe" Press OK. We will have "RecipeActivity extends AppCompactActivity". Next, we will need to override the "Oncreate" function.
Press Control All, and type "Oncreate" to select that one, and press OK. Inside "Oncreate", we will "setContentView" to the layout file that we just created. "R.layout.activity_recipe". Next, we will need to put this activity in our manifest file. Open "AndroidManifest.xml", copy and paste line 10 to line 15 for the main activity, paste it, and change the name to "RecipeActivity." You will need to change the package name as well, ".ui.recipe.RecipeActivity." We don't need the Intent Filter, because this is not the entry point of our app.
So delete those lines, from 17 to 20. Finally, we will add a parent activity name. Inside the tag for the activity, "RecipeActivity", add "android:parentActivityName", and choose ".ui.main.MainActivity". Go back to "RecipeActivity", and then go to the layout file. Use Command Click to help you navigate.
Here, the tools ":contexttag" on line 10, is still pointing to "MainActivity". So let's update that. We will point it to ".ui.recipe.RecipeActivity" instead. Right now, "RecipeActivity" is not hooked up to the rest of the app. It is only declared in the manifest. We will need to go back to the "MainActivity", and ask its adapter to handle the click. Go to "MainActivity", and then Command click on "RecipeAdapter".
Scroll down to "onBindViewHolder". Here, when we have the text view, we also want to change it so that when you click on it, it will launch the activity for the recipe. Go to line 29, "holder.textView.setOnClickListener", and then we'll create a new "OnClickListener". In the "OnClickListener", we want it to go to "RecipeActivity" with the proper recipe ID, so that it knows which recipe to load.
First, we need a context. "Context context = holder.textView.getContext()". It is not happy, so let's click on the holder, and press Alt+Enter, and make the holder final so that we can use it inside the "ClickListener". After defining context, we will need an intent. "Intent intent = new Intent". The first parameter with the context, and the second parameter will be the "RecipeActivity.class".
We need to add the recipe ID to this intent. "Intent.putExtra", and then we will need to have a string as the name of this parameter. For now, I'm just going to put something that is a inline string. So let's say, "id". The second parameter is the actual value for the ID, that is to say, "recipe.id". Finally, we are going to launch this activity, "context.startActivity", and then with the intent.
This will launch "RecipeActivity, which we'll then need to extract the value of the ID out of the intent. To make sure that the key is matching, it is good practice to define a constant. We will do that in "RecipeActivity". Go to "RecipeActivity". At the top, at line 10, at "public static final String", and then I will put "KEY_ID".
The value will be the string "id". Go back to the "RecipeAdapter", and use this constant in line 38, "RecipeActivity.KEY_ID". Now that we have defined the "ClickListener" to go to "RecipeActivity", we will also need the corresponding logic in "RecipeActivity" to extract this ID, and to load the recipe.
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP