Switch to a TableLayout
First, open LunchList/res/layout/main.xml and modify its contents to look like the following:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView android:text="Name:" />
<EditText android:id="@+id/name" />
</TableRow>
<TableRow>
<TextView android:text="Address:" />
<EditText android:id="@+id/addr" />
</TableRow>
<Button android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
/>
</TableLayout>
Notice that we replaced the three LinearLayout containers with a TableLayout and two TableRow containers. We also set up the EditText column to be stretchable.
Recompile and reinstall the application, then run it in the emulator. You should see something like this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView android:text="Name:" />
<EditText android:id="@+id/name" />
</TableRow>
<TableRow>
<TextView android:text="Address:" />
<EditText android:id="@+id/addr" />
</TableRow>
<Button android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
/>
</TableLayout>
Notice that we replaced the three LinearLayout containers with a TableLayout and two TableRow containers. We also set up the EditText column to be stretchable.
Recompile and reinstall the application, then run it in the emulator. You should see something like this:
Using a TableLayout
Notice how the two EditText fields line up, whereas before, they appeared immediately after each label.
NOTE: At this step, or any other, when you try to run your application, you may get the following screen:
Notice how the two EditText fields line up, whereas before, they appeared immediately after each label.
NOTE: At this step, or any other, when you try to run your application, you may get the following screen:
A "force-close" dialog
If you encounter this, first try to do a full rebuild of the project. In Eclipse, this would involve doing Project > Force Clean. At the command line, use ant clean or delete the contents of your bin/ and gen/ directories, then ant install. If the problem persists after this, then there is a bug in your code somewhere. You can use adb logcat, DDMS, or the DDMS perspective in Eclipse to see the Java stack trace associated with this crash, to help you perhaps diagnose what is going on.
If you encounter this, first try to do a full rebuild of the project. In Eclipse, this would involve doing Project > Force Clean. At the command line, use ant clean or delete the contents of your bin/ and gen/ directories, then ant install. If the problem persists after this, then there is a bug in your code somewhere. You can use adb logcat, DDMS, or the DDMS perspective in Eclipse to see the Java stack trace associated with this crash, to help you perhaps diagnose what is going on.


No comments:
Post a Comment