Archive for the ‘Android Tutorials’ Category

Move The Apple on TouchEvent

December 2nd, 2011 by admin | 1 Comment | Filed in Android Game Programming, Android Tutorials

The most important technical points you are going to learn are :
SurfaceView : The SurfaceView punches a hole in its window to allow its surface to be displayed.
getHolder(): By calling getHolder()we can access to the underlying surface
surfaceCreated(SurfaceHolder): Gives an idea of when the Surface is created.
surfaceDestroyed(SurfaceHolder):Gives an idea of when the Surface is destroyed
SurfaceHolder.addCallback :The Callback is set with SurfaceHolder.addCallback method

Step 1. Create a new android motodev project.

1

Step 2 : Put a apple.png in your res/drwable folder.
Step 3 : In your MainActivity class paste following codes :

			 package com.ballmovementontouch;

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

				 public class MainActivity extends Activity {
					 /** Called when the activity is first created. */
					 @Override
					 public void onCreate(Bundle savedInstanceState) {
						 super.onCreate(savedInstanceState);
						 requestWindowFeature(Window.FEATURE_NO_TITLE);
						 setContentView(new GameView(this));
					 }
				 }
				

Step 4: Create a new class GameView and paste following codes :

			package com.ballmovementontouch;

				import android.app.Activity;
				import android.content.Context;
				import android.graphics.BitmapFactory;
				import android.graphics.Canvas;
				import android.graphics.Color;
				import android.view.MotionEvent;
				import android.view.SurfaceHolder;
				import android.view.SurfaceView;

				public class GameView extends SurfaceView implements
					SurfaceHolder.Callback {
					private MainThread thread;
					private Apple appleInstance;

					public GameView(Context context) {
						super(context);
						getHolder().addCallback(this);
						appleInstance = new Apple(BitmapFactory.decodeResource(getResources(), R.drawable.apple), 50, 50);
						thread = new MainThread(getHolder(), this);
						setFocusable(true);
					}

					@Override
					public void surfaceChanged(SurfaceHolder holder, int format, int width,
						int height) {
						}

					@Override
					public void surfaceCreated(SurfaceHolder holder) {
						thread.setRunning(true);
						thread.start();
					}

					@Override
					public void surfaceDestroyed(SurfaceHolder holder) {
						boolean retry = true;
						while (retry) {
							try {
								thread.join();
								retry = false;
							} catch (InterruptedException e) {

							}
						}
					}

					@Override
					public boolean onTouchEvent(MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							appleInstance.handleActionDown((int)event.getX(), (int)event.getY());
							if (event.getY() > getHeight() - 50) {
								thread.setRunning(false);
								((Activity)getContext()).finish();
							}
						} if (event.getAction() == MotionEvent.ACTION_MOVE) {
							if (appleInstance.isTouched()) {
								appleInstance.setX((int)event.getX());
								appleInstance.setY((int)event.getY());
							}
						} if (event.getAction() == MotionEvent.ACTION_UP) {
							if (appleInstance.isTouched()) {
								appleInstance.setTouched(false);
							}
						}
						return true;
					}

					@Override
					protected void onDraw(Canvas canvas) {
						canvas.drawColor(Color.BLACK);
						appleInstance.draw(canvas);
					}

				}
                

Step 5: Create an Apple Class and paste following code :

				   package com.ballmovementontouch;

					   import android.graphics.Bitmap;
					   import android.graphics.Canvas;

					   public class Apple {
						   private Bitmap bitmap;
						   private int x;
						   private int y;
						   private boolean touched;						   

						   public Apple(Bitmap bitmap, int x, int y) {
							   this.bitmap = bitmap;
							   this.x = x;
							   this.y = y;
						   }

						   public Bitmap getBitmap() {
							   return bitmap;
						   }
						   public void setBitmap(Bitmap bitmap) {
							   this.bitmap = bitmap;
						   }
						   public int getX() {
							   return x;
						   }
						   public void setX(int x) {
							   this.x = x;
						   }
						   public int getY() {
							   return y;
						   }
						   public void setY(int y) {
							   this.y = y;
						   }
						   public boolean isTouched() {
							   return touched;
						   }

						   public void setTouched(boolean touched) {
							   this.touched = touched;
						   }
						   public void draw(Canvas canvas) {
							   canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null);
						   }	   

						   public void handleActionDown(int eventX, int eventY) {
							   if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) {
								   if (eventY >= (y - bitmap.getHeight() / 2) && (y <= (y + bitmap.getHeight() / 2))) {
									   setTouched(true);
								   } else {
									   setTouched(false);
								   }
							   } else {
								   setTouched(false);
							   }
						   }
					   }
                     

Step 6: Now Run your app and move the apple on touching it. It will look like this :
2

Tags: , , , , , , , ,

The Key movement for a particular object

November 30th, 2011 by admin | 2 Comments | Filed in Android Game Programming, Android Tutorials

Lets learn today how to handle the the Key movement for a particular object.
Here I have used a ball as object wich will move 10 px according to the key:

There are following steps :
1. Create a new motodev project ‘Ballmovement’.
New_Project

2. Your AndroidManifest.xml will look like this



    

    
        
            
                
                
            
        

    

3. Put the ball_1.png in your res/drwable folder

4. Now put following code in ManiActivity.java

package com.ballmovent;

import android.app.Activity;

import android.os.Bundle;

import android.view.Window;

public class MainActivity extends Activity {

   /** Called when the activity is first created. */

   @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(new GameView(this));

 }

}

5. Create a new class GameView.java. Now put following code in GameView.java


package com.ballmovent;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import com.ballmovent.ball;

public class GameView extends View {

      private ball ballInstance;
      public static final int UP = 0, DOWN = 1, RIGHT = 2, LEFT = 3;

      public GameView(Context context) {
            super(context);
            ballInstance = new ball(BitmapFactory.decodeResource(context.getResources(),R.drawable.ball_1), 50, 50);
            setFocusable(true);
    		this.setFocusableInTouchMode(true);
      }

      @Override

      protected void onDraw(Canvas canvas) {
          canvas.drawColor(Color.BLACK);
          ballInstance.draw(canvas);
      }

      @Override
  	public boolean onKeyDown(int keyCode, KeyEvent evt) {
  		boolean moved = false;
  		Log.v("aks 1"+ moved, "index=" + 1);
  		switch(keyCode) {
  			case KeyEvent.KEYCODE_DPAD_UP:
  				moved = ballInstance.move(UP);
  				break;
  			case KeyEvent.KEYCODE_DPAD_DOWN:
  				moved = ballInstance.move(DOWN);
  				break;
  			case KeyEvent.KEYCODE_DPAD_RIGHT:
  				moved = ballInstance.move(RIGHT);
  				break;
  			case KeyEvent.KEYCODE_DPAD_LEFT:
  				moved = ballInstance.move(LEFT);
  				break;
  			default:
  				return super.onKeyDown(keyCode,evt);
  		}
  		if(moved) {
  			//the ball was moved so we'll redraw the view
  			ballInstance.setX((int)ballInstance.getX());
  			ballInstance.setY((int)ballInstance.getY());
  			invalidate();
  		}
  		return true;
  	}

}

7. Create a new class ball.java. Now put following code in ball.java for varous function etc

package com.ballmovent;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class ball {

	private Bitmap bitmap;
	private int x;
	private int y;
	public static final int UP = 0, DOWN = 1, RIGHT = 2, LEFT = 3;

	public ball(Bitmap bitmap, int x, int y) {
		this.bitmap = bitmap;
		this.x = x;
		this.y = y;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}
	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}

	public void draw(Canvas canvas) {
		canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null);
	}

	public boolean move(int direction) {
		boolean moved = false;
		if(direction == UP) {
				y -= 10;
				moved = true;
		}
		if(direction == DOWN) {
				y += 10;
				moved = true;
		}
		if(direction == RIGHT) {
				x += 10;
				moved = true;
		}
		if(direction == LEFT) {
				x -= 10;
				moved = true;
		}
		return moved;
	}

}

8. Run your application and check the movement of ball by pressing keys. Your app will look like this :

ball_movement_on_key

Tags: , , , , , , , , , , ,

How to create Christmas Card Gallery using Grid view

November 18th, 2011 by admin | 1 Comment | Filed in Android Tutorials, Types of Views

