Git pre-push hook’s use case; Catch lint failures before pushing code to remote repo

Recently I ended up adding this simple pre-push hook to my local git repo. In our work environment, when we make a build, Bamboo is building the branch to make sure it is not failing. We have a setup where we take lint warnings very seriously and lint errors can end up in a PR, failing to build.

giphy

If you’re using AndroidStudio’s build in Version Control, you get notified about lint errors but if you’re using the Terminal to make commits, this can be a problem.

1- From your project root, go to hooks directory.

~ $ cd .git/hooks/

2- Now add the above script into pre-push

~ $ nano pre-push

Add following script into file.

#!/bin/sh
echo "executing pre-push hook to make sure lint is not failing the build"

# Run the lint with the Gradle wrapper. You can use lintProductionRelease 
# to run it for only one flavour. (time saving)
./gradlew lint

# Store the last exit code in a variable
RESULT=$?

# Return the exit code
exit $RESULT

3- Make sure the script is executable.

~ $ chmod +x .git/hooks/pre-commit

Now every time you will make a push from Terminal/AndroidStudio and there is a lint error, it will not push the branch to remove repo.

Menu in Activity and Fragments

Create a new xml file; res/menu/menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context=".MainActivity">

    <item
        android:id="@+id/action_add"
        android:icon="@drawable/ic_action_add"
        android:orderInCategory="100"
        android:title="@string/action_search"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:orderInCategory="100"
        android:title="@string/action_search"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="ifRoom"/>
</menu>

To add the menu in the activity you need to override following two methods;

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                //Do some action
                return true;
            case R.id.action_settings: {
                //Do some action
                return true;
            }
            case R.id.action_add: {
                //Do some action
                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }

Fragments will follow the same menu is an in the parent activity. In case you want to modify menu for a fragment, you have to setHasOptionsMenu(true) in onCreate method;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

Now you can manipulate a menu. Let’s say you want to hide a specific action;

@Override
public void onCreateOptionsMenu(Menu menu , MenuInflater inflater){
    mMenu.findItem(R.id.action_add).setVisible(false);
    super.onCreateOptionsMenu(menu,inflater);
}

You can also hide the whole menu for a fragment;

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
}

You can create a fragment specific menu and inflate the menu for that fragment. Create a new menu i.e. res/menu/menu_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_settings"
        android:orderInCategory="2"
        android:title="@string/action_settings"
        app:showAsAction="ifRoom"/>
</menu>

and now you can either extend this menu with the activity menu or use it as the only menu.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //map.clear(); //Clear the previous menu from the activity.
    inflater.inflate(R.menu.menu_fragment, menu);
}

Now you can override the method onOptionsItemSelected to handle item selection.

Repeat Background of a View

Create an new xml file, let’s say backrepeat.xml

<?xmlversion="1.0"encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="false"
android:src="@drawable/dot"
android:tileMode="repeat"/>

In your layout where you want to set this background add;

android:background="@drawable/backrepeat"

Mac Terminal Keyboard Shortcuts

[table class=”table table-striped”]Shortcut,Description

⌃ + A,Move to the beginning of line.
⌃ + E,Move to the end of line.
⌥ + ⟵ / ⟶,Move word-wise.
⌃ + C,Delete the entire line.
⌃ + W,Delete Previous word.
⌃ + K,Kill everything on the right of cursor.
⌃ + L,Clear Screen.

[/table]

 

Android Studio Keyboard Shortcuts

[table class=”table table-striped”]Shortcut,Description

⇧ + ⇧,Search Everywhere
⇧ + ⌘ + A,Search for Actions

,

⌘ + 1,Switch between project windows
⌘ + ⇧ + ⟵ / ⟶,Change size of project windows
⌘ + ⇧,Jump to the navigation bar
⌘ + E,Recent Files
⌘ + ⇧ + E,Recent Changed files.

,

⌘ + ↓,Jump to source code.

,

⌘ + B,Go to interface
⌘ + ⌥ + B,Go to implementation

,

F6,Move code block
⌘ + ⇧ + ↑,Move function above.
⇧+ F6,Refactor code
⌘ +ALT+ T, Surround with…
⌘ + J,Find live templates

,

⌥ + ↑/↓,Smart selection
⌘ + ⇧ + ⌫,Last Edit Location
⌘ + ⇧ + ENTER,Complete Current Statement
⌘ + ⇧ + J,Join Lines
⌘ + ⌥ + M,Extract Method
⌘ + P,Check the parameter info
⇧ + Enter,Enter New line

,

F2,Go to next Error
⇧ + F2,Go to previous Error
⌘ + F8,Toggle Breakpoints
⌘ + ⇧ + F8,View Breakpoints
[/table]

px, dp and sp? What should be used and where?

There are hundreds of Android device models with different sizes and resolutions. Keeping UI consistent among all devices will be almost impossible if we are not correctly understanding the difference between px, dp, pt and sp.
Let’s understand some technical terms;

Screen size: Android devices are grouped into small, medium, large, extra large, double-extra and triple-extra.
Screen Density: Amount of pixels within an area. This is grouped into; low, medium, high and extra high.
Resolution: Total number of pixels in the screen.

