Udacity Kotlin Nanodegree: Course 1

I have been waiting for this course to come out for the past six months and now it is here! I was able to get a 50% off on the course so that was really helpful. So far, the course has been amazing and I have built my first app, a dice roller!

You can check it out here. https://github.com/esegebart/diceroller

This is what I have learned in my first course. I am posting this as reference to myself later.

Namespaces – tools – define dummy content that is only used when you preview the app in the preview pane

Attr using the namespace tools will be gone when the app compiles

Two namespaces at top: tool and android. Android is part of the core framework.

Gradle determines what devices can run your app, compiles app to executable, handles dependency management, automated testing, and app signing for Google play. Gradle packages your app into an apk with its resources, compiled code, Android manifest, etc and then transfers the apk to your emulator or device.

There are two Gradle files: build.gradle (project) and build.gradle (app). The app gradle file handles your different modules that has different functionalities, libraries, and supporting android wearables. In larger apps, each module will have its own gradle file. Inside the Gradle project file, there are repos that are available to the whole project: google() and jcenter().

Gradle also handles all the dependencies which is external code and libraries a project depends on. These are handled in the dependencies block.

dependencies {

           dependencies are managed here

           app dependencies do not go here

}

The app folder gradle file configures how to set up the app module and deals with the plugins to run and build Android and kotlin projects. This also tells the app the compilesdk, targetsdk, and minsdk. These are pretty straighforward. Min is the minimum version it can run on. The target will always match the compilesdk. This also has the appID which is a unique identifier and necessary for Google Play. It is the domain your app is hosted on, reversed, followed by the app name. So if my app was hosted on elyse.com and my app is named diceroller, it would be com.elyse.diceroller. This is set up when the project is started.

The version level you lists matches to an API level, and all api levels are named after tasty treats.

ANDROID JETPACK is the new android support libraries – androidx – and has all the support libraries for new and old apis.

APPCOMPATACTIVITY – The main activity extends from this. It is a compatibility class that makes sure activity includes a menu bar (action bar) that looks the same across OS levels and is a part of androidx. I never knew exactly what this was, so this was helpful.

Adding Vector Drawables: The proper way to use vector drawables and keep compatibility with older versions of android is followed.

  • In Gradle build.app file – under default config add vectorDrawables.useSupportLibrary = true.
  • Add app:srcCompat=”@drawable/image_name” in the ImageView tag
Add the namespace to the root of the layout xmlns:app="http://schemas.android.com/apk/res-auto"
  • This is using the support library to reference the image resource in the layout file and enable the support library for vector drawables in the app level build.gradle
  • THIS OPTIMIZES APP SIZE ON OLDER PLATFORMS
  • Open drawable (dice.xml) to see the colors and shapes listed in xml

build.gradle app file will generate png files that are use on those devices that are below minsdk. Png files will make app larger and make it slower. Large apps have high chance of being uninstalled. Androidx compat library supports devices all the way back to api level 7.

Adding app namespace supports either custom code or libraries in your project and not core android libraries.

Dependencies required to build the project or enable additional functionality are defined in the build.gradle.

Basic app structure:
Layouts – xml files the define what your app will look like
Activites – Kotlin classes where you write the dynamic parts of your app
Layouts and activities are connected by the process of layout inflation – and finding view objects by their unique id
TextView – displays text to the user
ImageView – display images to the user
Images are a drawable resource type.

Build gradle project in command line:
Change the directory of your project.
Type gradle run.
You can also include gradle run –warning-mode=all to see specific warnings.
Or you can type gradle tasks –info. Shows all tasks and what they are doing.

Clean Code: Naming Conventions

A series of blog posts based on Robert C. Martin’s book “Clean code : a handbook of agile software craftsmanship“.

You can find the book here.

As a programmer, we are going to be naming lots of variables, classes, and methods. According to Uncle Bob in Clean Code, we should name a variable with the same care we do in naming a first-born child. After all, we are going to need to be able to call it, know what it’s for, and not confuse it with any other variables in our classes.

When we name with care, we name with “intention-revealing” names. Naming well saves you more time in the long run, saves everyone more time, and prevents a snowball of bad things happening to your precious code. The names we choose should answer the big questions:

Why does it exist?
What does it do?
How is it used?

Good and bad naming examples in code.

We need to be able to tell what the code is doing. In the example picture, it is very easy to see what the purpose is of the last three names versus the first name. Explicit names help immediately see what is going on in the code. If you MUST use a single letter name, it is best practice to use it as a local variable inside a short method. The length of a name should correspond to the size of its scope.

