Genymotion. android emulator, that actually works.

Anyone who has developed Android application, knows the issue with their emulators. Slow and sluggish are the words that come to mind, when we talk about them, and that’s just getting started. Practically, they’re pretty much useless for any serious app development. Most developers prefer to develop and debug right on their devices, and skip the emulator completely. 

Ok, enough about them. Back to our point..

I recently came across Genymotion, which is an evolution of the popular AndroidVM project. This emulator will completely put all our arguments to rest. It is fast and snappy. Loads in about 10 seconds and works as good as a device. It also provides lot of preset device configurations, so you can emulate the popular devices on genymotion.

To make it work, you need to install VirtualBox on your machine, and then follow the instructions to setup a device configuration in minutes. You can also add plugins to your favourite editor (Intellij IDEA, in my case) to work with the virtual devices that you’ve setup.

That’s it. You’re ready to run your app in this shiny new emulator. Go ahead and give it a try. Strongly recommended.

Advertisements

Useful tools if you’re starting an android app.

There are a host of independent libraries which help us write android apps more quickly and in more maintainable fashion. These apps range from accessing View elements to Event Handling to Security and Database access. Here are a few of them which rate high in my opinion:

1. An actual smartphone

There’s nothing like it. I’ve seen few people code on emulator. I’d not recommend it because of its speed and irregularities. Start using the phone for your development. The productivity benefits are huge.

One handset would do fine for development. 🙂

2. Robotium

Simple test framework on Android.

3. Robojuice

It has few good utilities like Injection of resources/views and pojos. Also has a basic event framework.

4. Sugar

[I’m the developer here]. But seriously, if you have more than a few tables, its better to go with a tool that provides you some abstraction. Sugar is an effort to provide ActiveRecord like access to database Objects. Few similar tools that offer this feature are listed here..

5. IntelliJ IDEA and MAT

IntelliJ IDEA is the best Java editor out there. Android support comes with its community edition. Memory Analyzer Tool is an obvious choice for analyzing the heap dumps.

6. GreenDroid

It has some readymade widgets for use in Android. The code is fairly mature and useful. Good addition to your UI development.

This in no way is an exhaustive list.. I’ll try to keep adding to this list with some specific libraries for specific purpose.

[Update]

7. ActionBar Sherlock

Action bar is a great way to present navigations and user actions in Android apps. It was started with Honeycomb and is well received and extremely improved in ICS and Jellybean. ActionBarSherlock provides an easy way to make this design available on prior versions of android. Its very popular and a must-have library.

8. Sliding Menu

Its a nice little library to add sliding menu (like Google+, Facebook, etc.) to your application. It is very easy to integrate with and adds a touch of style to your app. Along with ActionBarSherlock, it adds a great look and ease of navigation to your app.

Do share if you have some recommendations.

Playing flash videos on android webview..

Was recently playing around with getting a youtube video to run on a webview in android. Was able to run it fine using the iframe but had issues getting some control over it. Same with ooyala videos.

Simple issues like video still playing once i leave the activity, on orientation change, etc..

It was rather simple in the end, coz webview exposes methods like onPause and onResume to provide control on the flash content being played in that webview.

Here are few things that you can do:

Handle your orientation. This would help keep the video running continuously even on orientation change. Modify your manifest like below.. and override onConfigurationChanged method to handle the orientation change.



<activity android:name:"MyActivity"

 android:label="@string/app_name"

android:launchMode="singleInstance"

android:configChanges="orientation|keyboardHidden">

In your activity, handle onResume, onPause and onDestroy and control the webView instance from there. Create the webview as presented in the method showVideo. It basically loads the youtube video in an iframe. You can use the link of embeddable version of youtube video.

 

 

Hope it helps someone.. Let me know if there’s any better way.

Save and Update with Sugar

This post helps demonstrate the ease with which you can save and update a record with the help of Sugar in android project.

For an introduction to Sugar, click here.

Firstly, to enable your model to utilize Sugar’s benefits, you need to have configured Sugar in the manifest file (as shared in the introduction) and the model needs to extend SugarRecord.

Saving a record becomes straight forward, as you’re only interacting with your objects and not the usually messy way of dealing with SQL queries directly. Here’s a way to save a record using Sugar:

// Note class represents model. It extends SugarRecord.
Note note = new Note(context);
note.setTitle("Make Tea");
note.setDescription("Add tea along with milk and sugar. Boil for few minutes");
note.save();

That is all you need to do to save a record into the android database. The save() method takes care of inserting the record into its corresponding table.
Similarly, modifying and updating a record is made very simple with Sugar. Here’s a way to do it.

// obtain a reference to the note record from database.
Note note = Note.findById(context, 1);
note.setDescription("Take some water. Add milk, tea and sugar. Boil it for few minutes");
note.save();

Here findById() is a query method to obtain the record with id 1 from the table. After getting the reference to the instance, modify the values and call the save() method to update the record into the table.

So, as you see, saving and updating records is made very straight forward with the use of Sugar. The code is located here  . Check out SugarExample for an example project.

Do try it out.

Sugar : Android database made easy.

UPDATE : Some of the code snippets in the below post are outdated. Please visit http://satyan.github.com/sugar/ for latest updates to the library.

This post is an introduction to Sugar library for android. The aim of the project is to simplify database access in Android.

The project draws inspiration from GORM for grails or Active Record for Rails. These patterns have been hugely successful in the web world, and have set a benchmark for libraries for database access.

The basic objectives of the library  are:

1. Creation and management of database and connection.

2. Simple and Intuitive API for database querying.

3. Simple CRUD operations.

Source Code

Source code is present at https://github.com/satyan/sugar

You can clone it and start hacking. There’s a build script along with it to compile and generate the binary.

An example project using the library is present at https://github.com/satyan/SugarExample

Usage

Android Manifest file: 

Include the android:name attribute in the application tag. SugarApp extends android’s Application and initiates the database creation for the project. By including the attribute, you’re basically starting with SugarApp instead of android’s Application class. If you intend to write your own Application class, you can extend SugarApp.

<?xml version="1.0" encoding="utf-8"?>
<manifest..>
<application.. android:name="com.orm.SugarApp">
.....
</application>
</manifest>

Entities
The entities need to inherit from SugarRecord including a typed parameter. SugarRecord provides for all the CRUD methods that will be available to the entity classes.

public class Note extends SugarRecord<Note>{
private String title;
private String description;
public Note(Context context){
super(context);
}
...
}

Thats about it. Configuration is done. You’re ready to dive into the sweetness of Sugar. (OK.. I couldn’t stop myself from saying this.)

# inserting values

Note note = new Note(context, "Note 1");
note.save();

# query

Note.findById(context, Note.class, 1);
Note.find(context, Note.class, "title=?", new String[]{"Note 1"});

# delete
Note note = Note.findById(context, Note.class, 1);
note.delete();

# few more..

Note.listAll(context, Note.class);
Note.deleteAll(context, Note.class);

Nice.. All done. Happy Android hacking.