Posts

Showing posts from June, 2023

View Binding

  A note about the view binding feature (Android Studio 3.6+) Since Developing Android Apps with Kotlin has launched, a new feature, view binding, has been introduced and is available in Android Studio 3.6 and higher. We don't use view binding because it came out after the course, but it's a feature to be aware of. View binding  replaces  findViewById . View binding generates a binding object for each XML layout. You use this binding object to reference views, using their resource ids as the name: // Creating a binding object for the main_activity.xml layout binding = ActivityMainBinding . inflate ( layoutInflater ) // Referencing a view with the ID roll_button binding . rollButton View binding has the following benefits over  findViewById : Type safety  -  findViewById  requires you to specify the type of view you expect to be returned. For example, if you accidentally specify that you expect an  ImageButton  to be returned when the a...

Practice Time: Lambdas

  Practice Time In this practice, you are going to write the the first part of a higher-order functions game. You will implement everything, except the higher-order functions. Let’s get started. Create a new file. Create an enum class,  Directions , that has the directions  NORTH ,  SOUTH ,  EAST  and  WEST , as well as  START , and  END . Create a class  Game . Inside  Game , declare a var,  path , that is a mutable list of  Direction . Initialize it with one element,  START . Create 4 lambdas,  north ,  south ,  east , and  west , that add the respective direction to the path. Add another lambda,  end , that: Adds  END  to path Prints “Game Over” Prints the path Clears the path Returns  false Create a main function. Inside  main() , create an instance of  Game . To test your code so far, in  main()  print the path, then invoke north, east, south, west, and ...

Lambdas Recap

 Let's recap and do an example in REPL. Restart REPL to start from a clean slate. A lambda is an anonymous function, a function without a name. {println("Hello People")}() Hello People We can assign lambda to a variable,waterFilter, with an argument, dirty, and do a calculation, dividing dirty by two, whose result will be returned. For example, here, we are passing 90 in waterFilter, and we get back 45. waterFilter(90) res5: kotlin.Int = 45 Now, here is a data class, Fish, that has one property, its name. We can then create a variable, myFish, that is assigned to a list of Fish with three Fish each with a different name: Flipper, Moby Dick, and Dory. To print the names of all the fish whose name contains the letter I, we do the following. We start with our list of fish, myFish , then we add a filter . Inside the filter, we use it to refer to the current element of the list. We get the name and check whether it contains the letter "i". This returns a list of ...

Generic In and Out

Image
If we take a look at our Aquarium class of the previous example, we can see that the generic type is only ever returned by the property water supply.We didn't define any functions that take a value of type t as a parameter. Kotlin let's us define out types for exactly this case.   Out types are type parameters that only ever occur in return values of function s, or on Val properties .  If we try to pass an out type as a parameter to a function, Kotlin will give us a compiler error. Once we make a generic type and out type, Kotlin can infer extra information about where our types are safe to use. For example, let's declare a function, addItemTo that expects an aquarium of water supply.We can call addItemTo on an aquarium of tap water. Kotlin can ensure that addItemTo won't do anything unsafe with a generic, because it's declared as an out type. Main.kt function import generics.Aquarium // File: Main.kt // Programmer: Engineer Nolverto Urias Obeso //...

Practice Time: Generic Class

  Practice Time Using type hierarchies with generic classes follows a pretty basic pattern that we introduced in the previous segment. There was a lot of material introducing generics, but basically, when you are building them, it boils down to the following steps: Create a type/class hierarchy. The parent is non-specific and the sub-types/subclasses are specializations. There is at least one shared property between the classes/types, and it has a different value depending on the subtype (otherwise, having the sub-types is pointless). We then have a class that uses all the subtypes, and performs different actions depending on what the values of the subtype’s properties are. Let’s put this into practice using building materials and a building that needs certain amounts of those materials. Create a new package and file and call them  Buildings . Create a class  BaseBuildingMaterial  with a property  numberNeeded  that is set to 1. You always need 1 of the bas...