Using a name like h, we would have to mentally map what this variable is doing, translating it in our brain to what it actually does, using much more memory. Clarity is king. Use your powers for good naming practices to write code that everyone can understand. Pretend like the next person who will read your code is a violent psychopath who knows your address. It’s serious business!

Class and Method Names

This is something that needs to be written on your computer or tattooed on your arm or put anywhere you can see it, like an affirmation you are practicing, only programmer edition.

Photo by ThisIsEngineering on Pexels.com

Class names should have a noun or noun phrase. Class names should not be verbs.

Methods should have verb or verb phrases. Accessors, mutators, and predicates should be named for their value and prefixed with get, set, and is according to the javabean standard.
(“Oracle Java Technologies | Oracle,” 2019).

Don’t be cute in naming. Clever names are memorable only to people who share the same humor and some of us are the only ones laughing at our jokes. I know I’m funny, but others may not think so. Clarity is more important than entertainment. Our cleverness only goes so far as our cultural slang. Naming something dirtNap() meaning kill() or wipeout() meaning DeleteItems() is cute, but the latter is understood by all. A consistent lexicon is worth its weight in gold for the programmers who must use your code.

Other Important Naming Concepts

Pick one word for an abstract concept and stick with it. Using fetch, retrieve, and get as similar methods in different classes goes back to using too much mental memory. Ask yourself some questions about what these methods are doing and see if they are all serving the same purpose to choose an appropriate word.

Don’t Pun. Converse to using one word per abstract concept, do not use the same word for two purposes. Doing so would be a pun. Using “one word per concept”, could end up with a lot of classes using an “add” method as described in the book. If we are using the word for consistency rather than intent, it can get very confusing. Ask yourself what the method is actually doing so you know if you need to name it “insert” or “append”.

Use Solution Domain Names. Programmers will be reading your code so go ahead and use terms that other programmers understand like algorithm names, math terms, and pattern names.

Use Problem Domain Names. Problem domains refers to all the information that defines the problem (i.e. the problem you are solving with code). If there are no programming or computer science terms for what the code is doing, use the name from the problem domain. Good programmers and designers have the important task of separating solution and problem domain concepts.

Photo by Startup Stock Photos on Pexels.com

Add Meaningful Context, But Not Gratuitous Context. Most of the time we have successfully added meaningful context by using good naming, separation of concerns, and well-defined classes. As a last resort, you may have to add some prefixing. If you saw firstName, lastName, street, houseNumber, city, state, and zipcode in a method, you would assume it’s an address. If city was used alone in a method, would you assume it’s part of an address? The context can be added by prefixing with addrState, addrCity, etc so it’s explicitly part of an address.

This doesn’t mean to do this always. Adding gratuitous context can be very confusing, like prefixing every method with the acronym of the application. “ABCFirstName, ABCLastName, ABCStreetAddress” are too much. Shorter names are generally better than long ones when they are clear.

Following some of these rules and asking yourself the important questions about your code will help get you thinking about what and why you are naming things and keeping the code understandable for all, saving you many headaches in the future. Happy coding!

References:

Oracle Java Technologies | Oracle. (2019). Retrieved March 29, 2020, from Oracle.com website: https://www.oracle.com/java/technologies/

Martin, R. C. (2009). Clean code: A handbook of agile software craftsmanship. Upper Saddle River, NJ: Prentice Hall.




We Belong Here Podcast

I had an amazing experience this year to be on the We Belong Here Podcast with Lauren Lee Anderson (@lolocoding on Twitter).

I had heard a lot of podcasts about how they career transitioned from one career that was totally different and into tech. I didn’t hear a lot about the dark side of transitions. Transitions that come from dark, personal, life-changing experiences. My past is dark.

I got very vulnerable in this podcast to show others that it is your thoughts that allow you to keep going, a desire that keeps you going, knowing what you don’t want in your life that keeps you going.

Roadblocks happen, I believe they are supposed to. Success isn’t linear and those roadblocks build your resiliency. If you struggle with wondering if you can make it, you can. Have a listen.

I would love to hear your stories of how you overcame your obstacles!

Episode 21: Elyse Segebart
https://bit.ly/3bfXOyk

Elyse Segebart on the We Belong Here Podcast

Gradle Build Process and Deploying Your App

So far, here’s what I have learned about how the Gradle build process works when you are ready to sign and deploy your Android application:

  1. The application source code needs to be in DEX (Dalvik Executable) files. The compiler converts all source code into compiled resources that include the bytecode that runs on Android Devices.
  2. DEX files and compiled resources are organized in one package by the APK Packager. The final product is an unsigned APK and needs to have a signature in order to be installed or deployed.
  3. There are two ways the APK Packager signs your app:

