Now, the moment you've all been waiting for...time to build your first Android app!
In addition to finally letting you get your feet wet, this will help us test our install to make sure everything is in proper working order. Be warned, since the software installation will be proven good to go, any errors after this point will most likely be your own fault and you'll no be longer allowed to blame the IDE!
In keeping with tradition, we’ll call our first app “HelloWorld”. Google has provided a walk-through for their own version of Hello World app, but we're going to build our own.
If everything went as expected, you should see something closely resembling this:
At this point we have the framework in place for a complete Android application. In fact, we already have an application that will actually run! I realize that you may not understand a good portion of what you're looking at yet, and that's OK for now. Remember, one of our goals at this point is to make sure everything runs properly...so let's run this app! Click on the green 'Run' icon in the menu bar (the Play button beside the bug).
You will be given the option to choose which device to run your app on. We want to use the AVD we created previously, so choose it from the selector and click OK:
If everything is working properly, in short time you will see the fruits of our labor:
Now that we have a running app, let's have some fun and make some changes.
This is a Mess!
That's Better!
Not too bad so far...but we can do better! Let's add a little bit of interactivity:
Open MainActivity.java.
We want to make our app do something with the items in the layout file, so we need a way to reference them in our code. This is done similar to the way you might instantiate a new Java class. Modify the onCreate method to look like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
}
Now that we have a way to reference our Views in the Java code, let's add some functionality. What's our desired end state? For this example, we're going to make it so that when the ToggleButton is tapped, the background color toggles between white and black. To put it another way, our app will "listen" for you to tap on the button, and change the color of the RelativeLayout's background. To do this, we need to add an Action Listener. Modify your onCreate() method again..this time we'll add the code necessary to capture the user clickign on the ToggleButton. Your code should look like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the layout file this activity will use
setContentView(R.layout.activity_main);
// define variables that point to views defined in our layout file
final RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
// create an action listener for the toggle button
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
// We can do it this way...
mainLayout.setBackgroundColor(Color.WHITE);
} else {
// or use hexadecimal color codes for more options
mainLayout.setBackgroundColor(Color.parseColor("#000000"));
}
}
});
}
That does it. All of the logic is in place...Now run it! Tap on the ToggleButton to see what happens...
There you have it. We'll call this app "The Amazing Flashlight"®
We've touched on a lot of stuff , so let's recap. You have now been shown how to: