Basic Android Development Cheat Sheet

Ryan Stewart
Jun 1, 2017 · 4 min read

Every want a place to reference layouts, listeners, or literally anything else? Look no further!


The Set Up

When setting up the app, you really only need to focus on a few things.

The Application Name: This will stay with your app forever, so make it meaningful.

Min and Max Version Number: The minimum Android version is the lowest version your app will support. A lot of people set theirs to 2.2 (Froyo); However, this prevents you from using new features from any Android API released after Android 2.2.

MainActivity and Layout Name: The MainActivity is what your main Java file (where all your important code will go) is called. It can be named anything, just remember what it is. Layout Name is the name of your layout file, a file that Android uses to align features in your app.


Layout Options

There are three types of layouts in Android Studio: Linear, Relative, and Grid. The layout type is set by the first tag in activity_main.xml. (All images in this section are credited to Derek Banas).

“<RelativeLayout” is what tells Android Studio that the app is styled Relatively.

Linear Layout: Linear layouts are either vertical or horizontal. They are set the same way with the <LinearLayout> tag, but the attribute “android:orientation” is set to either “=’horizontal’” or “=’vertical’”. Linear layouts organize elements like so:

Vertical linear layout. Notice how it ONLY stacks up-and-down.
Horizontal linear layout. Notice how it stacks left-to-right until it’s out of room.

Relative Layout: Elements are organized to either the left, right, below, or stretch to another object. Relative layouts are set with the tag <RelativeLayout>. To set if relation to another object, us the “android:layout_toLeftOf=”@+id/xxx”” attribute (or toRightOf, etc). They organize like so:

Relative layout. match_parent means to fill the entire area of the app’s activity.

Grid Layout: Grid layouts organize, like a spreadsheet, in rows and columns. They are set with the “<GridLayout>” tag, and they look like this:

Grid Layout.

Values Folder

The values folder holds constants that are shared globally throughout your app.

Dimens.xml: XML file containing two <dimen> tags in dp unit. Sets the global app width and height dimensions.

Strings.xml: Holds ALL TEXT VALUES to be used in your app. This is for two reasons: 1) Easy editing if multiple text values are shared on different pages, and 2) Automatic / Easier translations to other languages. When you write a string in an element, write “@string/x” as the text, where x is the variable name found under strings.xml.

Styles.xml: Holds the Android Studio (to whatever API you’re using) style types and areas for you to customize colors, et cetera.

Android Manifest

AndroidManifest.xml is a file that stores information about your app, such as your unique package name and version number. It contains the <intent-filter> tag, with “intents” being what an action is intending to do. For example, the action you start with will have a tag of “<action android:name=”android.intent.action.MAIN”/>” because it’s going to run when the app starts.


The Java ☕

The main place you’re going to be writing Java is in the MainActivity.java (or whatever you named your activity) class. This class contains various methods for app actions, such as when an app starts (onCreate() function), the options bar is opened (onCreateOptionsMenu() function), and when an option is selected (onOptionsItemSelected() function).

Getting An Element With Java

Want to selected a button you placed using the designer/XML in your Java class file? It’s easy! Just use the “findViewById()” method!

If you wanted to select a TextView, you would first create a final constant with the data-type of TextView, then set that equal to a casted TextView using the findViewById() function. For example:

final TextView myTextView1 = (TextView) findViewById(R.id.textView1);

Or a button:

Button myButton1 = (Button) findViewById(R.id.button1);

This is not marked final because it is not inside an inner class; However, you don’t even need to mark a TextView final for it to work, so I really don’t understand why it’s recommended.

Listening For a Button Click

After you have your button created and a variable assigned to it, to make it do something when clicked, you must create a listener. To do this, type the button variable with the “setOnClickListener” method with the argument of “new View.OnClickListener()”. Android Studio will automatically set up the click function for you, making the complete function look like this:

myButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}

That concludes the basics for Android app development! Hope you’ve found this useful!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade