Get Your Device for Android

Monday, December 19, 2011

Create Your Beyond First Android App (Part 1)

android
This is my first article about android application development. When you read the phrase "Your first app", you'll say anything in your mind like "I think this must be "Hello World!!" for sure". Don't worry about this. The Hello World was boring me too! So, I'll let get start with a beyond Hello World app. (but it's still Hello Word lol)
In this project we will create an app that contain a Button and a TextView. When you click on the button it'll change to another screen that have a "Hello My Friend!!" text and play some sound. Interesting?





Download Android Sample Code Here

This Article You Will Learn:
  1. How to connect UI to your code
  2. How to use Intent to start new Activity
  3. How to Multithread programming
  4. How to play a sound

Ok, let's move on...

And... Hey! I'll never tell you about how to install the Android SDK or Setting up your work environment because you can find it in various site on the internet just ask Google


Using eclipse. First, Create a new Android Project named "HelloFriend".

android
I use android platform 2.3.3 (API level 10) and set package name to "com.massdstu.hello"

android
Next, open main.xml in Package Explorer and edit code like this below then you will get an UI with one TextView and one Button
android
Next, make a copy of main.xml and rename it to main2.xml in the same folder (/res/layout) and edit code like the below

android

Next, Create a class named "HelloAgain.java" and edit code like the below:

android

android

This project has more than one activity. So, we must add activity to AndroidManifest.xml

android

Ok! it's nearly done, first, create a folder name it as "raw" under "/res" folder and download this sound and put it in raw folder.
Then, open HelloFriendActivity.java and edit the code:

package com.massdstu.hello;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HellofriendActivity extends Activity {
/** Called when the activity is first created. */
Button btnCon;
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCon = (Button)findViewById(R.id.btnCon);
btnCon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp != null)
mp.release();
new Thread(new Runnable() {
public void run() {
try {
mp = MediaPlayer.create(getApplicationContext(),R.raw.ring);
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
Intent i = new Intent(getApplicationContext(),HelloAgain.class);
startActivity(i);
}
});
}
}

Yeah!! It's done!!! Then, let's run this application by press Run button or command + Fn + Shift+ F11 key (in OSX). While application run you'll see text "Hello World" and a continue button. If you click on the button, you'll here a ring sound and it'll bring you to the second screen that have a text "Hello My Friends".

This is The first part of the project. In the second part I'll explain you about What you are coding in this app. See you in next part.

- vyppN-

No comments:

Post a Comment