Custom Search

dimanche 30 décembre 2012

Intellij Idea key scheme for Eclipse

Most of the time I use Intellij Idea Community Edition for Android development, but recently I had to switch to Eclipse for some reasons. It could be really painful because I really got used to Intellij Idea default key scheme (pdf). But, thankfully, there is simple and elegant solution for this by Santhosh Kumar

Just download the jar file and place it into plugins folder in your eclipse folder. Than restart eclipse. In eclipse go to Window -> Preferences (Windows) or ADT -> Preferences (Mac) than to General -> Keys and in the "Scheme" droplist choose "Intellij Idea" and press apply. That's it! Now you have the default Intellij Idea Keymap in your Eclipse workspace.

Links:

Developing Notepad Application - Part 1 (Creating SQLite database)

Application minimum required SDK version - 14 (ICS)

Contents

  1. Intro
  2. Creating SQLite database
  3. Accessing SQLite database from ADB shell.
  4. Links

1. Intro

In this tutorial we will develop a simple notepad Application for Android that allows you to create, read and edit text notes. To understand this tutorial you've got to have basic knowledge about relational databases and SQL.

The app and the idea are not mine, almost all of the code is taken from Android developers web site. The tutorial there is just awesome but there are some topics that I would like to highlight in more detail. There are also some things that are deprecated and I will replace them with new implementations and point out where it happens. 

We will start from a scratch and add some functionality step by step. Before reading this tutorial I highly recommend to look through the original article from Android web site.

In this tutorial we are going to create a very simple database that will serve us as data storage for our app.

At the end of this tutorial you will know how to:
  • create a simple SQLite database in Android.
  • get access to SQLite database via the command prompt (terminal) and execute some simple queries.

2. Short facts about SQLite database

First, some facts about SQLite in general:
  • SQLite is free and open-source embedded SQL database engine. 
  • SQLite reads and writes directly to ordinary disk files. 
  • A complete SQL database is contained in a single disk file. 
  • The database file format is cross-platform.
  • It supports standard relational database features including syntax, transactions and prepared statements. Transactions are ACID.

Database Structure

Our database will contain only one table and the following columns to persist notes:
  • title -- text (note title)
  • body -- text (note body text)
  • _id -- integer, autoincrement (the id of the note)

Creating the Database

Let's create a new project with default settings which contains a single activity called NotesListActivity. Now in the same package with our activity create a class called NotesDbAdapter which will contain a subclass of SQLiteOpenHelper as a subclass which is a class to manage database creation and it's versioning. NotesDbAdapter will grant us an access to our database by defining CRUD methods in it. So let's create the wrapper class for our database and define all the constants we need and static inner class to get a database connection.
NotesDbAdapter.java
package com.blogspot.android_by_example.notepad;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class NotesDbAdapter {

public static final String COLUMN_TITLE = "title";
public static final String COLUMN_BODY = "body";
public static final String COLUMN_ID = "_id";

public static final String TAG = NotesDbAdapter.class.getSimpleName();
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static final String DATABASE_NAME = "notes.db";
private static final String TABLE_NAME = "notes";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null);";

private final Context mContext;

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
Log.d(TAG, "onCreate() database");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
Log.d(TAG, "onUpdate() database");
}
}

public NotesDbAdapter(Context context) {
this.mContext = context;
}

public NotesDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mContext);
mDb = mDbHelper.getWritableDatabase();
return this;
}
}
The code looks quite big but actually there is nothing special here. We create static subclass of SQLiteOpenHelper and implement abstract methods onCreate() and onUpgrade(). The onCreate() method is invoked when there is no database available for our app, so in this method we have to execute all the statements to create our database. onUpgrade() is invoked when the version of the current database version is smaller than new version (database version is provided when SQLiteOpenHelper subclass instance is created and this version is passed as a parameter to a superclass constructor).

NotesDbAdapter is a wrapper class that holds the instance of SQLiteOpenHelper and provides API for database access (We will define basic CRUD operations in the next tutorials).

Now we need to create an instance of our NotesDbAdapter in our activity an call open() method to create the database. That's all the code we need to have a working database.

NotesListActivity.java
package com.blogspot.android_by_example.notepad;

import android.os.Bundle;
import android.app.Activity;

public class NotesListActivity extends Activity {

private NotesDbAdapter mDbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_list);

mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
}
}

3. Accessing SQLite Database from ADB Shell

Now we need to make sure that our database is created and in a working state. Let's open DDMS perspective in Eclipse (or launch monitor from tools folder in your SDK location) and locate the database file on the device file system.

