Android Tutorials
Working With Android Contacts
Accessing Data With Android Cursors
Creating Lists Using The Android ListActivity
Android 9 Patch Scaled PNG Image Guide
Working With Images In Android
Exploring Android LinearLayout And RelativeLayout
Writing A Basic Android Application
Installing The Android SDK In Eclipse

Writing A Basic Android Application

Building The Android Code

Now that the foundation for the application has been created it's lets start building the Java. First create the Relative class to clear the error. Right click on com.higherpass.testapp in the left pane and select New > Class. Enter Relative for the name and android.app.Activity for the superclass.
Eclipse Install Android SDK Continued

We are going to leave the Relative class for later though, but as long as the class exists the error from AndroidManifest.xml will be cleared. Now open the TestApp.java file in src/com.higherpass.testapp in the package explorer. This is the default activity for our application and what will be executed when the program is launched. This class was pre-filled with code to load any saved running instance data and to build the layout from main.xml. The variable R is a special object that is built by the SDK in auto generated code. The setContentView method displays the view.

package com.higherpass.testapp;

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

public class TestApp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

At this point the application will launch and display the buttons we created in main.xml. The next thing we need to do is to create our dialog box.

final Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.dialog_title);
dialog.setMessage(R.string.dialog_text);
dialog.setPositiveButton(R.string.dialog_ok, null);
dialog.setNegativeButton(R.string.dialog_cancel, null);

Place this code in the onCreate function in TestApp.java below the setContentView statement. The code first creates the builder that will deploy the dialog box. Next set the title and body of the dialog box with the setTitle and setMessage methods respectively. The OK/Cancel buttons are created with the setPositiveButton and setNegativeButton functions. Notice that we use the R variable to pass the int value of the ID of the string values from strings.xml.

Now we need to attach an on-click listeners to the first button.

Button btnDialog = (Button) findViewById(R.id.Button01);
btnDialog.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.show();
    }
});

To create the on-click listener the first thing to do is create a Button object representing the button in the layout. Use findViewById to retrieve the Button. Since buttons are superclassed from a TextView which is superclassed from View it's easy to cast findViewById back to a Button object. Most view components are built from the View class which allows easy casting to the proper type from findViewById. Next call the setOnClickListener and pass a new anonymous OnClickListener() instance. Finally set the action in the onClick method to dialog.show() to make the dialog visible with focus.

Next create the on-click listener for the play sound button. This is the same procedure as before except that instead of initializing a dialog box we start the music player. Notification sounds are usually stored in /system/media/audio/notifications, but unfortunately the emulators don't have them installed so you'll need to copy a music file into the virtual SD card.

$ adb push /local/path/to/sound/file /sdcard/file

The adb push command is used to send files to the device (physical or emulator) connected to adb. The first argument is the path to the file on local disk and the second argument is the path on the device to store the file. /sdcard is normally the path to the SD card (physical or virtual). Now for the code to use the built in music player to play the file. The path to the file was set in strings.xml earlier. The file, moonbeam.ogg, listed in strings.xml was copied from a physical phone with the adb pull command.

Button btnSound = (Button) findViewById(R.id.Button02);
btnSound.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        MediaPlayer player = new MediaPlayer();
        try {
            player.setDataSource(getString(R.string.sound_file));
            player.prepare();
            player.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

Attach an on-click listener to Button02 that launches the music player. Initialize a MediaPlayer object as player. Open a try/catch block to catch exceptions from the MediaPlayer object. Then set the file to play from the value of the sound_file string in strings.xml. Use the prepare statement to ready the MediaPlayer for use. Then use the start method to play the file. Finally if an exception is caught print the stack trace.

Working With AndroidManifest.xml <<  1 2 3 4 5  >> Displaying The RelativeLayout
New Content