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:


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

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:

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

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"?>
    <color name="global_color_primary">#03a9f4</color>
    <color name="global_color_primary_light">#b3e5fc</color>
    <color name="global_color_primary_dark">#0288d1</color>

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

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

You can than access all this defined values in the code using getResources() method:
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.

Saturday, September 12, 2015

[Android] Chapter 1: Preparation and Installation

Hello! So, you want to know how to make Android apps huh? You don't want any chitchat and you want to go right to the point? You are at right place then!

What will you need:
  • Android Studio
  • Android SDK
  • JDK
  • Genymotion (optional)

Android Studio:

You can download Android Studio at official site here.

You can easily install it on Linux using paolorotolo repositories:
sudo apt-add-repository ppa:paolorotolo/android-studio
sudo apt-get update
sudo apt-get install android-studio
Note: You are not required to install "Android Virtual Device" during the Android studio installation. You can launch you apps on real device, or use Genymotion, which is much faster alternative.

Android SDK:

Surprise! Android SDK comes bundled with Android Studio installation! This means that there should be no need to install it manually. If for some reason you need to download and install it manually, you can download it from here.


JDK can be downloaded at official Oracle site here. Just click on "JDK Download" button.

You can easily install it on Linux using webupd8team repositories:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
Then you can switch between Java versions using:
sudo update-alternatives --config java


Genymotion is Android emulator - it is pretty fast, and also free for personal usage. It can be downloaded here.

In genymotion you can easilly add new device with specific "Android version" and "Device model". Then you can run it just by hitting Start button.

First run:

When you run Android studio for the first time, you need to install and update Android SDK. You can do this in "Android SDK Manager".

This can be found in Configure -> SDK Manager. ("Tools -> Android -> SDK Manager" in case you have the Android Studio IDE already opened).

In SDK Manager you basically install these things:
  • Tools
    • Android SDK Platform-tools
    • Android SDK Build-tools
  • Android X.X (Api XX)
    • SDK Platform
    • Google APIs
    • Documentation for Android SDK (optional)
    • Sources of Android SDK (optional)
  • Extras
    • Android Support Repository
    • Android Support Library
    • Google Repository
    • Google Play services

Here is the example of my SDK Manager, with everything necessary installed:

Note: During the app development, you can encounter problems like this one:

In this case, you have to open SDK Manager again, and install "Android SDK Build-tools" revision "22.0.1".


If you have installed Android studio with Android SDK, and you have properly installed JDK, then your system should be ready for starting developing Android apps.

Sunday, February 9, 2014

[Linux] HOW TO: Official NVIDIA drivers installation guide for Optimus graphic cards. (Linux MINT 16)

Hello guys, Last guide was a quite deprecated... There were a lot of people having problems with it so I wrote another tutorial for current version of MINT.

(Tested on Linux MINT 16 - Cinnamon 64bit edition)

- Xorg   >= 1.13.4
- xrandr >= 1.3.5
- kernel  >= 3.9

NOTE: To get rid of the black screen problem at the end, you can set your OS to autologin your user, so you don't have to put your username and password!

1) Perform a clean install linux.

2) Add repository for X-server updates and install all updates!

sudo apt-add-repository ppa:xorg-edgers/ppa
sudo apt-get update
sudo apt-get upgrade

3) As in linux MINT 16 is already kernel >= 3.9, there is no need to update it anymore...

4) Use repository to install nvidia drivers.
NOTE: You can maybe install nvidia-331, but I didn't test it!
sudo apt-get install nvidia-319

5) Download xorg.conf to /etc/X11/ folder.
NOTE: There are already filled PCI numbers! At 90% you have the same, but check it with "lspci | grep vga" and change it, if needed!

sudo wget -O /etc/X11/xorg.conf-new

6) REBOOT system. After boot enter tty1 (Ctrl + Alt + F1). Stop mdm and move xorg.conf.
NOTE: mdm is in Linux MINT. In Ubuntu use gdm instead. In other distributions it can be even lightgdm or kdm.
NOTE: If something goes wrong, and you will have problem running, or this guide will just won't work for you, just remove this xorg.conf file, and reboot. Everything should be OK then.

sudo service mdm stop
sudo mv /etc/X11/xorg.conf-new /etc/X11/xorg.conf

7) Get your xrandr IDs. Startx on tty1(you get blackscreen), then switch to tty2 and write down your IDs.

a) tty1:
b) tty2:
xrandr -d :0 --listproviders

You should get output like this:

Provider 0: id: 0x2cb cap: 0x1, Source Output crtcs: 0 outputs: 0 associated providers: 1 name:NVIDIA-0
Provider 1: id: 0x46 cap: 0x2, Sink Output crtcs: 3 outputs: 4 associated providers: 1 name:modesetting

as you can see, my IDs are 0x2cb for NVIDIA and 0x46 for intel.

8) Create file .xseesionrc, make it executable and open it for edditing.

touch .xsessionrc
chmod +x .xsessionrc
nano .xsessionrc

9) Write those line into .xsessionrc, but replace my IDs with yours!

xrandr --setprovideroutputsource 0x46 0x2cb
xrandr --auto

10) Now, when you restart, you should get black screen, don't worry there is login screen, you just can't see it. Just wait for a sec, write your login, wait another sec, write you pass and hit enter. Be careful! Sometimes it want only password, if you have just one user. Now you should see desktop. Also you can set autologin to get rid of this.

UPDATE: As one Anonymous noted, the black screen can be avoided by putting those lines in step 9) into script that's run BEFORE the display manager. 

For lightdm it can be done by editing /etc/lightdm/lightdm.conf and un-comment the line display-setup-script=/path/to/the/script. Where /path/to/the/script is path to the script that contains code from step 9). 