Emulator/device instances store SQLite3 databases in the folder:
/data/data/<package_name>/databases/<database_name>
So in our case the database will be located at:
/data/data/com.blogspot.android_by_example.notepad/databases/notes.db
Database file location
Database file location
To issue SQL queries to your database enter a remote shell on the emulator instance and enter sqlite3 command following with the full path to our database. Here is an example:
C:\Users\Taras Osiris>adb shell
root@android:/ # sqlite3 /data/data/com.blogspot.android_by_example.notepad/databases/notes.db
.android_by_example.notepad/databases/notes.db <
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

On Windows to acces ADB directly from command prompt add path of your SDK to your environment variables. On Mac OS follow this instructions.

If you have only one USB device or emulator connected you don't need to specify what device is the target for your commands. Otherwise you need to direct your commands to the targeted device by specifying arguments after adb command. For more details read about issuing ADB commands.

adb [-d|-e|-s <serialNumber>] shell

Now you can execute your SQL queries directly to your database. Let's insert a few rows into our database and than query all of them.

sqlite> INSERT INTO notes (title, body) VALUES ("Title1", "Body1");
INSERT INTO notes (title, body) VALUES ("Title1", "Body1");
sqlite> INSERT INTO notes (title, body) VALUES ("Title2", "Body2");
INSERT INTO notes (title, body) VALUES ("Title2", "Body2");
sqlite> INSERT INTO notes (title, body) VALUES ("Title3", "Body3");
INSERT INTO notes (title, body) VALUES ("Title3", "Body3");
sqlite> SELECT * FROM notes;
SELECT * FROM notes;
1|Title1|Body1
2|Title2|Body2
3|Title3|Body3

As you can see, everything is great, our database is fine and ready to use. In the next part of tutorial we are going to work on ListActivity that will display a list of our notes from database.

Links and Materials Used

samedi 22 décembre 2012

Using ArrayAdapter (Creating list or spinner with a set of predefined data)

Application minimum required SDK version - 8

Contents

  1. Preface
  2. When should ArrayAdapter<T> be used?
  3. Using ArrayApater<T> default implementation in practice
  4. Conclusion
  5. Links

1. Preface

For this article to be useful, basic knowledge about Adapter objects in Android is required. 

For those readers who know Russian, have a look at this article - Android Adapters Overview (rus).

Unfortunately, I haven't found an article of the same type in English, that's why I am going to write an overview of Android adapters soon.

In this post I will make a brief overview of the simplest default Android Adapter implementation - ArrayAdapter<T>

android.widget.ArrayAdapter<T> is a concrete class that can be instantiated with the help of one of the six constructors or using static factory method (Effective Java. Item 1)  

2. When should ArrayAdapter<T> be used?

A parameterized class ArrayAdapter<T> is a very simple adapter implementation. 

The default implementation is used when you have an array or List of objects of any type and you need to show user their string representation in the form of ListView, Spinner, GridView or any other subclass of AdapterView. For string representation of an object, this adapter will use toString() method. 

By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource. (Documentation)

3. Using ArrayAdapter<T> in practice

List of planets
ListView and Spinner of planets
Suppose we need to display a list of planets in our application (As a basis I took the API guide on developer.android.com about Spinner). The simplest case is when the set of data we need to display is predefined (we exactly know how many items we want to show and have their string representation). In this situation our set of data should be defined in the resource file and static factory method createFromResource() should be used. 

Create a new project with a blank activity.

First, let's define our data set in res/values/strings.xml resource file:
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>

</resources>
Now let's create a layout file (res/layout/activity_planets.xml) which contains a single ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listViewPlanets"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>
And finally our PlanetsActivity.java :
package com.blogspot.android_by_example;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PlanetsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_planets);

ListView listViewContacts = (ListView) findViewById(R.id.listViewPlanets);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, // context
R.array.planets_array, // source of data
android.R.layout.simple_list_item_1); // layout file to make views
listViewContacts.setAdapter(adapter);
}
}
And don't forget to include the activity into your AndroidManifest.xml file.

That's the whole code for creating a simple list of items. In this case the created adapter takes the array of data declared in the resource file and builds each item in the list using the default layout for the Android list item.

The same result can be achieved by using constructor. It accepts array of objects (T[]) as in the code above or it can receive a List<T> of objects. In our case the code gets a little bit more verbose because of fetching the array from application resources. Note that in the code below the constructor receives a List of Strings.
package com.blogspot.android_by_example;

import java.util.Arrays;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PlanetsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_planets);

ListView listViewContacts = (ListView) findViewById(R.id.listViewPlanets);
String[] planets = getResources().getStringArray(R.array.planets_array);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, Arrays.asList(planets));

listViewContacts.setAdapter(adapter);
}
}
Note that in this example ListView can be substituted by any AdapterView (Spinner, GridView)