Lets develop a Christmas Card Gallery application. We will need to follow these steps :
Step 1: Create a new android project using motodev studios.
Step 2: Choose the name and SDK version for the app. Your AndroidManifest.xml will look
like this :

			
			
			

			
			
			
			
			
			
			

			
			
		   

Step 3: Put your app launcher icon image into res/drawable folder.
Step 4: Open your res/layout/main.xml and put following code there :

			
			
          

Step 5 : Write your app name into res/value/String.xml

			
			
			Hello World
			Christmas Card gallery
			
		   

Step 6 : write following code in your MainActivity.java file

		package com.cardGallery;

		import android.app.Activity;
		import android.content.Context;
		import android.os.Bundle;
		import android.view.View;
		import android.view.ViewGroup;
		import android.widget.AdapterView;
		import android.widget.BaseAdapter;
		import android.widget.GridView;
		import android.widget.ImageView;
		import android.widget.Toast;
		import android.widget.AdapterView.OnItemClickListener;

		public class MainActivity extends Activity
		{
		//---the images to display---
		Integer[] imageIDs = {
		R.drawable.card1,
		R.drawable.card2,
		R.drawable.card3,
		R.drawable.card4,
		R.drawable.card5,
		R.drawable.card6,
		R.drawable.card7,
		R.drawable.card8,
		R.drawable.card9,
		R.drawable.card10,
		R.drawable.card11,
		R.drawable.card12,
		R.drawable.card13,
		R.drawable.card14,
		R.drawable.card15

		};

		@Override
		public void onCreate(Bundle savedInstanceState)
		{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		GridView gridView = (GridView) findViewById(R.id.cardview);
		gridView.setAdapter(new ImageAdapter(this));

		gridView.setOnItemClickListener(new OnItemClickListener()
		{
		public void onItemClick(AdapterView parent,
		View v, int position, long id)
		{
		Toast.makeText(getBaseContext(),
		"Card" + (position + 1) + " selected",
		Toast.LENGTH_SHORT).show();
		}
		});
		}

		public class ImageAdapter extends BaseAdapter
		{
		private Context context;

		public ImageAdapter(Context c)
		{
		context = c;
		}

		//---returns the number of images---
		public int getCount() {
		return imageIDs.length;
		}

		//---returns the ID of an item---
		public Object getItem(int position) {
		return position;
		}

		public long getItemId(int position) {
		return position;
		}

		//---returns an ImageView view---
		public View getView(int position, View convertView, ViewGroup parent)
		{
		ImageView imageView;
		if (convertView == null) {
		imageView = new ImageView(context);
		imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
		imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		imageView.setPadding(5, 5, 5, 5);
		} else {
		imageView = (ImageView) convertView;
		}
		imageView.setImageResource(imageIDs[position]);
		return imageView;
		}
		}
		}
		

Step 7 : Run your application it will look like this.
result

Step 8 : In main menu your app icon will be shown like this :
app_icon

Tags: , , , , , , , , , , , , , ,

Android components

November 15th, 2011 by admin | No Comments | Filed in Android Tutorials

An Android application consists following parts:

componentsOfAndroid


Tags: , ,

TableLayout in Android

November 15th, 2011 by admin | 1 Comment | Filed in Android Tutorials, Types of Views

droid_tool
TableLayout displays data in table view.
This example is for TableLayout :

Steps :
Step 1. Create a new android project.
Step 2. Give the project name.
Step 3. Open res/layout/main.xml

Step 4. Here is the code :


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
android:layout_column="2"
android:text="row1col1"
android:padding="3dip" />
android:text="row1col2"
android:gravity="right"
android:padding="3dip" />
android:layout_column="2"
android:text="row2col1"
android:padding="3dip" />
android:text="row2col2"
android:gravity="right"
android:padding="3dip" />
android:layout_column="2"
android:text="row3col1"
android:padding="3dip" />
android:text="row3col2"
android:gravity="right"
android:padding="3dip" />

android:layout_height="2dip"
android:background="#FF909090" />

android:layout_height="2dip"
android:background="#FF909090" />

Step 5.Run you app. it will look like this :
tableLayout

Tags: , , , , , ,

RelativeLayout in Android

November 15th, 2011 by admin | 1 Comment | Filed in Android Tutorials, Flash AS 3.0, Types of Views

droid_tool