a. The debug version of your app is for testing and profiling only and will receive a debug keystore which is also the default keystore for new projects in Android Studio.

b. The release version (the one you will release externally) will receive a release keystore.

4. The packager will use zipalign tool before generating your final APK. This ensures it is optimized to use less memory when running on a physical device.

A signed APK file will include a “META-INF” folder containing three files: manifest.mf, cert.sf, and cert.rsa. Unsigned APKs will require a user to enable Unknown Sources to be able to install it. Amazon accepts unsigned apps for their store and Google Play Console does not. When you want to market your application, many users prefer signed ones over unsigned ones because downloading from unknown sources could be dangerous as it might include viruses, malware, or spyware.

References

Google Developers. (2019, January 23). Configure Your Build. Retrieved from Android Developers: https://developer.android.com/studio/build/#build-process

Mangroliya, S. (2012, April 23). Android differences between signed and unsigned .apk files? Retrieved from StackOverflow: https://stackoverflow.com/questions/10280278/android-differences-between-signed-and-unsigned-apk-files/10280362

Android N Developer Course: D5

Today I created a higher or lower game in Android Studio. Used Random class to generate a random number when the app launches.

Created a method that takes the user input when a button is clicked and converts the String to an int. Then I used If statement to take the int object and compare it to the random number that is generated. The app will tell the user higher or lower based on which statement is true. If the number is correct, it tells the user “That’s right! Try again!” and added the Random number code to generate a new number so the user can play again.

Went one step further and created a method called makeToast that takes a String object as its parameters and will display a message to the screen. So when the makeToast function is called, it will display the appropriate message.

This was super fun and it was great to see how to further simplify code by creating the makeToast function. I didn’t see it right away that it was possible, but with time I think I will get better at simplifying my code. And a photo of the app!

Android N Developer Course: D4

100 days of code: D8&9

Today I built currency converter application to take in Dollars and use toast to pop up the message in pounds. Even added the little squiggly pound sign to make it look more official. This was the end of the Android course section two. Next is the Java deep dive which I am really excited about. Some of the takeaways from the currency converter are using

String.format(“%.2f”, poundAmount)

In this example, the String.format sets the decimal point to two places and accepts the variable it will be formatting as the second parameter. If you are wondering what the “%.2f” means, like I did, %f is the format specifier for float data type in the functions printf and scanf. This format specifier will display up to six digits after the decimal point, but using %.1f or %.2f will make the precision to the first or second digit.

I added in the currency photo from Google and matched parent width.

I had to take the EditText (which is a String) and create a new variable and parse it to a Double. I added the conversion and assigned it to the variable poundAmount.

Then I used toast to display the conversion to pounds at the bottom of the screen.

Here is the code snippet:

And the application output!

Android N Developer Course: D1

Days 3 and 4 of 100 days of code.

What I’ve Learned:

I had a problem getting my project set up and kept getting the error “Add Google Maven Repository and Sync Project”. The solution to this was to go into the Gradle Project: Build files, find “allprojects” and inside of “repositories” add google() and sync. This error was realized in the MainActivity, my class file for the “import android.support.v7.app.AppCompatActivity;” was read and the AppCompatActivity was red. So, if you ever have this problem.. this solves it. I was trying to run a version of Android that was too old since I am working on the Android N Developer course and had to update it to SDK version 28 and it required the Maven repository.

If you are struggling with getting layout to work in the activitymain.xml, check one of two things. First, go into your component tree and see if you have ConstraintLayout or RelativeLayout. RelativeLayout is a lot easier to work with since you can drag and drop the components onto the screen. Right click ConstraintLayout and “convert view”.

Second, if you ARE using RelativeLayout and they keep popping to the top left corner, go to the top left of your design screen and you will see a tiny little magnet. Click this to turn autoconnect on/off.

Something interesting I found out is 1 dp is roughly 1/60th of an inch. So 160dp is about 1″ on any Android Screen. Good to know for design!

If you are working with TextView and trying to retrieve the text from it by clicking a button, you will create a function that is called when you click the button. You will declare a variable of EditText and typecast EditText to the findViewById (since find VIEW is obviously looking for a view). “R” is for resources and then you will find the ID you entered for the component in the xml file. The button you created on the screen will also need an onClick function saved to it in the XML. You can use Log.i which takes a String tag and String msg parameter. I used the tag as “Info” and used the ID and getText().toString() function to return what it retrieved and convert it to string to display to the log. Code below to explain!

Find this helpful? Connect with me to get these kind of tips everyday!

Thank you for checking my post out.