dp: Density Independent Pixel, it varies based on the Screen Density.
px: Standard pixel which maps to the screen pixel.
pt: 1/72 of an inch, with respect to the physical screen size.
sp: Scale Independent Pixel, scaled base on user’s font size preference.

Conclusion:

  • Fonts should always use sp.
  • Everything else should always use dp.
  • Never use px or pt.

Formula:
px = dp * (dpi / 160)

Resources:
Free Online Tool can be used to generate better scaled resources.
Android DPI Calculator

Finding the Latest Version of Gradle Plugin

If you are managing a non-gradle based android project, you need to find the cross sign on top to close this article.

Once a while, when we are introducing gradle dependencies into our project, we need to find the latest version of a dependency. Like, recently I was look into this dependency from okhttp3 (by Square) i.e. logging-interceptor and on their github page they had following message to download the dependency;

compile 'com.squareup.okhttp3:logging-interceptor:(insert latest version)'

How to find the latest version? My recommendation is; jFrog Bintray. Just enter the name of the dependency into the search bar on the top.

Bintray Search

You will get list of relevant packages and the latest version of packages.

Bintray Search Results

Do you know a better way to manage this? Please feel free to comment.

Finding Specialist Doctor (Speciallæge) in Denmark

Once a while we all get sick or face some kind of injury. Every Danish residence is covered by Medical insurance in form of Yellow card.

Types of Medical Treatment:
  1. In case of assistance, you are suppose to reach your local physician. Contact number of physician would be available on the yellow card.

    Sample Danish Yellow Card

    Sample Danish Yellow Card

  2. In case you need assistance when the local physician is closed (i.e. after 4pm), you can call 1813 (Capital Region of Denmark). The Medical Helpline is staffed by physicians and nurses who are able to guide you to proper and quick help and assistance when your GP is closed. If they consider, you will be advised to go to a hospital where a doctor can take a look at your problem.
  3. If you need urgent medical assistance with acute life threatening illness or with injury, then call 112.
Specialist Doctor (Speciallæge):

Usually when we go to the local physicians AKA personal doctors, we are advised to contact the specialist, related to the problem you have. There is an online portal you can use to find the relevant doctor and make a call to book an appointment.

  1. Go to Syndhed website.
  2. In “Hvem/Hvad:” choose type of specialist you’re looking for (See list below for reference).
    [table caption=”List of Specialist”]Dansk, English
    Anæstesiolog/Smertebehandling, Anesthesiologist / Pain
    Børnelæge, Pediatrician
    Fysioterapi, Physiotherapy
    Gigtlæge, Arthritis Doctor
    Gynækologer, Gynecologists
    Kirurgi, Surgery
    Lunge/allergiklinik, Pulmonary / Allergy Clinic
    Neurokirurg, Neurosurgeon
    Neurolog, Neurologist
    Ortopædkirurg, Orthopedic Surgeon
    Psykiater, Psychiatrist
    Røntgen Klinik, X-ray Clinic
    Tandlæger, Dentists
    Øjenlæger, Ophthalmologists
    Ørelæger, Ear Specialists
    [/table]
  3. Under “Hvor:” you can enter the “postnummer” (i.e. post code) to find specialists close to where you live.

Please note that: In case of ENT (Ear, nose and throat) or Eye specialist, you can call the specialist directly. Otherwise, for all other cases, you need to ask your personal doctor for a referral. 

 

How to keep your important files save all the time

Problem: So you have files at your one laptop and you want to make sure:

  • You have backup of those files.
  • You can access those files even if you don’t have your laptop with you.
  • You want to access those files from your mobile phone.
  • You want to share those files between more than two laptops.
  • You want to share your files with other users.

This can be done by using any cloud services available i.e. Dropbox, Drive, OneDrive etc. They all provide free storage, enough to store important stuff.

  1. Lets start with Drive (Google). You need to have google account to use this service. You will get 15GB of free storage. You can buy more if you want.
  2. Download Drive on your Windows PC/Laptop from here Google Drive.
  3. Install the software on your computer.
  4. After installation, you will be able to see the link to Google Drive folder in the quick access650x281xhty41-650x281.png.pagespeed.gp+jp+jw+pj+js+rj+rp+rw+ri+cp+md.ic.lhcg5pbGrl.png
  5. Copy/Move all files you want to backup, in this folder. You can make subfolders to organize your data.
  6. Once copied, It will take sometime for these files to get uploaded (Depends on number of files you are adding). In case there are a lot of files, the best way is to move all stuff there and keep your PC connected for a night (Connected with power)
  7. Once synced (A process of uploading your files to the cloud and get changes from the server), you can either:
    1. More than one laptop: Install Drive on other computer, install Drive and get files on that computer.
    2. Away from your computer: Access your files from here: Google Drive
    3. Share files with other users: Go to https://drive.google.com/, right click on the file and folder, Press share and enter email address of person you want to share with.
    4. Access on Mobile: Downlod mobile apps, to access them from your mobile. iOS and Android

Important: If you will delete or edit files at one place, changes will reflect on all other devices.