Month: March 2014

Genymotion – really fast Android emulator

2014-03-15 Android No comments


For a long time I was struggling with very slow Android emulators. Actually, it was impossible to work comfortably with using them, so most of tests I’ve performed, were on real devices. Of course, I can’t test mobile application on every of thousands mobile devices which runs on Android system. Some time ago, I tried to do some experiments with running Android as a virtual machine on Virtual Box. It was faster than emulators provided with SDK, but still, it was slow. Recently I’ve found Genymotion project. Actually Genymotion provides virtual machines with Android OS, which runs with Virtual Box. It seemed to be the same solution I’ve tried before, but it provides all-in-one package with optimized images of Android OS and predefined configurations of various devices. It’s nothing special. One thing, which truly amazed me, was the speed of this emulator. It is really fast! Actually it runs as fast as Android OS on real device! With such tool, debugging with emulator finally makes sense and can be comfortable. One of the useful features is the possibility to attach real device, pass data from its accelerometer to the computer and emulate it on virtual machine. You can use it for free for your personal use with basic functionality. If you want to use it for commercial purposes or you want to have additional features like recording screen casts, then you have to buy the license. You can also install IntelliJ (Android Studio) plugin, which integrates Genymotion with your IDE.

How to highlight and click on ListView item in Android programmatically?

2014-03-06 Android No comments

There are many approaches concerning selecting items on Android’s ListView. Unfortunately most of them don’t work. I was struggling with this problem for some time and decided to publish my solution, which is actually quite easy.

The problem

I want to highlight item on a ListView programmatically (in a source code – without touching item on the screen).
I also want to click on item of the ListView programmatically.


We have to create our own adapter. Let’s call it ContactsAdapter.

Important methods are:

  • View getView(int position, View convertView, ViewGroup parent) – returns View of the single list item on the given position
  • void highlightItem(int position, View result) – highlights item on a given position (sets proper background)
  • public void setSelectedItem(int selectedItem) – sets item to highlight

Now, we have to set our adapter in a ListView in activity.

In this example, highlightListItem method will highlight chosen item on a ListView available in a variable contactListView.

Clicking programmatically is much more easier, than highlighting item. We can simply use the method below assuming, that we properly created contactListView attribute and assigned it to appropriate view.

How to read contacts in Android device using ContentResolver?

2014-03-02 Android 4 comments

With Content Providers we can access data stored by Android system applications. Popular example of using Content Provider is retrieving contact list from the smartphone. We can also access Calendar or create our own Content Provider. More information about that can be found in documentation of Android. If we want to access Content Provider, we can use Content Resolver in our application’s context.

In this example, we will access contact list. First of all, it’s good to create Contact entity class, which will be model of our data.

We can access application’s context through GenericApplication class extending Application class.

We can use static application’s context in many situations. Nevertheless, we should avoid this practice when we work with UI and Views, because we may have problems with styles, look & feel of the application and other issues.

Now, we can create ContactsProvider class (we can call it as we want).

We can filter our data, by manipulating selection parameter. E.g. if we want to retrieve only starred contacts, we can change “selection” parameter in the following way:

If we want to retrieve more or less data, we can manipulate “projection” parameter. ContentResolver is just kind of proxy for SQLite databases available in Android applications, which allows us to access this data in a safe way. We can use SQL language in order to select proper data. Check Android documentation for more details.

We also need to remember to add proper parameter to our AndroidManifest.xml file.

If we want to use GenericApplication class, we should also add proper attribute to “application” tag in AndroidManifest.xml file.

It’s not necessary. In this example, we can pass activity context instead, if we want to.

That’s it! Now, we can use our class to read contacts very easily. We can even create an adapter and display contacts on the list or do whatever we want to.