For mdm it can be done by editing file /etc/mdm/Init/Default and adding code from step 9) to the end of file, right before "exit 0" code. 

This is not tested so please proceed carefully!

Thats it!

To check if NVIDIA is running:

inxi -GSx

you should see something like this:

System: Host: Sierra Kernel: 3.11.0-12-generic x86_64 (64 bit, gcc: 4.8.1) Desktop: Gnome Distro: Linux Mint 16 Petra

Card-1: Intel 3rd Gen Core processor Graphics Controller bus-ID: 00:02.0
Card-2: NVIDIA GF108M [GeForce GT 630M] bus-ID: 01:00.0

X.Org: 1.14.5 driver: nvidia Resolution: 1366x768@60.0hz
GLX Renderer: GeForce GT 630M/PCIe/SSE2 GLX Version: 4.4.0 NVIDIA 331.38 Direct Rendering: Yes
Please let me know, if something goes wrong :)

Saturday, February 1, 2014

[Linux] HOW TO: Backup your linux installation with TAR.

Hello again!

Today, I will show you how to backup your Linux installation using tar, step by step.

I am not responsible for any data damage!

(Tested on Linux MINT 16 - CINNAMON 64bit edition)


To backup system partition, we will use tar. Tar can store all files on disk into one file. Another good thing is that tar can preserve file permissions as well as user/group ownership. To save space, we will use gzip comprimation.
It is good to know that you don't have to make backup from live CD. But it is necessary to restore it from CD, if you can't boot your system. Don't put output file on the same partition! In this example, I save my backup file to second disk, connected to /media/Backup-Disk/.
Here is the command:

tar -zcpf /media/Backup-Disk/fullbackup.tar.gz --directory=/ --exclude=proc --exclude=dev --exclude=sys --exclude=mnt --exclude=media .
 tar parameters explained -> click

We exclude dev, proc and sys directories because system will restore them automatically.

Also, we exclude media and mnt directory because there is nothing to backup, and you can have connected device here to save your backup.

That's it, your backup file is now ready :)


If your system still works, you don't have to boot liveCD, to restore your backup. But remember that restoring backup won't remove any files! Only overwrite same files, and copy missing files. In my opinion, this only messes-up your system, and I don't really see any point in this.

Easiest way to restore is boot the live CD or USB stick, formate partition and restore backup. With this you get nice clean system as you had when you backed up.

1) First connect system partition you want to restore. For example I will use /dev/sda6 where is my linux partition located.

sudo mount /dev/sda6 /media/mounted-partition-to-restore/

2) Format partition
!!!WARNING!!!: This will erase all data on the partition! So be careful about it!

sudo rm -rf /media/mounted-partition-to-restore/*

3) Restore from backup.
NOTE: This will overwrite all files with same names on this partition!

sudo tar -xpzvf /path/to/backup.tar.gz -C /media/mounted-partition-to-restore --numeric-owner
tar parameters explained -> click

4) Create back missing directories in restored partition

cd /media/mounted-partition-to-restore
sudo mkdir proc
sudo mkdir sys
sudo mkdir dev
sudo mkdir mnt
sudo mkdir media
sudo mkdir /var/tmp
sudo chmod a+rwxt /tmp

5) If you need to restore grub, follow my "how to" here:

[Linux] HOW TO: Grub update / repair.

Hello again!

In this tutorial, you will learn how to repair or update grub on your system.

(Tested on Linux MINT 16 - CINNAMON 64bit edition)

1) Chose partition for grub installation. For list of partition use lsblk:

My output -> click

In this tutorial, I will use /dev/sda6.

2) Mount chosen partition:

sudo mount /dev/sda6 /mnt
sudo mount --bind /dev /mnt/dev
sudo mount --bind /sys /mnt/sys
sudo mount --bind /proc /mnt/proc

3) Make chroot for /mnt, update grub and exit chroot:

sudo chroot /mnt
sudo update-grub

4) Un-mount at the end:

sudo umount /mnt/dev
sudo umount /mnt/sys
sudo umount /mnt/proc
sudo umount /mnt/

5) You are done. Just restart system, grub should be there :)

Saturday, October 5, 2013

[Linux] HOW TO: Everything about beep!

Hi guys!

Today we will talk about beep in Linux. First, I show you which modules take care of the beep operation.


Removing the beep.

To remove just add these modules to the blacklist, and PC will never beeps again.

1) Edit /etc/modprobe.d/blacklist-beep.conf:

sudo nano /etc/modprobe.d/blacklist-beep.conf

2) type in:

blacklist pcspkr
blacklist snd_pcsp

Change beep volume.

To change beep volume use alsamixer program. Inside find beep and change it to chosen value.

Playing with beep.

The easiest way to use the pc-speaker is:

echo -e "\ a"

But if you want to change the key and the length of the tone you need to install the "beep".

sudo apt-get install beep

Now you can change the height, length and the gap between tones. Here is a small sample:

beep -l 350 -f 392 -D 100 -n -l 350 -f 392 -D 100 -n -l 350 -f 392 -D 100 -n -l 250 -f 311.1 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 350 -f 392 -D 100 -n -l 250 -f 311.1 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 700 -f 392

-f frequency
-l length
-D delay
-n new tone
Note frequency:

C  = 261.6
C1 = 277.2
D  = 293.7
D1 = 311.1
E  = 329.6
F  = 349.2
F1 = 370.0
G  = 392.0
G1 = 415.3
A  = 440.0
A1 = 466.2
B  = 493.9

C2  = 523.2
C22 = 554.3
D2  = 587.3
D12 = 622.2
E2  = 659.3
F2  = 698.5
F22 = 734.0
G2  = 784.0
G22 = 830.6
A2  = 880.0
A22 = 932.3
B2  = 987.8

Thank you for your attention.
Have fun with that :)