Friday, May 23, 2014

Delete row in SQLite database

android.database.sqlite.SQLiteDatabase class provide a convenience method for deleting rows in the database.



A simple example using Android's SQLite database, exposes data from Cursor to a ListView

In previous exercise "A simple example using Android's SQLite database", the result of queue was presented as string. It's going to be modified to exposes data from Cursor to a ListView widget.



A simple example using Android's SQLite database

It's a simple example using Android's SQLite database. A adapter, SQLiteAdapter, is implement as a adapter between our activity and SQLite, with a inner class SQLiteHelper which extends SQLiteOpenHelper.

The SQLite database have only one field, "Content". When the app start, it will open the database and delete all first, then insert some dummy data, then close it. And Re-open, read all content.

Thursday, May 22, 2014

Code Examples of TranslateAnimation

An animation that controls the position of an object. See th android.view.animation description for details and sample code.
  • android.view.animation.TranslateAnimation

Touch Events and Sprite Collision Detection. Android Game Programming

In this tutorial we are going to handle the touch event in the screen and check if the (x,y) coordinates collision with any of our characters sprites.


The first thing to do is add this method in the view to handle each touch in the screen. For each sprite in the sprites list we are going to ask if the (x,y) touch coordinates have a collision. If the collision exists, we remove the sprite from the sprites list. We iterate backwards the sprite list to avoid errors in the next iteration when we remove a sprite.

Wednesday, May 21, 2014

Playing audio example code in an Android application

It is really simple to use audio files in your application. Just like the graphics you use on the display to convey some information to the user, you can use audio to convey information to the user in your application.Android supports music and sound through MediaPlayer class. This class is available in android.media package.

setVolumeControlStream() method is used to tell the Android that the user presses the volume up or down keys while this application is running. This will adjust the volume of the audio file being played instead of the ringer volume.

We need to copy the audio files to /res/raw directory.
Copying a file into the res directory causes the Android eclipse IDE to define a Java identification for you in the R.java class. So we can simply access the audio file using R.raw.filename

Once you are done with this, all you need to do is create a MediaPlayer instance and call start() method.

MediaPlayer mp=MediaPlayer.create(this, resId);
mp.start();


Vibrate Android device with Example

Vibrating Android device is found effective when you wanted to get notified on the incoming connection in various cases like, user cannot hear a ringtone or he wants to get notified silently. Its a very basic feature found in all mobile phones. Lets see how to vibrate an android device programatically. Its just two lines of code.



Android : how to check if device has camera

In Android, you can use  PackageManager hasSystemFeature()  method to check if a device has camera, gps or other features.

See full example of using  PackageManager  in an activity class.

package com.mkyong.android;
 
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
 
public class FlashLightActivity extends Activity {
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.main);
 
  Context context = this;
  PackageManager packageManager = context.getPackageManager();
 
  // if device support camera?
  if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
   //yes
   Log.i("camera", "This device has camera!");
  }else{
   //no
   Log.i("camera", "This device has no camera!");
  }
 
 
 }
}

Camera flashlight example

You may interest on this example – How to turn on/off camera LED/flashlight in Android.

How to turn on/off camera LED / flashlight in Android

In this tutorial, we show you how to turn on/off the phone camera led or flashlight in Android. See code snippets :

1. Turn on
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();


Tuesday, May 20, 2014

Android SDK: Creating a Simple Property Animation

With Android you can include various types of animation in your apps. In this tutorial we will create a basic property animation using Android's Object Animator and Value Animator classes. The result will be simple but the techniques involved will apply in more complex animated effects. We will create an animation in which a steering wheel turns and the background scene moves accordingly.

With property animation, you have a few more options than with View animation, for example you can animate UI items other than Views and can animate more properties. Property animation can also have more consistent results in some cases, as unlike View animation, it alters the UI objects themselves, rather than just drawing them in particular ways. The downside to these advantages is that property animation is a little more complex - but it's still accessible for beginners.