Month: January 2013

Custom fonts in Android

2013-01-31 Android No comments

developers_design_typography

Using custom fonts in Android is quite simple, but requires to do some things programmatically and cannot be done using only XML file defining the view. Let’s have a look on an example.

Here we have a simple Android view file:

hint: if you cannot see the code below, please disable AdBlock on this site (I don’t have advertisements, but I’m using Gist)

We want to use custom font for the TextView element with id: custom_font. Firstly, we have to put our font (in this example: Custom_Font.ttf) into the ./assets directory (create it if it doesn’t exist yet) in the main directory of the project. Then, we can use the following code:

Please note, that Android does not fully support fonts in *.otf format, so it’s safer to use *.ttf format instead.
If we have font only in *.otf format, we can use one of the free font converters in order to obtain desired and proper file extension.

When we use the same font in whole application or a lot of elements in our project should use it, we can create custom TextView.
The only thing, we have to do, is to create an additional class extending default TextView class.

Note: isInEditMode() method is used for graphical preview of the UI in Eclipse IDE.

Afterwards, inside the view in the XML file, we should replace default TextView with our custom TextView like in the example below.

You can create other custom widgets respectively. E.g. MyButton, MyCheckBox, etc.

It can be useful, when you want to unify fonts in your application taking into consideration older versions of the Android.
Please note, that Roboto font was introduced in Android Ice Cream Sandwich, but luckily is free and available for download from official Google website.

Further reading & references:

Update: There is a library, which simplifies described process. Check it out here: https://github.com/ikocijan/MagicViews.

Easy conversion from binary to decimal numbers in C

2013-01-28 C No comments

Below, you can see simple and useful code snippet presenting conversion from binary to decimal number in C language without any sophisticated operations using only strtol function.

hint: if you cannot see the code below, please disable AdBlock on this site (I don’t have advertisements, but I’m using Gist)

Android & Maven

2013-01-24 Android No comments

Overview

Etymology

Maven, a Yiddish word meaning accumulator of knowledge.

What is Maven?

Maven is a tool that can be used for building and managing any Java-based project with all its dependencies (libraries).

Maven’s objectives

  • Making build process easy
  • Providing uniform build system (Project Object Model)
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features

Setting up Maven Android projects on MS Windows

Configuring environment

  1. You need Eclipse Indigo or Juno installed
  2. Run Eclipse
  3. Install Android Connector for Maven via the Eclipse Marketplace. Select Help -> Eclipse Marketplace… and search for android m2e.
  4. Click the Install button next to the Android Connector for Maven that appears and follow the path through the wizard dialog to install the plug-in and its dependencies (including the Android Development Toolkit and the Maven for Eclipse m2e plug-in). Accept the terms-and-conditions and click Finish.
  5. Create ANDROID_HOME environmental variable and assign location of the Android SDK into it.
      1. Click Start button
      2. Right click on the Computer and choose Properties
      3. Choose Advanced system settings
      4. Click Environment Variables…
      5. Click New below System variables window.
      6. Variable name should be: ANDROID_HOME.
      7. Variable value should be location of the directory containing Android SDK.
  6. Click OK and save your data.
  7. Go to the following website: http://maven.apache.org/
  8. Go to the download section (http://maven.apache.org/download.html) and download Maven (version 3.0.4 is preferred – Binary zip)
  9. Extract zip archive and copy apache-maven-3.0.4 directory into C: drive.
  10. Location of the extracted directory should be as follows: C:\apache-maven-3.0.4\
  11. Add C:\apache-maven-3.0.4\bin directory to the environmental variable called Path
    1. Click Start button
    2. Right click on the Computer and choose Properties
    3. Choose Advanced system settings
    4. Click Environment Variables…
    5. Find Path variable and and click Edit
    6. After semicolon (;) add path to the directory containing batch script inside the maven directory which should be as follows: C:\apache-maven-3.0.4\bin
    7. Click OK and save your data.
  12. Add platform-tools directory from Android SDK directory to the Path environmental variable respectively.
  13. Click Start button and then choose Run option (click Windows button + R key on your keyboard) and type: cmd.
  14. Type: mvn –v
  15. The returned output should be as follows:
    • Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)
      Maven home: C:\apache-maven-3.0.4\bin\..
      Java version: 1.6.0_33, vendor: Sun Microsystems Inc.
      Java home: C:\Program Files (x86)\Java\jdk1.6.0_33\jre
      Default locale: en_US, platform encoding: Cp1252
      OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
  16. Now Maven should be properly configured and prepared for deployment of the Android applications.

