O'Reilly logo

Learning Android by Marko Gargenta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The StatusActivity Layout

Let’s start by designing the user interface for our screen where we’ll enter the new status and click a button to update it.

By default, Eclipse created a file called main.xml under the res/layout folder. For consistency purposes, we should rename this file to status.xml to match our StatusActivity. To rename a file in Eclipse, right-click on it, choose RefactorRename…, and enter the new name. Eclipse is somewhat smart about renaming files and does more than just change the name. It also offers to look up all references to this file and update those as well. Although this feature works well when renaming a Java file, it is not fully automatic with XML files. So, renaming this file requires us to change the line in Java where we refer to it via the R class. To do that, in your StatusActivity’s onCreate(), change setContentView(R.layout.main); to setContentView(R.layout.status);.

This screen will have four components:

  • A title at the top of the screen. This will be a TextView widget.

  • A big text area to type our 140-character status update. We’ll use an EditText widget for this purpose.

  • A button to click to update the status. This will be a Button widget.

  • A layout to contain all these widgets and lay them out one after another in a vertical fashion. For this screen, we’ll use LinearLayout, one of the more common ones.

Example 6-1 contains the source code for our StatusActivity layout.

Example 6-1. res/layout/status.xml

<?xml version="1.0" encoding="utf-8"?> <!-- Main ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required