Tag Archives: Android

Snippet to determine a size of control in Android

It is a common task to get a control size. You may need it to create image assets to customize that control.

First we get a control object, for example ToggleButton. Then we get a pixel density of the device. After that we can convert px units to dp units.

Android Java Serialization Using Generics

I have created a class – SerializationManager, it can serialize any objects and write them to files. It can be used to save and restore form states in Android. It is based on Java Generics. What is achieved by this is that you do not need to duplicate serialization code in every class.

images

Object must implement Serialized interface. Here form is an object instance of a class. It can be named as just obj.

This is how you can use it. First you declare a SerializationManager in your class and you tell it which class you are going to serialize:

Then in onCreate method you create SerializationManager instance:

Writing object to a file:

Reading objects from saved file:

A little joke for reading to the end:
java generics

Serialize an object, save it to a file and read it from a file in Android

Android interface Serializable is not sufficient to save your objects to the file, it only creates streams.

This is a complete code with error handling and handling closes of streams. Add it to your class that you want to be able to serialize and deserialize.

In my case the class name is CreateResumeForm. You should change it to your own class name.

Use it like this in your Activity:

On TextView Hints and StringUtils.isBlank

I had a problem caused by setting empty or blank strings to the TextView and AutoCompleteTextView – hints were invisible after that. So I decided to check strings, that are set, if they were not only empty, but also not blank. I used StringUtils.isBlank instead of StringUtils.isEmpty: it works much better now, and hints are always visible. If you don’t still use StringUtils, I highly recommend you to do. This is a Gradle import for a library that contains them:

And another example with AutoCompleteTextView:

Here is some information on how isNotEmpty and isNotBlank are working.

Title of this article: On Android TextView and AutoCompleteTextView Hint and StringUtils.isBlank and .isEmpty

Gradle build file for a project with Android Annotations

There is a sample Gradle build file for a project with Android Annotations. This is pretty complicated stuff, that was achieved with a lot of my blood, so I decided to post it here, so that you can use it to try to find your mistakes. Android Annotations library is loaded with apt. The position of every setting is important. There is also a trick to avoid Lint errors and a custom task to run app via command line.

I used Gradle 1.12 and Android Studio 0.8.4 with Android SDK 20, Android Build Tools 20.0 and Android Plugin 0.12.+.

Running Android app on device using Gradle

Here I will show you, how I managed to build and run an Android app via command line (Terminal, since I use Mac) using Gradle and how I start a resulting app on the device (Google Nexus 5 with Android 4.4.2) automatically. I had to solve two major problems to achieve this.

The first problem is with Gradle version: last version of Gradle is not compatible with the last version of Android Plugin.
The second problem is with custom task to run app on device: you have to put the correct package and activity names.

The final result looks like this:

Where Multiverse is the root folder of my project and the name of the project, appStart – custom Gradle task that builds and runs an app on device.
Gradle loads all dependencies, builds app, makes a lint check and starts and app on your device automatically. Yahoo! This is how it looks on Terminal:

However, I should notice, that this works slower than just running via Eclipse: as you see it takes at least 13 seconds for a little project on Macbook Pro Retina Display Late 2012.

First of all you need to install a correct version of Gradle. If you just download the latest version, your Android plugin will not work. You have to get a version, for instance, 1.10, not 2.0. Also you need to setup PATH variable, so that you will have bin folder of Gradle and android sdks tools there:

Then I created a gradle build file:

Here com.alwawee.multiverse is the package name of the application.
And com.alwawee.main.MainActivity is the full Activity name, that the application starts from. This is how my project tree looks like:
Снимок экрана 2014-07-28 в 1.51.29

This is a part of the Manifest file:

At the end there is a custom task appStart. It is important to correctly write the package name before class name. I thought that the package name is the package name for MainActivity class. I was wrong. It’s a name for a whole app’s package name. You can find it at the root node of your Manifest file.

Notes on commented code: use abortOnError false if you have any serious problems with lint and don’t want to solve them. Uncomment the line about Windows, if you use Windows.

This is a StackOverflow question, I asked to reach this goal.

How to add shortcut to Home screen in Android programmatically

I improved a little bit a solution that was found in StackOverflow. Now program saves in preferences whether a shortcut was already added and doesn’t add it in new launches of an app if user deleted it. This also saves a little bit time, since the code to add an existing shortcut doesn’t run anymore.

In Android Manifest:

Open-Source Expandable Selection Tree for Android

Today I have published a new open source project on GitHub, called DKExpandableSelectionTree. It is a better expandable list view for Android that allows user to select multiple items from a tree of objects. You can clear all selections, you can select and deselect multiple items by selecting a whole group. It also displays a number of selected items inside a group in parentheses after the name of a group.

This is how it will look in a sample project, when you clone the repository and run it:
Screenshot_2014-05-27-19-22-47

This is how it looks in my project:
Screenshot_2014-05-27-19-16-17