Android - Hello World
Objective #1: Locate and open important files and resources in Eclipse's Package Explorer
- The Package Explorer window pane in Eclipse gives you the ability to locate and open many important files related to a project.
- The main java file for a project is found in the src folder. You'll find it with a .java file extension inside the package name that you gave to the project when you created it. For example, the name of the file might be HelloWorld.java which may be located in a package named com.minich.flashlight
- The project's manifest file is located under the res (i.e. resources) folder and will be named AndroidManifest.xml
- The resource files are also found in the res folder. Within the layout folder, you'll find a file named main.xml which is important since it is the main interface screen for the project. The strings.xml file is also important and found in the values directory.
Objective #2: Edit a project's main java source file.
- The main java source file is a subclass of the Java Activity class. Therefore it extends the Activity class.
- The onCreate method from the Activity class needs to be overwritten. The onCreate method is similar to the Visual Basic Form_Load method and the main method in Java console programs that we studied in earlier classes.
- The setContentView call statement is necessary to display the appropriate UI (i.e. user interface). Often, it is wise to display the main view whjich is defined in the main.xml class and referred to as R.layout.main.
Objective #3: Edit the UI for a simple project by editing XML files.
- Double-click the main.xml file and go to the Layout tab to be able to drag and drop objects onto the main interface.
- Or, click the main.xml tab to edit XML tags in the source code of the XML file.
- XML is a markup language similar to HTML that is used to create web pages. XML files are not executed. Rather, they merely store data that is used by executable files when an application is opened or executed.
- An XML tag is structured like this
<Tag property settings />
Notice the opening angle bracket < and the closing > angle bracket. Also notice the forward slash that is typed just before the closing angle bracket.
- The settings inside each XML tag are similar to properties of a class. Here's a website that can help you with RGB values for interesting colors.
- Here's a screencast that demonstrates how to change the interface of a view using XML.
Objective #4: Create a HelloWorld app
Objective #5: Design the interface and create a Layout
- See this tutorial - http://www.learn-android.com/2010/01/05/android-layout-tutorial
- Available Layouts
- AbsoluteLayout
- FrameLayout
- LinearLayout
- RelativeLayout
- TableLayout
- TabbedLayout
- BoringLayout
- You can use the free program DroidDraw to create the interface for a project. Follow this screencast to download and install DroidDraw.
Objective #6: Use a button.
- To make use of a button you can add the following code to your project's main.xml file
<Button
android:text="Click Me"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
In your java source file, you must import the following classes: android.widget.Button, android.view.View, & android.view.View.OnClickListener
and add the following code to the activity's onCreate method:
final Button button01 = (Button) findViewById(R.id.Button01);
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
button01.setText("Hello");
}
});
- To test a button, you can use Toast as in
Toast.makeText(HelloWorld.this, "hello world", Toast.LENGTH_SHORT).show();
Objective #7: Use common methods in an activity's onCreate method.
- To reference the text found in a text view
final TextView textview01 = (TextView) findViewById(R.id.TextView01);
followed by
String text = textview01.getText().toString();
in a button's onClick method.
- To reference a string found in the strings.xml resource file
final String text = this.getString(R.string.example);
- To convert a string value found in a textview to an int
num = Integer.getInteger(textview01.getText().toString());
- To display an number stored in an int variable into a text view
textview01.setText(num + "");
- To convert a string into an int
int num = Integer.parseInt(str);
or
int num = Integer.getInteger(str);
Objective #8: Use EditText object to obtain input from the user.