Importing, building and running sample Maven Android project

  1. Download samples from the stable branch on the website: http://code.google.com/p/maven-android-plugin/wiki/Samples
  2. Extract downloaded archive.
  3. Open Eclipse IDE.
  4. Import helloflashlight project (as an Android project).
  5. Right click on the project.
  6. Choose an option Configure -> Convert to Maven Project.
  7. Dependencies and project configuration are defined in pom.xml file.
  8. Click Start button and then choose Run option (click Windows button + R key on your keyboard) and type: cmd.
  9. Go to the directory, where the current project (helloflashlight) is located.
  10. Type in the command line: mvn clean install
  11. Command from previous point will create .apk file in the target folder.
  12. Type in the command line: mvn android:deploy
  13. Command from previous point will install the application via Maven on your Android device or Android emulator (virtual device). If more than one device is available, you can specify the relevant device in your pom.xml file. Maven can also start and stop an Android virtual device automatically for you.
  14. You can start the application via Maven by typing the following command: mvn android:run

Summary – Pros & Cons

Pros

  • Uniform build system.
  • No need to search for external libraries.
  • Good quality project information.
  • Transparent migration to new features.
  • Single project configuration for libraries in team work.

Cons

  • Libraries in the repository are not always up to date.
  • Project configuration takes more time.
  • Mistakes in pom.xml file are not detected by IDE, but can cause fail of the build.
  • Necessity of using CLI (Command Line Interface) in some cases.

Additional resources & references

  1. Maven reference book
  2. Maven reference book – Android Development Section
  3. Maven Android Plugin
  4. Vogella’s article about Maven & Android
  5. Eclipse plugin for Maven Android projects
  6. Maven repository

Redesigning Google

2013-01-24 Design, Google No comments

Redesigning Google

How Larry Page engineered a beautiful revolution? Article by The Verge.

Redesigning Google

5 exciting startups that haven’t launched yet

2013-01-21 Inspiration, Startups No comments

5 exciting startups that haven’t launched yet

Article worth reading brought by The Next Web.

TNW - Human.co

Fixing bug with disappearing map overlay after zoom in Android

2013-01-21 Android, Java No comments

In Android 3.0 – HoneyComb (API 11) or higher occurs specific bug connected with map overlay. When we draw overlay on the map (e.g. routes, directions or polylines) after zooming to particular level, overlay disappear unexpectedly. We can fix this bug very easily by disabling hardware acceleration for drawing overlays. Below, you can see exemplary code snippet with map injected by RoboGuice and disabled hardware acceleration. Changing way of drawing overlays should fix the bug.

hint: if you cannot see the code below, please disable AdBlock on this site (I don’t have advertisements, but I’m using Gist)

Adding reversed numbers

2013-01-21 Algorithms, Ruby No comments

In this post I will show you my approach to adding reversed numbers.
This problem occurred during the ACM Central European Programming Contest, in Prague in 1998.

General description of the problem

Input

The input consists of N cases (equal to about 10000). The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.

Output

For each case, print exactly one line containing only one integer – the reversed sum of two reversed numbers. Omit any leading zeros in the output.

Example

Sample input:

3
24 1
4358 754
305 794

Sample output:

34
1998
1

Solution

Exemplary approach

  1. In the first line of the input, we put number of the lines to process.
  2. In the next lines, we put numbers split by space, in which we should reverse order of the digits and then add them.
  3. In the final step, we should reverse order of the digits in the generated data and write result to the output.

Proposal of the solution in the Ruby language

hint: if you cannot see the code below, please disable AdBlock on this site (I don’t have advertisements, but I’m using Gist)

Launching brand new blog

2013-01-19 Uncategorized No comments

sunrise and the road

Hello! I’ve decided to create my own blog once again. This time I’m gonna write most of the posts only in English in order to make the articles more available and also to train my language skills (I’m Polish native speaker as some of you may already noticed). Template is clean and simple, because I’m going to focus on content – not the form. Stay tuned!