Hi All,
There's lots of people willing to share their code and experience right here. Everybody had to start some place.
Here's some code for doing something with a couple of buttons:
Code:
private static int SOUND_1 = 1;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
initSounds();
Button play = (Button)findViewById(R.id.button_play);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
playSound(SOUND_1);
}
});
Button secondActivity = (Button)findViewById(R.id.button_secondactivity);
secondActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(YourActivity.this, Second.class);
startActivityForResult(i, ACTIVITY_ID);
}
});
}
Here's a layout for them:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
/>
<Button
android:id="@+id/button_secondactivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
/>
</LinearLayout>
Does that help any?
Phyll
PS
I probably shouldn't do it because its just opening up Pandora's box but here's something that might help you learn by example.
Go to betterpaving.com/stuff/ and download the Android Code Writer application you'll find there.
This application allows you to write an outline of your Android app in pcode. Then it converts it into Java code and xml files. Finally, it can export those files directly into a project in Eclipse.
For example, if you wanted to create two activities and have one able to start the other and return results from it, you could just write an outline like this:
Code:
Package com.bandpsoftware
Application App9
Activity App9 Activity
Intent MAIN LAUNCHER
View Button Second
End Activity
Activity Second Activity
View Button Done
End Activity
Handoff App9 Second
Uses Sdk 7
This will give you an application named App9 with a package of com.bandpsoftware.app9. You can make them anything you want. The app would have two activities. Each has a button to allow switching to the other. Boilerplate code to make the handoff to and from the second activity is accomplished by the Handoff statement. Finally it will require a minimum of level 7.
It makes a .java file and a layout xml for each activity. It makes a strings.xml file and puts strings there if you declare them and some names and labels. It also makes the manifest file and puts all of them in the proper folders when its exported.
It is not meant to be a production application so it might break once and a while but all in all I find it very useful for making apps in minutes. When you get it working, click the help button so you can get an idea what it can do. You can add to the help file by just typing and clicking the Save button. There are a number of statements that cause code to be written for custom forms, menus and custom menus, taking pictures, creating a drawing surface, using preferences, alerts, timers, threads, images, icons, sounds, text file read and write, phone dialing, slider widget, configuration changes, services and alarms to name a few.
I add to it frequently so you should check back now and then to see if its version has changed.
Here's what the App9 main activity example .java code looks like:
Code:
package com.bandpsoftware.app9;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class App9 extends Activity {
private static final int SHOW_NAME = 1;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button second = (Button)findViewById(R.id.button_second);
second.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//TODO: Button Function
}
});
}
//Add this to a button handler, menu item, etc.
//Intent i = new Intent(App9.this, Second.class);
//startActivityForResult(i, SHOW_NAME);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SHOW_NAME)
if (resultCode == Activity.RESULT_OK) {
//TODO: Do something here with it
}//if result_ok
}//onactivityresult
//TODO: Fill In Methods Etc.
}//class
Just download the application. Run it. Enter the example. Click on the Run button to see what the rest of the code looks like.