Survey							
                            
		                
		                * Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Hello Android Example
Here, you will know how to create the simple hello android application. We are creating the
simple example of android using the Eclipse IDE. For creating the simple example:
Create the new android project
Write the message (optional)
Run the android application
Example of Hello Android
You need to follow the 3 steps mentioned above for creating the Hello android application.
1) Create the New Android project
For creating the new android project:
Select File > New > Project...
Select the android project and click next
Fill the Details in this diaglog box and click finish
2) Write the message
For writing the message we are using the TextView class. Change the onCreate method as:
package com.example.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview=new TextView(this);
textview.setText("Hello Android!");
setContentView(textview);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
3) Run the android application
To run the android application:
Right click on your project > Run As > Android Application
The android emulator might take 2 or 3 minutes to boot. So please have patience. After
booting the emulator, the eclipse plugin installs the application and launches the activity.
You will see something like this:
Expanding upon Hello World with XML
First, in case you didn't get the notice, it may be a while before the video tutorials are up. Turns out my
screen recorder of choice doesn't work with OSX 10.7, and I'm currently searching for a temporary
alternative.
Second, if you haven't noticed, there is a "donate" button on the right side, somewhat against my
wishes. However, it was suggested that one be added after I had "delved" into the project, so I have
fulfilled that request. If you're wondering where the money goes, it will go to help cover the hosting cost
of the website where all of this will be moving soon. It's more expensive than I thought it would be, and
being a college student, I'd like to cover the costs from being out of pocket somehow.
Onto today's content: using more graphical interfaces with XML. This will go along with the Android
Developer information, except I'm going to change things around a little bit to make it a bit more user
friendly. (If you haven't checked it out, I recommend it).
If you search through the Android library, there are lots of different opportunities for you to use in your
programs; we're talking anything defined to be used for the Android library. Today we'll be focusing
somewhat more on GUIs (graphical user interfaces). If you're familiar with GUIs in Java, then this will be
a bit easier for you.
GUIs are used in Java to bring your program away from the typical console. You can add all sorts of
elements to a Java GUI; text boxes, radio buttons, select tabs, etc. As well as images, audio, and any
calculations you do "behind the scenes" in Java. However, in Android, the GUI is what we'll be seeing on
the screen.
If you noticed from my first tutorial, however, the code was pretty lengthy for just displaying a couple
words on your screen. This is because the app was done completely in what is called "programmatic" UI.
This means that the entire project was constructed using the UI source code provided by
Google/Android. Anyone who's ever programmed an application like this can understand how
frustrating it is; debugging can be stressful, and a small change in your source code can lead to big
problems with your layout. However, there's another way to make our Android apps simpler; XML.
XML, or Extensible Markup Language, is a "set of rules for encoding documents in machine-readable
form". In short, XML is meant to make projects across platforms more usable, simpler, and more
generic. XML is very widely used as a standard, and many languages have been created based around
XML, including RSS, Atom, XHTML, and SOAP.
So, let's look at an example of XML for an Android app; specifically the one that Android Dev provides:
?
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
1
android:layout_width="fill_parent"
2
android:layout_height="fill_parent"
3
android:text="@string/hello"/>
4
5 XML files for Android (or, for anything) are fairly straightforward: it's a "tree of
elements" where each node is the name of a View class. You can define custom
6 View elements in your code, or you can use any of the classes that extend
View.
The above example has just one View element, being TextView (which has 5
elements). A description of each element as such:
Attribute
Meaning
xmlns:android
This is an XML namespace declaration that tells the Android tools that
you are going to refer to common attributes defined in the Android
namespace. The outermost tag in every Android layout file must have
this attribute.
android:id
This attribute assigns a unique identifier to the TextView element. You
can use the assigned ID to reference this View from your source code or
from other XML resource declarations.
This attribute defines how much of the available width on the screen this
android:layout_width View should consume. In this case, it's the only View so you want it to
take up the entire screen, which is what a value of "fill_parent" means.
android:layout_heigh This is just like android:layout_width, except that it refers to available
t
screen height.
android:text
This sets the text that the TextView should display. In this example, you
use a string resource instead of a hard-coded string value. The hello
string is defined in the res/values/strings.xml file. This is the
recommended practice for inserting strings to your application, because it
makes the localization of your application to other languages graceful,
without need to hard-code changes to the layout file.
Source: Android Developers
XML files can be found in your project folder, under res/layout. These are automatically created for you
using the Android plugin in Eclipse. There should be one auto-generated file in your project, namely
main.xml. Open the file, and replace the code inside with the following:
?
1
<?xml version="1.0" encoding="utf-8"?>
2
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
3
android:id="@+id/textview"
4
android:layout_width="fill_parent"
5
android:layout_height="fill_parent"
6
android:text="@string/hello"/>
Now, inside your res/values folder, open up strings.xml. This is where you should store all your default
strings. You should have two default strings already made: hello and app_name.
Revise the hello to something else; doesn't really matter, just to show you how it works. You can revise
the app_name as well, but it's just a setting from when you created a project, so changing it might not
be wise.
Now open your original Hello World app. In order to change to the XML format, we need to get rid of all
of the TextView data we wrote yesterday (just go ahead and delete it). Instead, we want to create a
contentView based off of our XML file, with this line of code:
?
1
setContentView(R.layout.main);
Your autofill functionality in Eclipse should start to fill in while you type at this line. In the end, your code
should look like this:
?
1
package com.example.helloandroid;
2
3
import android.app.Activity;
4
import android.os.Bundle;
5
6
public class HelloAndroid extends Activity {
7
/** Called when the activity is first created. */
8
@Override
9
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
10
setContentView(R.layout.main);
11
}
12
13
}
Now run your app just the same, and you should see the difference.
This ends your intro into XML.