Android development cheatsheet

Getting started in Android development.

1. Download Android Studio.
2. Launch Android Studio and create a Hello World, just click the Start a new Android Studio project and follow the instructions, choose any project name you like and choose any default activity you like and you should have a basic android app ready to run on a emulator or a real Android device.
3. Click the run button, if you don’t have any emulator set up yet, there is an option to create one from Android Studio, and then it will launch the app on the emulator. Genymotion is an alternative for the default one, the emulator created from Genymotion is much faster than the default one. It’s free to use for personal projects or you can pay to get more features and supports.
4. If you have an Android device, you can run the app on your device, but you have to enable the developer options and enable the usb debugging mode, and then all you have to do is to connect your Android device to your computer using a usb cable.


Enable developer options for your Android device.

There are lots of different variations of Android OS from different phone providers. So there are slightly different setting UI, but almost always that you just have to find the about the device from the settings menu and tap the build number multiple times until you see a messages says something alone the line you are a developer now, and you will see a new menu item showed up, developer options. Tab the developer options, and make sure you have the usb debugging mode checked and you now should be able to connect your device to your computer with a usb cable. For example, in Samsung devices, it’s usually Settings Menu -> About Device -> Tab Build Number multiple times -> back to settings -> you get the new menu item Developer options


The AndroidManifest.xml and the build.gradle files

The app/manifests/AndroidManifest.xml file is a configuration file for your android application. For every Android Activity class you created, it’s required to register that Activity in this manifest file. Other configurations including style, permission, service, receiver, etc.

The app/build.gradle file is a build configuration file which tells Android sdk how to build your applications, you specify your application version, compileSdkVersion, buildToolsVersion, third party library dependencies, etc. To include a third party library, File -> Project Structure -> app -> Dependencies -> + -> Library Dependency -> search for the libraries -> click Ok


The Java class files of your Android application

The folder app/java is where you will put all your Java class files for your Android application. Whenever your java code needs to get resources, you get the resource ids by R.layout, R.anim, R.colors, R.string, etc. For example, if you have a layout file activity_main.xml, you get it by R.layout.activity_main. You set the layout for an Activity by setContentView(R.layout.activity_main); in the onCreate method in your activity file. Each activity controls a Screen on your android device. In order to work with Activity classes, you need to understand the lifecycle of an Activity.


The resource files of your Android application

The folder app/res is where all the resource files located. The most used resource folder are layout, values, drawable, and anim.
layout folder is where you define the UI view components such as Buttons, TextViews, ImageViews, LinearLayout, RelativeLayout, etc.
values folder is where you define color values in colors.xml, dimension values in dimens.xml, string values in strings.xml and styles in styles.xml
drawable folder is where you put all the image files, you can also define drawable resources using xml file.
anim folder is where you put animation definitions in xml
See this post for supporting muliple screen sizes using resource files.


Debugging statements in Android

Log.i("info", "This is info level debugging, it will be printed in the Android Studio logcat console");
Log.d("debug", "This is debug level debugging, it will be printed in the Android Studio logcat console");
Log.e("error", "This is error level debugging, it will be printed in the Android Studio logcat console");


Android toast message lets you show a quick message on the screen

Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();


Most used and useful keyboard shortcuts in Android Studio, these are for MAC keyboards

command + shift + f, pops up a search dialogue which you can use it to search for any keyword in the entire project.
control + o, pops up a dialogue where you can choose a override method and it will generate the method signature for you.
command + o, pops up a search box where you can search for Java class in your application by name.
command + shift + o, pops up a search box where you can search for all Java class files as well as resource files.
command + click on a Java class name, method or variable in your java class file, it will bring you to the class file, method definition and variable declaration.
command + option + left arrow or right arrow, bring you to the previous or next location in a file you was at.


Locating the source code in an android application

1. You look a screen on a device, you want to know which Activity is responsible for this screen. The first thing is just to guess, if it’s a Login screen, you can try to look for classes names start with the word login in it, the keyboard shortcut command + o comes in handy for this. When this fails to find you the class, look at the screen if there are any text, and then search that text in the source code by the keyboard shortcut command + shift + f.

2. You found the string that’s appearing on the screen, but it’s in the strings.xml file and you still don’t know which class is controlling this screen. The string would have a name in the strings.xml, for example if the string name is greetings, then press command + shift + f and search for R.string.greetings

3. You found the java class but the class has thousands of lines and you want to find out if this class is using any layout files, drawable files, just search for R.layout and R.drawable respectively.

Search within Codexpedia

Custom Search

Search the entire web

Custom Search