The literature on app development describes specific techniques and frameworks to help you separate form from function.
To add buttons, boxes, and other goodies to your app, do the following:
- Launch Android Studio and begin a New Project.When you're finished with these steps, you have a brand-new project with an empty activity. The project appears in Android Studio's main window.
- In the new project's
app/res/layout
branch (in the main window's Project tool window), double-clickactivity_main.xml
.As a result, Android Studio's Designer tool displays the contents ofactivity_main.xml
. The Designer tool has two modes: Design mode for drag-and-drop visual editing and Text mode for XML code editing. So the bottom of the Designer tool has two tabs: a Design tab and a Text tab. - Click the Design tab.
In Design mode, you see the palette, the component tree, two preview screens, and the Properties pane.
If you don't see the palette, look for the little Palette button on the left edge of the Designer tool. If you click that button, the palette should appear.
The component tree has a branch labeled TextView – "Hello World!" This branch represents the text Hello World! that appears automatically as part of your app. You don't need this text in your app. - Select the
TextView - "Hello World!"
branch in the component tree, and then press Delete.The"Hello World"
branch disappears from the component tree, and the words Hello World! disappear from the preview screen. The next several steps guide you through the creation of the app. The app's layout has three different kinds of components, and each kind of component goes by several different names. Here are the three kinds of components:EditText
(also known as Plain Text): A place where the user can edit a single line of text.A common name for this kind of component is a text field.Button
: A button is a button is a button.Do you want to click the button? Go right ahead and click it.TextView
(also known as Plain TextView, Large Text, Medium Text, and so on): A place where the app displays text.Normally, the user doesn't edit the text in aTextView
component.To be painfully precise, Android's
EditText
,Button
, andTextView
components aren't really different kinds of components. EveryEditText
component is a kind ofTextView
, and everyButton
is also a kind ofTextView
. In the language of object-oriented programming, theEditText
class extends theTextView
class. TheButton
class also extends theTextView
class.
- Drag a Plain Text (that is,
EditText
) item from the palette's Widgets group to either of the preview screens.The Plain Text item may land in an ugly-looking place. That's okay. You're not creating a work of art. You're learning to write Java code. - Repeat Step 5, this time putting a Button item on the preview screen.Put the Button component below the Plain Text (EditText) component. Later, if you don't like where you put the Button component, you can easily move it by dragging it elsewhere on the preview screen.
- Repeat Step 6, this time putting a
TextView
component on the preview screen.Put theTextView
component below theButton
component but, once again, it's up to you. In the remaining steps, you change the text that appears in each component. - Select the
Button
component on the preview screen or in the component tree.As a result, the Designer tool's Properties pane displays some of the Button component's properties. After selecting theButton
component, you may see the word TextView in the Properties pane. Don't confuse this with theTextView
component that you dragged from the palette in Step 7. With the button selected, all the fields in the Properties pane refer to thatButton
component. If the appearance of the word TextView in the Properties pane confuses you, refer to the Technical Stuff icon in Step 4. (If the word TextView doesn't confuse you, don't bother reading the Technical Stuff icon!) - In the Properties pane, in the field labeled text, type the word COPY.When you do, the word COPY appears on the face of the
Button
component. You can check this by looking at the wysiwyg preview screen. In the Properties pane, you may see two fields labeled text. If so, one is for testing and the other is for running the app. When in doubt, it doesn't hurt to type the word COPY in both of those fields. - Repeat Steps 8 and 9 with your activity's
EditText
andTextView
components, but this time, don't put the word COPY into those components. Instead, remove the characters from these components.When you're finished, the preview screens look similar to the screens below. If your preview screens don't look exactly like this, don't worry about it. Your components may be scattered in different places on the preview screens, or the creators of Android Studio may have changed the way the preview screens look. As long as you have anEditText
component, aButton
component, and aTextView
component, you're okay. - Choose File → Save All to save your work so far.