Archive

Archive for August, 2009

Play Video

August 18th, 2009

Hello,

Here we are going to play a 3gp video in our Android application.

The important point we have to note is that video doesn’t plays when placed in the raw folder.Hence it has to be placed in the SDCARD itself.

So lets begin this interesting topic.

Ok, let us first insert our video:

  • You can get a short 3gp video from net (I got it from http://www.free-3gp-video.com).

  • In eclipse open perspective DDMS(Window –> Open Perspective –> DDMS) .

  • In DDMS perspective open view File Explorer(Window –> Show View –> File Explorer).

  • Also open Device view (Window –> Show View –> Device) and click the emulator you want to use(if emulator is not running then run a emulator with a sdcard option).

  • Then in File Explorer view navigate the tree shown and click sdcard node.

  • There are two buttons on the top right hand side of File Explorer as shown below

push a video onto the sdcard

push a video onto the sdcard

  • Click the button “Push a file onto the device” and in the file chooser navigate and choose the 3gp video.It should appear in the list of sdcard.(If not able to insert, check whether SDCARD is emulated in the emulator , in 1.5 sdk while creating a new Emulator there is a option of sdcard , insert 128M in the textbox next to it).

  • Ok, back to our code, open java perspective(Window –>Show View –> Java) and open layout file Main.xml.

  • Replace the its content with following contents:

<?xml version=”1.0″ encoding=”utf-8″?>
<FrameLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<VideoView
android:id=”@+id/myvideo”
android:layout_gravity=”center”>
</VideoView>
</FrameLayout>

  • Now open your main activity and insert the following in the onCreate method as:

super.onCreate(savedInstanceState);

// set the view to our layout file

setContentView(R.layout.main);
// Video view: to view our video
VideoView video = (VideoView) findViewById(R.id.myvideo);

//set video path to our video(in this case man-cheetah-gazalle.3gp)
video.setVideoPath("/sdcard/man-cheetah-gazelle.3gp");
video.start();

  • Run the application and the video runs as shown below:

Video snapshot

Video snapshot

Android , , , , , , , , , , ,

View site in a new Window(Browser)

August 12th, 2009

Hello ,

Ever thought to include a feature in your application where user clicks a link of certain site or your personal website in the about section of the application and it opens the site in a window and when user finished viewing your achievement and clicks back button your application again gets displayed.

We are going to perform this in just two lines.

  • So first we have to create a intent.

As we already know intent is an abstract description of an operation to be performed.

So we are going to use another version of intent which has following format.

public Intent(String action,Uri uri)

Here:

action is the action we intent(i.e the action we want to perform).

uri is the URL on which the above action is to be performed.

create and intent as

Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.wissen.co.in"));

The above intents means that we want the system to perform a View Action on uri “http://www.wissen.co.in” (i.e. To view it).

  • Next start this activity as:

startActivity(viewIntent);

  • The site will open in browser as

Site opens in a new window(browser)
Site opens in a new window(browser)

Android , , , , , , , , ,

Creating custom Views

August 12th, 2009

Hi All ,

Today we’ll discuss on how to create a canvas using views.

Step 1>

We’ll start off by extending our class by the android.view.View

example:

public class DrawView extends View {
		/**
		 * Constructor
		 */
		public DrawView(Context context) {
			super(context);
		}
	}

Step 2>

Next we’ll use the onDraw() function provided by the View class.

( Tip: In Eclipse Ganymede 3.4.1 ,

use Ctrl+space to see the available methods and select onDraw(…) )

It will look something like this…

public class DrawView extends View {

		/**
		 * Constructor
		 */
		public DrawView(Context context) {
			super(context);
		}

		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
		}
	}

Step 3 >

Lets draw some Text in it..

For that we’ll need the paint class.

Let’s create an object of Paint..

private Paint paint = new Paint();

( Android javadoc :

The Paint class holds the style and color information about how to

draw geometries, text and bitmaps. )

Let’s set its properties..

// set's the paint's colour
paint.setColor(Color.WHITE);
// set's paint's text size
paint.setTextSize(25);
// smooth's out the edges of what is being drawn
paint.setAntiAlias(true);

Step 4>

Let’s draw the actual text…

 canvas.drawText("Hello World", 5 , 30 ,paint); 

The final code will look some thing like this…

public class DrawView extends View {

		private Paint paint;

		/**
		 * Constructor
		 */
		public DrawView(Context context) {
			super(context);

			paint = new Paint();
			// set's the paint's colour
			paint.setColor(Color.GREEN);
			// set's paint's text size
			paint.setTextSize(25);
			// smooth's out the edges of what is being drawn
			paint.setAntiAlias(true);
		}

		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);

			canvas.drawText("Hello World", 5, 30, paint);
			// if the view is visible onDraw will be called at some point
			// in the future
			invalidate();
		}
	}

Step 5>

Now we need to display this View ….

For that we need to create an activity and set its content view as this DrawView.

One way is to suclass DrawView or create a new class file of DrawView..

The final code looks something like this…

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

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

	private class DrawView extends View {

		private Paint paint;
		/**
		 * Constructor
		 */
		public DrawView(Context context) {
			super(context);

			paint = new Paint();
			// set's the paint's colour
			paint.setColor(Color.GREEN);
			// set's paint's text size
			paint.setTextSize(25);
			// smooth's out the edges of what is being drawn
			paint.setAntiAlias(true);
		}

		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);

			canvas.drawText("Hello World", 5, 30, paint);
			// if the view is visible onDraw will be called at some point in the
			// future
			invalidate();
		}
	}
}

The output looks like..

output

Android , , , , , , , ,