RelativeLayout is a ViewGroup that displays child View elements in their relative positions.
The position of a View is as relative to elements.
This example is for Relative Layout :

Steps :
Step 1. Create a new android project.
Step 2. Give the project name.
Step 3. Open res/layout/main.xml

Step 4. Here is the code :



Step 5.Run you app. it will look like this :
RelativeLayout

Tags: , , , , ,

Linear Layout in Android

November 15th, 2011 by admin | No Comments | Filed in Android Tutorials, Types of Views
This example is for Linear views :
Steps :
Step 1. Create a new android project.
Step 2. Give the project name.
Step 3. Open res/layout/main.xml
There is a root LinearLayout that defines its orientation to be vertical and horizontal.
Step 4. Here is the code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="column one"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text=" column two"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="row one"
android:textSize="15pt"
android:gravity="center_vertical"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:gravity="center_vertical"
android:background="#aaaaaa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Step 5.Run you app. it will look like this :

droid_tool

This example is for Linear views :

Steps :

Step 1. Create a new android project.

Step 2. Give the project name.

Step 3. Open res/layout/main.xml

There is a root LinearLayout that defines its orientation to be vertical and horizontal.

Step 4. Here is the code :


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TextView

android:text="column one"

android:gravity="center_horizontal"

android:background="#aa0000"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

<TextView

android:text=" column two"

android:gravity="center_horizontal"

android:background="#00aa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

</LinearLayout>

<LinearLayout

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TextView

android:text="row one"

android:textSize="15pt"

android:gravity="center_vertical"

android:background="#000000"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

<TextView

android:text="row two"

android:textSize="15pt"

android:gravity="center_vertical"

android:background="#aaaaaa"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

Step 5.Run you app. it will look like this :

LinearView

Tags: , , , , ,

Android Hello world Program

November 15th, 2011 by admin | 1 Comment | Filed in Android Tutorials

Android Hello world Program :
Lets start with hello world program. Here we will learn how to write
a hello world program in android :
Step 1 :Create New android Project.

1

Step 2 :Give the Project Name and choose SDK version.

2

Step 3 :The manifest file.

3

step 4 : Here is code :

Code of used classes :
Activity Class :


package com.helloworldandroid;

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

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hi This is your first Program");
setContentView(tv);
}
}

R.Java :


/* AUTO-GENERATED FILE.  DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found.  It
* should not be modified by hand.
*/

package com.helloworldandroid;

public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}

res/layput/main.xml :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

Step 5 : Run your application :- It will look like this
4

Tags: , , , , , ,

Android Programming with MOTODEV Studio

November 14th, 2011 by admin | 3 Comments | Filed in Android Tutorials

Welcome all  !! :)

android

This section is going to provide you tutorials for Android Game Programming . Lets Start with MOTODEV Studio 2.1.1 .

What is MOTODEV Studio 2.1.1 ?


Motodev Studio is for Android which provides a great environment for developement of Android Games and application.

Feature List of MOTODEV Studio :

1. Code Snippets

2. Translated Content

3. App Validator

4. Handset Emulators

5. Ease of Installation

6. Database Management

7. Application Creation Wizards

8. Deploy Packages

9. Application Signing

10. Target All Android Handsets

11. Translate Your App

12. Screen Capture

13.Discover SDK Add-ons

14. Supports the Android NDK


Here you can the download it

http://developer.motorola.com/docstools/motodevstudio/

we will further proceed with installation and development in next Chapter  .


Tags: , , , , , , , , , , , , , , , , ,

How to Install MOTODEV Studio

November 14th, 2011 by admin | 2 Comments | Filed in Android Tutorials

android

Lets start with the Installtion of MOTODEV Studio 2.1.1 .

There are following Steps :

1. On After clicking the EXE setup of MotoDev it will show this window

2. Click Next to continue the Installtion

3. Select the language

4. Accept License Agreement

5. It will check the  presence of Java Environment

6. Choose location directory :

7. You can browse and select your location

8. Select destination folder and click OK . It will start the installation process:

9. Installation in progress

10. Installation Comleted

11. Start the Editor and choose work directory which will work as your projects repository

12. Provide android sdk path

13.  Getting Error : Something like the below image ???

Lets short out this error and start new project in our next tutorial :)

Tags: , , ,