alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (GesturesListActivity.java)

This example Android source code file (GesturesListActivity.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

adding, android, app, arraylist, cursor, gesture, gesturelibrary, gestureoverlayview, gestureslistactivity, listactivity, ongestureperformedlistener, os, override, provider, reloading, removing, simplecursoradapter, string, ui, widget

The GesturesListActivity.java Android example source code

package com.example.android.gestures.demo.list;

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.provider.Contacts;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class GesturesListActivity extends ListActivity implements OnGesturePerformedListener {
    private GestureLibrary mLibrary;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Populate the activity with the names of our contacts
        Cursor query = managedQuery(Contacts.People.CONTENT_URI,
        		new String[] { Contacts.People._ID, Contacts.People.DISPLAY_NAME },
        		null, null, Contacts.People.DEFAULT_SORT_ORDER);

        ListAdapter adapter = new SimpleCursorAdapter(this,
        		android.R.layout.simple_list_item_1, query,
        		new String[] { Contacts.People.DISPLAY_NAME },
        		new int[] { android.R.id.text1 });
        
        setListAdapter(adapter);

        mLibrary = GestureLibraries.fromRawResource(this, R.raw.actions);
        if (!mLibrary.load()) {
        	finish();
        }

        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
        gestures.addOnGesturePerformedListener(this);
    }

	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
		if (predictions.size() > 0) {
			if (predictions.get(0).score > 1.0) {
				String action = predictions.get(0).name;
				if ("action_add".equals(action)) {
					Toast.makeText(this, "Adding a contact", Toast.LENGTH_SHORT).show();
				} else if ("action_delete".equals(action)) {
					Toast.makeText(this, "Removing a contact", Toast.LENGTH_SHORT).show();
				} else if ("action_refresh".equals(action)) {
					Toast.makeText(this, "Reloading contacts", Toast.LENGTH_SHORT).show();
				}
			}
		}
	}
}

Other Android examples (source code examples)

Here is a short list of links related to this Android GesturesListActivity.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.