Month: January 2012

Playing audio & video in Android – 1

Android’s open platform and provider-agnostic philosophy ensures that it offers a multimedia API capable of playing and recording a wide range of image, audio, and video formats, both locally and streamed. The Camera API and OpenCORE multimedia platform expose these capabilities to your applications, providing comprehensive multimedia functionality.

Android includes a comprehensive Media Player to simplify the playback of audio and video. This section describes how to use it to control and manipulate media playback within your applications. Android 2.1 (API level 7) supports the following multimedia formats for playback as part of the base framework. Note that some devices may support playback of additional file formats:

Audio
➤ AAC LC/LTP
➤ HE-AACv1 (AAC+)
➤ HE-AACv2 (Enhanced AAC+)
➤ AMR-NB
➤ AMR-WB
➤ MP3
➤ MIDI
➤ Ogg Vorbis
➤ PCM / WAVE

Video
➤ H.263
➤ H.264 AVC
➤ MPEG-4 SP

Android Sudoku – 6 Applying a Theme for About

A theme is a collection of styles that override the look and feel of Android widgets. Subjects were drawn from the Cascading Style Sheets (CSS) used web page, which separates content and presentation screen or style.  Android comes with several themes that can be reference by name, four, or you can make your own theme by subclassing existing and ignore their default values.

To use the theme, open the AndroidManifest.xml and change the About activity with following code.

<activity android:name=”.About” android:label=”@string/about_title” android:theme=”@android:style/Theme.Dialog” >
</activity>

The android @: prefix before the name of style means that this is a reference to a resource defined by Android, not one that is defined in its the program. Run again and the output look at like this,

Android Sudoku – 5

Implementing an About Box

When the user select the About button (touch or navigate with D-pad or traceball and press the selection button), we want to pop up a window with some information. After scrolling through the text, the user can press the Back button to dismiss the window.

We can accomplish this in several ways,

1.Define a new Activity, and start it

2. Use the AlertDialog class, and show it

3. Subclass   Android`s Dialog class, and show that

For this application, let`s define a new activity. Like the main Sudoku activity.

STEP 1. : Now we need About activity layout file. We will name it res/layout/about.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<ScrollView
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:padding=”10dip” >
<TextView
android:id=”@+id/about_content”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/about_text”/>
</ScrollView>

We need only one version of layout, because this will work both portrait and landscape modes.

STEP 2 : Add strings for the title of the About dialog box and the text it contains to res/values/strings.xml

<string name=”about_title”>About Android Sudoku</string>
<string name=”about_text”> Sudoku is a logic-based number placement puzzle. Starting with a partially completed 9×9 grid, the objective is to fill the grid so that each row, each column, and each of the 3×3 boxes (also called <i>blocks</i>) contains the digits 1 to 9 exactly once.
</string>

– is used to prevents an extra blank from appearing before the first word.

STEP : 3 Define the About activity at About.java . Create new java class at /src/androidsl.raaz.sudoku/Activity.java

package androidsl.raaz.sudoku;

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

public class About extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);
    }
    }

STEP 4 : Wire all this up to the About button in the SudokuQuiz class /src/androidsl.raaz.sudoku/SudokuQuizActivity.java

1. Add few imports

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

2. In the onCreate( ) method, add code to call findViewById( ) to look up an Android view given its resource ID, and add code to call setOnClickListener() to tell Android which object to tickle when the user touches or clicks the view:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}

3. The setOnClickListener( ) method needs to be passed an object that implements the OnClickListener Java interface. We’re passing it the this variable, so we had better make the current class (Sudoku) implement that interface, or we’ll get a compiler error. OnClickListener has one method in it called onClick( ), so we have to add that method to our class as well. [If you’re a Java expert, you may be wondering why we didn’t use an anonymous inner class to handle the clicks. You could, but according to the Android developers, every new inner class takes up an extra 1KB of memory.]

/src/androidsl.raaz.sudoku/SudokuQuizActivity.java

public class Sudoku extends Activity implements OnClickListener {
// …
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
// More buttons go here (if any) …
}
}
}

To start an activity in Android, we first need to create an instance of the Intent class. There are two kinds of intents:

(a) public (named) intents that are registered with the system and can be called from any application
(b) private (anonymous) intents that are used within a single application.

For here when we run this program and select About button this will get an error.

Solution : We forgot one important step: every activity needs to be declared in AndroidManifest.xml. To do that, double-click the file to open it, switch to XML mode if necessary by selecting the AndroidManifest.xml tab at the bottom, and add a new <activity> tag after the closing tag of the first one

<activity android:name=”.About” android:label=”@string/about_title” >
</activity>

Now run again and press About button, this will give the following screen.

Android SDK Features

The true appeal of Android as a development environment lies in the APIs it provides. As an application-neutral platform, Android gives you the opportunity to create applications that are as much a part of the phone as anything provided out of the box. The following list highlights some of the most noteworthy Android features:

  • No Licensing, distribution or development fees.
  • No fee for release approval process
  • Full control of multimedia, including playback and recording with the camera and microphone.
  • Wi-Fi access
  • GSM, EDGE, 3G, SMS, Call, etc services
  • Comprehensive APIs for location-based services – GPS
  • APIs for using sensor hardware, including accelerometers and the compass
  • Libraries for using Bluetooth for peer-to-peer data transfer
  • IPC message passing
  • Shared data stores
  • Background applications and processes
  • Home-screen Widgets, Live Folders, and LiveWallpaper
  • The ability to integrate application search results into the system search
  • An integrated open-source HTML5WebKit-based browser
  • Full support for applications that integrate map controls as part of their user interface
  • Mobile-optimized hardware-accelerated graphics, including a path-based 2D graphics library and support for 3D graphics using OpenGL ES 2.0
  • Media libraries for playing and recording a variety of audio/video or still image formats
  • Localization through a dynamic resource framework
  • An application framework 
  • that encourages reuse of application components and the replacement of native applications

Android Sensors – 1 Introduction

Modern mobile phones are much more than simple communication devices with a connection to the Internet. With microphones, cameras, accelerometers, compasses, temperature gauges, and brightness detectors, smart-phones have become extra-sensory devices which able to augment your own perception.

Sensors that detect physical and environmental properties offer an exciting innovation for enhancing the user experience of mobile applications. The incorporation of electronic compasses, gravity sensors, brightness gauges, and proximity sensors in modern devices provides an array of new possibilities for interacting with devices, such as augmented reality and physical movement-based input.

–Will be Continue —

Android Sudoku – 4

Using Alternate Resources

Create the new file in res/layout-land/main.xml with following codes.

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:background=”@color/background”
android:layout_height=”fill_parent”
android:layout_width=”fill_parent”
android:padding=”15dip”
android:orientation=”horizontal” >
<LinearLayout
android:orientation=”vertical”
android:layout_height=”wrap_content”
android:layout_width=”fill_parent”
android:layout_gravity=”center”
android:paddingLeft=”20dip”
android:paddingRight=”20dip” >
<TextView
android:text=”@string/main_title”
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:layout_gravity=”center”
android:layout_marginBottom=”20dip”
android:textSize=”24.5sp” />
<TableLayout
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:layout_gravity=”center”
android:stretchColumns=”*” >
<TableRow>
<Button
android:id=”@+id/continue_button”
android:text=”@string/continue_label” />
<Button
android:id=”@+id/new_button”
android:text=”@string/new_game_label” />
</TableRow>
<TableRow>
<Button
android:id=”@+id/about_button”
android:text=”@string/about_label” />
<Button
android:id=”@+id/exit_button”
android:text=”@string/exit_label” />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>

This will give the following Output….