4. Conclusion

  • ArrayAdapter<T> is a concrete class. It should be used when we need a string representation of data in array or List is required. You can subclass it if the default implementation lacks functionality.
  • To instantiate an ArrayAdapter<T> object one of the constructors can be used. Or you can use static factory method createFromResource(), when data set is in the application resource file. In each case you have to provide ArrayAdapter<T> an object with the data set for string representation and a layout file for making a View
  • We've developed the most simple application that shows a list of planets using ArrayAdapter<T>.

mercredi 25 juillet 2012

Jelly Bean: Accessibility Gestures Explained

Jelly Bean: Accessibility Gestures Explained

1 Jelly Bean: Accessibility Gestures Explained

This article details accessibility gestures in Jelly Bean and is a follow-up to Jelly Bean Accessibility Explained. It gives a conceptual overview of the 16 possible gestures and describes how they are used. The interaction behavior described here holds for all aspects of the Android user interface, including interactive Web pages within Chrome and the Android Web Browser.

1.1 Conceptual Overview Of The Gestures

Placing a finger on the screen speaks the item under the finger by first placing Accessibility Focus on that item. Moving the finger triggers touch exploration which moves Accessibility Focus.

To generate any of the Accessibility Gestures discussed below, one moves the finger much faster — how much faster is something we will tune over time, and if needed, make user customizable.

To remember the gestures, think of the four directions, Up, Down, Left and Right. In addition to these four basic navigational gestures, we defined an additional 12 gestures by picking pairwise combinations of these directional flicks, e.g., Left then Down — this gives a total of 16 possible gestures. In what follows, left then down means swipe left, and continue with a down flick. Note that in performing these additional gestures, speed matters the most. As an example, it is not essential that you make a perfect Capital L when performing the Down then Right gesture for instance; speed throughout the gesture, as well as ensuring that the finger moves some distance in each direction is key to avoid these being misinterpreted as basic navigational gestures.

1.2 Accessibility Focus And Accessibility Gestures

Accessibility Focus is moved using the four basic directional gestures. For now we have aliased Left with Up, and Downwith Right; i.e., both Left and Up move to the previous item, whereas Down and Right move to the next item. Note that this is not the same as moving with a physical D-Pad or keyboard on Android; the Android platform moves System Focus in response to the D-Pad. Thus, moving with a D-Pad or trackball moves you through the various interactive controls on the screen; moving Accessibility Focus via the Accessibility Gestures moves you through everything on the screen.

1.3 Accessibility Gestures For Common Actions

In addition to the basic navigation describe above, we define the following gestures for common actions:

Navigation Granularity
You can increase or decrease navigation granularity by rapidly stroking Up then Down or Down then Up.
Scrolling Lists
You can scroll a list forward by rapidly stroking Right then Left; the reverse, i.e., Left then Right scrolls the list backward by a screenful.

1.4 User Configurable Gestures

Gestures Down then Left, Up then Left, Down then Right and Up then Rightare user configurable; their default assignments are shown below.

Back
Gesture Down then Left is the same as pressing the Back button.
Home
Up then Left has the same effect as pressing the Home button.
Status Bar
Gesture Up then Right opens Status Notifications.
Recent
Down then Right has the same effect as pressing the Recent Applications button.

1.5 Summary

Gestures for manipulating and working with Accessibility Focus are an evolving part of the Android Accessibility; we will continue to refine these based on user experience. At this point, you are probably saying:


But wait, you said 16 gestures, but only told us the meanings of 12 of them

You are correct — we have left ourselves some gestures to use for future features.

mardi 24 juillet 2012

Jelly Bean Accessibility Explained:Touch Exploration Augmented By Gestures

Jelly Bean Accessibility Explained:Touch Exploration Augmented By Gestures

1 Jelly Bean Accessibility Explained:Touch Exploration Augmented By Gestures

We announced a number of accessibility enhancements in Android Jelly Bean — see our Google IO 2012 announcements and our Android Accessibility talk from I/O 2012. This article gives a user-centric overview of the Jelly Bean interaction model as enabled by touch exploration and navigational gestures. Note that as with every release, Android Accesssibility continues to evolve, and so as before, what we have today is by no means the final word.

1.1 High-Level Concepts

First, here's some shared vocabulary to ensure that we're all talking of the same thing when explaining Jelly Bean access:

Random Access
Enable user to reach any part of the on-screen UI with equal ease. We enabled this as of ICS with touch exploration.
Deterministic Access
Enable user to reliably land on a desired item on the screen. We enable this in Jelly Bean with linear navigation.
Accessibility Focus
The item that the user most recently interacted with — either via touch exploaration or linear navigation receives accessibility focus.
Activation
User can activate item having accessibility focus by double-tapping anywhere on the screen.

1.2 Sample Interaction Scenarios We Enable

