Specify Roboto font in Android

There is a pretty nice font in Android called Roboto. Introduced in version 4.1 and enhanced in 4.2 and 5.0. Specimen and download is available at android design guidelines. What’s kinda missing is the way how to use the Roboto font in your app.

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light"     // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-thin"      // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium"    // roboto medium (android 5.0)

Combine above with font weight specification and you will get all the possible variants:

android:textStyle="normal|bold|italic"

Android Tutorial – Square Layout

For the latest app I’ve developed, I needed to use squared layout. It would be easy with fixed size – I would put the height (width) dimension into the dimens.xml file and it would work. However that wasn’t what I needed.

The goal is to have layout which will be squared with width (or height) set up to match_parent.

And because this is not part of the Android SDK, we are going to create it. Firstly create new Java class in your project. I store these files in src/main/java/com.example.something/tools but you are free to choose your own destination 😉 Name it SquareLayout.java.  This file will be extending the LinearLayout, so I will just paste the complete code, it’s pretty simple to understand.

public class SquareLayout extends LinearLayout {

    public SquareLayout(Context context) {
        super(context);
    }

    public SquareLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        // this is the scale between width & height, for square should be == 1
        int scale = 1;

        if (width > (int)(scale * height + 0.5)) {
            width = (int)(scale * height + 0.5);
        } else {
            height = (int)(width / scale + 0.5);
        }

        super.onMeasure(
                MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
        );
    }
}

Well done. Now we can use this new layout in your layout files. And it works like this (don’t forget to change com.example.something to your namespace):

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_margin="60dp"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <com.example.something.SquareLayout
        android:background="@color/pink"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        </com.example.something.SquareLayout>

</RelativeLayout>

And it should look like this screenshot:

android-square-layout-tutorial-example

Poker Incomes Reports – android app for casual poker players

Are you a non-pro poker player and do you want to track your winnings (or loses) in poker? This is the app for you. Poker Incomes Reports let’s you track your poker incomes and divide them into the three main categories – tournaments, cash games and online poker.

 

ButterKnife inject views in Fragment

I’ve lost a lot of time to figure it out. I had in onCreateView() in my Fragment:

 ButterKnife.inject(this, rootView);

and it wasn’t working (nullpointerexception), so I was testing what’s wrong and could not fixed it, because it looked allright. And the solution is simple as always. This is the solution for Android Studio (for Eclipse check this)

Open File -> Settings (ctrl + alt + s). Navigate to Compiler -> Annotation Processors and at the top of the window check the Enable annotation processing. That’s it.

butterknife-fragment-inject-android-studio

Android hackathon

Yesterday I was watching last episode of Sherlock and suddenly my phone rang. My friend called me to tell me that I have to join him in the pub to solve some sort of programmers problem. So I did. The problem was that he was suffering of lack of programmers.

Long story short, he asked me to help him with an android app. Firstly he had planned to use Titanium SDK but he found out that this is not working solution for him after all and he has to ship the app for iOS and Android asap. Actually the deadline has been missed already.

So we made a deal – I will try to develop an android app and he will be working on an iOS version at the same time. The latest possible deadline is 14 days.

So my android hackathon started. I got designs today and worked on the layouts and the basic functionality. And prepared most of the resources. Tomorrow (day 3) I would like to implement  something more complicated (there’s an augmented reality in the app, so I will be coding camera stuff).

Will post more about implementation later, there’s a lot of interesting things to share.

Android Studio – Could not create plugin of type ‘AppPlugin’

Problem

I’ve updated Android Studio & SDK, then updated build.gradle to be using latest build tools and gradle plugin. And because IDE showed me some errors about using auto import for gradle, I’ve decided to install latest gradle and use it locally. But when I tried build my project I got this error.

A problem occurred evaluating project ':Project'.
> Could not create plugin of type 'AppPlugin'.

 Solution

Don’t use latest Gradle (version 1.10), downgrade to 1.9.

Android Studio not using built in SDK

If you download Android Studio you’re probably ready to develop the apps instantly, because there is built in SDK (android) in the software. That’s sweet for new developers, not for me, because I have used Eclipse and sometimes I run this IDE too. And that means I already downloaded SDK, unzipped it in different path, download all important images and set up all virtual devices.

And there’s a problem. Even if I set up Android studio to use my SDK path, it sometimes doesn’t work and it uses Android Studio path (../android_studio/sdk/). That means I have to create demanded virtual device (and sometimes download correct API version images). And because Android Studio is EAP, the settings might not work and sometimes I’m not able to change the path.

Here’s the quick fix. Delete the SDK folder from Android Studio folder and create junction (in Windows). That means run command prompt (press button with windows logo at keyboard, type cmd, press enter) and type in this command:

MKLINK /J [destination path] [sdk path]

In my case it was:

MKLINK /J C:\Java\android_studio\sdk C:\Java\sdk

Here we are, problem succesfully solved.

junction

Android Studio – connection error

If you are experiencing a connection error trying to update Android Studio ( – Connection error. Please check your network connection and try again), you probably googled that’s an error in Android Studio.

connection-error.fw

Quick fix

Just add the following line to .vmoption file. It is located in your installation path (aka C:\Program Files (x86)\Android\android-studio\bin). There are two files you should check: studio.exe.vmoptions and studio64.exe.vmoptions.  Change the one you are using or both just to be sure.

-Djava.net.preferIPv4Stack=true

or you prefer IPv6 (if you don’t know what IPv6 is, just use the first line)

-Djava.net.preferIPv6Addresses=true

 Still not working?

Try to run your Android Studio directly from installation path (C:\Program Files (x86)\Android\android-studio\bin) by clicking studio.exe or studio64.exe and update the Android Studio then. After update you are able to run Android Studio the usual way.

Android studio – Project Wizzard

I’ve prepared some screenshots from Android studio’s project wizzard, as you can see, the functionality is the same like in Eclipse.

Android studio is better than Eclipse, you should try it. It’s faster, smoother, not ugly and have nice dark theme (darcula).