How to get started with Android development – Basic structure

andro02In a previous article, I wrote about how you can launch your first Android application from the samples provided with Android Studio. Now that you’ve had a chance to poke around a few samples, I’m going to tell you a bit about the basic structure of an Android application.

A good place to start understanding an application is the manifest. The AndroidManifest.xml file links all the parts of an Android application together, including:

  • The activities contained in your application,
  • The theme you use,
  • The name of the application and its icon on the main screen,
  • The permission the application must request to run.

Activities and the java folder

The Java code of your application lives in the java folder. The first class you’ll create is a Activity class, which is the main building block of all Android applications. An activity is a full screen window in which all your code will run. Each activity has it own life cycle which starts when it’s first launched and last until it is finally destroyed.

The user navigates between different activities in your application: the back button of the device closes the current activity by default and goes back to the previous one. If the user closes the first activity of your application, he’ll go back to the application that was previously open, if any. You can also launch other applications (with their own activities) using intents. You can use this to request an application that can send an email or play music, and the installed applications that can manage this will be displayed to the user.

The activity life cycle can be affected by events outside your control: for example, if a phone call comes in, your current activity (so your application) will be paused. Each activity has a layout associated with it that describes how controls that are displayed, and the activities are declared in the AndroidManifest.xml file.

Unfortunately, the fact that the activity is bound pretty strongly to the navigation AND to the user interface/layout makes it hard to separate the business logic and the display logic properly. You can create a usable Android application with only a .java file with an Activity class, but eventually you’ll want to move on to a better architecture. There are some patterns out there that can help you do this, but for your first application you should stick with this and avoid introducing additional complexity.

Resources

Resources are everything that your application needs that is not Java code, such as layouts, strings, images and other constants. The Android API uses XML for most resources. The res folder contains all the resources of your application, such as:

  • XML layouts for each activity and component in the layout folder, styles in the values\styles.xml and images in the drawable folders. Layouts in Android are inspired by HTML/CSS, so if you’ve ever done some web development you’ll feel right at home pretty soon.
  • All the strings used in your application in values\strings.xml. You can hardcode them in the layouts, but if you ever need to translate your application you’ll be glad you put them all in one place to get started with.
  • Menus for your application are defined in the menu folder.

See the other articles in the series here

Get my Learning as a Software Developer guide

You became a software developer because you love programming and couldn’t believe people got paid to do this. You first learned programming at school, where somebody else dictated what you should do and learn. After you finished your degree, you finally got a job, and you kept learning fast about building software and your industry until you were up to speed.

But now that you’ve settled into a routine at your job, what is the next step? How do you keep learning and growing as a software developer? Nobody will tell you what you should learn next anymore: you’re on your own. But you’re stuck on an endless plateau, and can’t see a way out of it. You maintain the same old legacy application and fix routine bugs day in, day out, since you’re too valuable in your current role. You can feel your passion and motivation dwindling dangerously fast, but you don’t know what do to about it.

Even worse, you’re falling behind, slowly crushed by the huge treadmill of new frameworks and languages. All those kids fresh out of school are all about React, Rust and NodeJS, while you were learning Java only a few short years ago. You know you can learn new tools without waiting for permission but every time you tried it a fancier tool came out shortly after, making your knowledge obsolete. Silverlight anyone?
Treadmill
You could switch jobs, leaving your crummy legacy code behind to solve new problems. It’s a valid strategy if you can’t take it anymore, and it’ll work for a time. Unfortunately, once you’re over the initial learning phase, you’re going to repeat the same pattern. If you start over as a junior every few years, you’re never going to improve your craft.

But what if you could take control and grow without waiting for a boss to tell you what to do? What if you could keep learning, stay sharp and become passionate about software development once again? You’ll have skills you’re proud of, work on cool projects and be confident in your knowledge and expertise.

If you try to keep up with all the new frameworks and tools you come across on Hacker News, staying up to date is an overwhelming burden. Fortunately, you don’t have to do it all: you can choose the skills to invest in to get the best out of your limited learning time and grow as a software developer.

Understand how to choose the best skills to learn with the Learning as a software developer guide and workbook. You will :

  • Learn to see the big picture, understand where you are and what motivates you.
  • Plan which skills you need to learn to reach your goals.
  • Choose what you’ll learn next deliberately instead of jumping on the latest bandwagon.
  • Pick up new skills with ease using the right techniques and habits.

Grab a cup of coffee and the Learning as a software developer package, and get to work becoming the best software developer you can be!

Purchase now at https://sellfy.com/p/ibXx/.

Getting over myself and doing things the easy way

During the summer, I usually spend a lot less time working on the computer. I’m outside instead, taking advantage of the short Canadian summer to garden and soak up sun while it lasts. When I do work on something, I keep it light and try new technology that piqued my curiosity just for the fun of it.

Poule-smallThis year, we also started keeping chickens, so my new project is to automate our chicken coop. The goal of this system is to automate the lights, and act as a weather station to collect information about the conditions inside and outside the coop. All of this will be controlled by a web application accessible from a smartphone. Ultimately, I’d like to automate the doors too, but it’ll require a bit more work.

It’s a fun project that allows me to dust up my neglected hardware skills. It’s been a few years since I’ve seriously worked with hardware: when I was last involved with microcontrollers and embedded computers, Arduinos were just starting to get popular. Since it’s been so long, it’s been interesting to see how my thinking has evolved.

Some background…

I have been a professional programmer all my working life, but I’ve studied electrical engineering AND programming. One could believe that all that theory from school would be useful, but it used to slow me down a lot: every time I would start a new project, I would first try to plan out how everything would go.

I would then start worrying about all the potential problems that could come up, get sucked down the rabbit hole and eventually quit working on it. If I didn’t knew much about electronics, I could have hacked things together until they worked and be satisfied, but I had the curse of knowing just a little bit too much.

Also, since I did spend a few years studying this, I felt an obligation to follow the “one true way” and make my own life as hard as possible. Why use Arduino and existing libraries when avr-gcc works just fine? Libraries are for wimps! If you’re not pouring over 100+ pages of datasheets, you’re not doing real electronics!

Getting over myself

All of this is completely ridiculous of course, especially if it’s a project you’re doing for fun. I’m thankful there are standards and good practices for commercial products: I don’t want my oven to set the house on fire or interfere with my smartphone. But if you’re just looking to learn and experiment, you can’t do much damage with a 5V circuit. Maybe you’ll burn a few components, but that’s life.

Looking back, I see a lot of similarities with how people are told to learn to program or to start using a new language, cramming in too many computer science patterns and practices before getting started. Trying to learn theory without balancing it with practical work on real software is a dangerous pitfall that will stop you from progressing. If you have too many things to keep in mind at once, you’ll start worrying about how your project will collapse under its own weight because of your spaghetti code, even if you’ve barely gotten started.

In the end, you’ll stop yourself from shipping projects even if you don’t have any users yet. You’ll start believing that you must understand everything to perfection before getting started, and that no shortcuts are allowed, ever. That makes it really hard to keep learning and growing.

As a programmer, you’ll learn where you can cut and where you must put more effort with experience. This can’t be learned from books and courses, you have to see it for yourself. There is a time and place for implementing things yourself because you wish to learn more or because it’s a critical part of your project, and another for using code someone else has written and that is good enough.

Thankfully, I’ve gotten over myself and I’m now more laid back about using hardware and code other people made. For this project, I’ve been using some great open source code from the Arduino community instead of trying to reinvent the wheel, and it’s been a blast. Hopefully, I’ll bring this one to the point where I can use it!