Saturday, April 30, 2016

[Android] Chapter 3: Package Structure


Hello! Today we take a look at project and package structure in our project.

Project structure:

The basic project structure is the following:

  • app
    • src
      • main
        • java - Contains java classes.
        • res - Contains resources (images, layouts, dimensions, strings, etc.)
        • AndroidManifest.xml - File defining application components.
      • build.gradle - Build configuration file.

the Java folder contains our project package (like com.example.myapp). We already chosen this name when we created our project. This project package will contain all our Java classes.


Java package structure:


Let's say our project package looks like this:

app->src->main->java->cz.helu.stocks.

In this project root package we will create another packages for easier orientation. We should use simple names, which can tell us exactly what the package contains. As an example, I will show you my favorite project structure I personally use:

  • app
    • src
      • main
        • java
          • cz.helu.stocks
            • activity
            • adapter
            • database
            • dialog
            • entity
            • enum
            • fragment
            • service
            • rest
            • utility
            • StocksApplication.java
            • StocksConfiguration.java

Many of those are pretty simple and self explanatory. Like activity, adapter, database, fragment, service, etc.
Some people suggest putting entity and enums into the same package. I personally prefer to split them in two separate packages. Also, I like to put DialogFragments into dialog package, instead of fragment package.
In the rest package I usually store everything that has something to do with REST API calls, like whole retrofit configuration and classes.
More complicated is the utility package, where I personally store classes that can't be placed anywhere else. For example DateConverterUtility.

In the project root folder I also like to create the Application class and also Configuration class, in which I hold many useful information that I use in other places of the app, and want them in one place (for example REST API URL, logs, and services switchers).


Resource package structure:


Here we store our layouts, images, icons, and other resources for our application. Here is the structure we should use.
  • app
    • src
      • main
        • res
          • drawable
          • drawable-mdpi
          • drawable-hdpi
          • drawable-xhdpi
          • drawable-xxhdpi
          • drawable
          • layout
          • mipmap-mdpi
          • mipmap-hdpi
          • mipmap-xhdpi
          • mipmap-xxhdpi
          • values
            • colors.xml
            • dimens.xml
            • strings.xml
            • styles.xml
            • themes.xml

In drawable folder we store images. In mipmap folder we store application icon. In layout folder we store all the layouts we use in the app and in the values folder everything else.
You surely noticed that there is a lot of drawable and mipmap folders. There is a reason for it. As a developer you should support multiple screen sizes and this it the way for doing it. When you create some image you want to use in your app, you should create it in more sizes, and put it in all those folders. You can learn more about it here: http://developer.android.com/guide/practices/screens_support.html.

Now for the values xml files, they all have the following structure:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Some items here -->
</resources>

And here are some examples for the xml files you should use. All are pretty self explanatory.

The colors.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="global_color_primary">#03a9f4</color>
    <color name="global_color_primary_light">#b3e5fc</color>
    <color name="global_color_primary_dark">#0288d1</color>
</resources>

The dimens.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="global_spacing_8">8dp</dimen>
    <dimen name="global_spacing_16">16dp</dimen>
    <dimen name="global_spacing_24">24dp</dimen>
</resources>

The strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="global_network_offline">No Internet connection available</string>
    <string name="global_network_fail">Communication with server failed</string>
</resources>

You can than access all this defined values in the code using getResources() method:
getResources().getColor(R.color.global_color_primary);
getResources().getDimension(R.dimen.global_spacing_8);
getResources().getString(R.string.global_network_offline);
In the next lesson we will take a look at how we should correctly name these classes and files in your package.




[Android] Chapter 2: Creating first project


Hello! Today we take a look at creating new project in Android Studio.

Creating a project:


First thing you have to do is fill your "Application name". The app will be presented to users under this name.
Second thing is to fill "Company Domain". This is qualifier that is appended to "Package name". Package name must be unique across all packages installed on an Android system, so chose wisely!


Here you can specify devices supported by your app. In this tutorial we will develop application for Android Phones and Tablets on API 16+. Chosen min API level determines methods you can use in your project. Olders API do not support some functionality. No worries, you can change it later.

Let's just create basic project with a Blank Activity. This is all we need to have runnable application with some basic skeleton at the start.

I recommend to leave this at is it. I will talk about naming convetion later.


Congratulation! You have created your first project! Do not hesitate and click Run app button at the top (green arrow (shift + F10))

Now. I recommend to switch Project tab display mode from Android to Project (dropdown 2.). You will have better overview on your project in this display mode.

In the next lesson we will take a look at project and package structure.