The combination of random access via touch exploration, backed up by linear navigation starting from the point the user just explored enables users to:

  • Touch explore an application to understand its screen layout,
  • Use muscle memory to quickly touch parts of the display to access familiar application screens,
  • Use linear navigation to reach the desired item when muscle memory is wrong by a small amount.

As an example, when using the Google Play Store, I can use muscle memory with touch exploration to find the Search button in the top action bar. Having found an application to install, I can once again use muscle memory to roughly touch in the vicinity of the Install button; If what I touch is not the Install button, I can typically find it with one or two linear navigation steps. Having found the Install button, I can double-tap anywhere on the screen.

The same use case in ICS where we lacked Accessibility Focus and linear navigation would have forced me to use touch exploration exclusively. In instances where muscle memory worked perfectly, this form of interaction was highly effective; but in our experience, it also tended to lead to breakdowns and consequent user frustration in instances where users almost found the control they were looking for but missed by a small amount.

Having introduced accessibility focus and linear navigation in Jelly Bean, we decided to eliminate the ICS requirement that the user tap on or near a control to activate it — we now enable users to activate the item with accessibility focus by tapping anywhere on the screen. To eliminate spurious taps --- especially on tablets, we made this a double-tap rather than a single tap. Note: based on user experience, we may optionally bring back single tap at some point in the future as an end-user customization.

1.3 Summary

Android Accessibility continues to move forward with Jelly Bean, and will continue to evolve rapidly along with the platform. Please use the Eyes-Free Google Group to provide constructive feedback on what works or doesn't work for you --- what is most effective is to objectively describe a given use case and your particular experience.

vendredi 29 juin 2012

What's New In Google Accessibility From Google I/O 2012

Google IO 2012: What's New With Google Access

1 Google IO 2012: What's New From Google Access

We showcased a number of exciting advances in accessibility on Android and Chrome during IO 2012. With these advances, blind and low-vision users can leverage Google applications and services on Android and Chrome to collaborate effectively with their peers and to obtain on-the-go access. Advances include out-of-the-box access on Android (JellyBean), a new set of gestures that enable fluent interaction on touch-screen devices, Braille support on Android, an extension framework for ChromeVox, and a new, high-quality voice for use with Web applications on Chrome.

1.1 Enhanced Android Accessibility In JellyBean:

  • Accessibility on Android can be enabled by long-pressing with two fingers (4 seconds) the setup screen to enable out-of-the-box access for blind users.
  • Touch exploration has been enhanced with simple gestures that enable users navigate on-screen contents.
  • JellyBean provides a set of Accessibility Actions that can be called from any AccessibilityService such as TalkBack; it also provides early support for Braille displays.
  • Touch exploration and gesture navigation both set AccessibilityFocus —

double-tapping anywhere on the screen activates the item with AccessibilityFocus .

  • TalkBack now has a sister service BrailleBack for providing Braille support on Android.
  • Chrome on Android is now accessible and supports the latest in Web Access standards.

With these enhancements in Android access, blind users can use a combination of touch exploration and navigational gestures to access any part of the Android user interface.

As an example, I typically use the Android Play Store by touching the screen around the area where I expect a specific control; quick flicks of the finger then immediately get me to the item I want. With these touch gestures in place, I now use touch exploration to learn the layout of an application; with applications that I use often, I use a combination of muscle memory and gesture navigation for rapid task completion.

1.2 Chrome OS On Chrome Books And Chrome Box

Chrome OS comes with ChromeVox pre-configured — ChromeVox is our Web Accessibility solution for blind users. With the new high-quality voice that is being released on the Chrome Webstore, ChromeVox now provides smooth spoken feedback in Chrome on all desktop environments. Finally, we announced a flexible extension framework that enables Web developers leverage ChromeVox from within and outside of their own Web applications to provide rich, contextual spoken feedback.

1.3 Developer Tools For Ensuring Accessibility

To help developers better leverage Web Accessibility, we are releasing a new Accessibility Audit tool that enables Web developers detect and fix commonly occuring accessibility errors. This tool has been integrated into Chrome's Developer Tools and helps Web developers ensure accessibility while working within their normal workflow.

1.4 Accessibility Related Presentations At Google I/O 2012

Catch these on Youtube in the next week if you weren't able to attend I/O this week.

  • Android Accessibility (T. V. Raman,Peter Lundblad, Alan Viverette and Charles Chen).
  • Advanced Web Accessibility (Rachel Shearer, Dominic Mazzoni and Charles Chen).
  • What's New In JellyBean: Android Team.
  • JellyBean announcement in the Wednesday keynote.

Date: 2012-06-21 Thu

Author: T.V Raman

Org version 7.8.11 with Emacs version 24

Validate XHTML 1.0