Monday, May 16, 2016

Getting Started with Android

Last one for today, unless I get bored.

I'm currently trying to teach (show) a colleague how to get started programming for Android. Let me preface this by saying that I do not claim to be a good or even a decent Android programmer, but I at least have a grasp on the basics in my opinion.

Anyway.

Android is hard. Not just in the same sense that, well, programming is hard. But Android is hard mainly because there is no simple answer to the "how do I do this?" question. Especially for a beginner.

My colleague, whom I will simply refer to as 'he' or 'him' from now on, is running into many a road block as he attempts to learn the ins and outs of Android. He barely knows Java. He's never worked with Android before, but is 'curious.' Normally, one would wonder why I would be wasting my time trying to teach someone to, essentially, code Java from the ground up and then attach Android knowledge onto it as well. My answer to that is that I see in him what I saw in myself when I first started. I had a will to learn and a hope to improve. I would find it extremely disheartening to be turned down before I had even began.

But where to begin?

First, you need to know Java. Luckily, Java has been around for a long time, and can be picked up with a basic understanding of the ideas of Classes and Object Orientated Programming. But once you know basic Java, that's where the real fun begins.

There is no disagreement around the fact that if you want to learn Android programming, you must understand the Android Activity. The Activity is, arguably, the single most important piece of any Android application as that it what the user will in many cases actually interact with. But the Activity is big, and has many additions that are sometimes used and sometimes not. And most folks don't even use the plain Activity anymore -- Oh sorry, I meant to link this one. If a new developer were to jump into the Android Activity and try his luck at creating even a super simple application, he may not always know where to start.

Android's default project layout gives a MainActivity java file and a couple of activity related XML files. It also imports a dependency library, appcompat, which imports with it another library, support. It also manages these dependencies in Gradle.

That seems like an awful lot of work to make a single Activity which displays 'Hello World.' And it is. But this is the world that a new developer is introduced into. This confusing mess of build systems, dependencies, and Android support libraries that have almost effectively become a core part of app development.

One way to learn about a framework or a programming language is to make a To-Do list application in that language or using that Framework (this is a step that I did not do when starting out). To-do lists generally touch on a large amount of various concepts, like programming an interface and setting up a service to notify the user at given times. So my programmer padawan begins this by first creating an Activity. He doesn't know what he's going to really need yet, but its a good place to start.

We muck around for a bit with which Activity implementation he should use. I'm not going to start confusing him with various support libraries, so I just have him use the framework Activity class. Awesome. Now for his to-do list, he's going to need, well, a list, so he starts to spin up a ListView.

Normally, this is the part where I tell him that ListView has "kind of" been replaced by this new, cooler version, but I hold my tongue. He won't be able to truly appreciate how much easier things are until he suffers the view holder pattern firsthand. Then comes the first of what I can only assume will be many "Android WTF" questions.

"If Android documentation recommends the viewholder pattern, and everyone seems to use the viewholder pattern, why didn't they just make the ListView use it by default instead of me having to make it every time."

Good question, my son. Android likes choice and doesn't believe the developer should be tied to one way of doing things. But aside from that, I can't really say why. In fact, Android seems to have realized this too, hence the release of RecyclerView which revolves around the ViewHolder pattern. But its support lib appcompat only.

But anyway, back to our story. He's hacked out a ListView with a viewholder-y adapter and he's ready to rumble. A couple HLOC later and now he's gotten to a point where he wants to be able to save his to-do list items somewhere so that he can close the app and still have them. We talk about SharedPreferences for a bit, seems easy enough. He wants to know if he can save images into SharedPreferences also, but I tell him that's a topic for another day and a different tool.

He wants to have the todo list start when he starts his phone and ping him with all of his open tasks. All good, we chat about Broadcast Receivers and a bit about Toasts. Easy peasy. Everything is going swimmingly, so let's pause for a second.

It is both good and bad to learn about the Android framework in this way. I am there with him, so I can point out his mistakes and tell him about what classes and tools to use for the problems he is seeing. I can help explain to him why certain bits are how they are, and what has worked in my experience.

But if I wasn't there, I wonder how would he have gone about figuring out what to use to create this simple to-do. Sure, it would be easy to Google search the questions he had. But some answers may point him to things like SQL databases, or ORMS, and he doesn't quite need that yet. He may not know what term to search for, or how to describe his problem to the search engine. He would not know what to do because he would not even know if the class exists. Sure he could read the documentation, just like he could read the Android Studio TOS, but its going to take him down a path that, while he may learn a lot, may distract him from answering his original questions.

Of course, one can argue that in order to learn you must have an open mind and welcome new knowledge; That you should learn the "right" way, not be spoon fed the answers. I fully agree. But this is not the situation that we all face, and sometimes we lack the patience or the knowledge or the experience to approach problems in a well thought out manner.

What Ifs aside though, I was there and I did tell him. So he doesn't have to search. He types, and I talk and explain what he's doing and he nods in fake understanding. I know he doesn't understand fully, as how could anyone the first time they are exposed to so much information, but I'm hoping that he will read back the code he's produced and at least try to make heads or tails of it. I'm hoping that he will learn everything he can now, so that when he ventures into the world of the support libraries and their tools, and bugs, he will be ready. So that when he learns about Android and its major fragmentation across versions and vendors (Samsung) he'll be prepared. So that when he reads about MVP and how "everything you've done up to this point is wrong, god activities, blah blah" he'll be ready to go back and make things cleaner and nicer. So that when he begins using RxJava and other fancier Android toolkits that he will at least be able to approach his problems and think critically.

But first, he'll have to suffer. He'll have to design applications the wrong way to understand why the correct way is "better." He'll need to hand code his threading so that he can appreciate the ease of use that libraries bring. He'll need to learn a lot, while still keeping up with the massive changes that happen in the Android ecosystem almost every day. And once he has suffered, he will be ready.

For Android N to come along and change every way he thinks about Activities. For Android O to come along and break loaders again for another year.

I'm gonna go type.

========================
Follow pyamsoft around the Web for updates and announcements about the newest applications!
Like what I do?

Send me an email at: pyam.soft@gmail.com
Or find me online at: https://pyamsoft.blogspot.com

Follow my FaceBook Page
Follow my Google+ Page
=========================