The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
Later in these tutorials, we are going to allow the user to write down notes related to the book. These notes will be stored in a database and can be viewed, modified, or deleted as the user sees fit.
In this tutorial, we are going to set up the layout resource to allow the user to fill in these notes.
This is a continuation of the work we did in the previous tutorial.
Right-click over the
res/layout/ directory and choose New > “Layout resource file”
from the context menu. This brings up the New Layout Resource File dialog:
Figure 149: Android Studio New Layout Resource File Dialog
editor as the “Layout File Name”, leave the rest of the dialog
alone, and click the “OK” button.
That should have opened up the graphical layout editor for this new
editor layout resource:
Figure 150: Graphical Layout Editor
Drag a “Multiline Text” widget from the Text section of the
Palette into the preview area.
In the attributes pane, change the
android:layout_height each to be
change the ID to
Figure 151: Graphical Layout Editor Attributes Pane
Next, in the attributes pane, click on the “hint” entry, then click the “…” button to the right of it. This will open up a string resource picker dialog:
Figure 152: Android Studio String Resource Picker Dialog
Towards the upper right, click the “Add new resource” drop-down and choose “New string Value…” from it, to bring up the string resource editor dialog:
Figure 153: Android Studio New String Resource Dialog
Fill in a resource name of
hint and a value of
Enter notes here.
Leave the rest of the dialog alone, and click OK.
Then, back in the attributes pane, switch to viewing all of the attributes,
rather than just the subset. Scroll down to the
fold it open, and change the checked values to “top” and “start”.
In Android Studio 3.0, this will look bizarre, with four other checkmarks
appearing, due to a bug in the IDE.
Figure 154: Layout Editor Attributes Pane, Showing Gravity Options
If you look at the layout XML in the Text sub-tab, you should have something like this:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="match_parent" android:layout_height="match_parent" android:inputType="textMultiLine" android:ems="10" android:id="@+id/editor" android:hint="@string/hint" android:gravity="top|start" /> </LinearLayout>
Your hint may appear to be
"Enter notes here", as if you had directly typed
that in rather than creating a string resource.
As was covered earlier, Android Studio is lying to you.
Click on the
"Enter notes here" to see the actual string resource reference.
The preview of this section is in the process of being translated from its native Klingon.