We need to create the layout to hold our list. Add code inside of the LinearLayout element so the whole file looks like this:

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

<LinearLayout xmlns:android="http://schemas"

android:layout width="wrap content"

android:layout height="wrap content">

<ListView android:id="@android:id/list"

android:layout width="wrap content"

android:layout height="wrap content


<TextView android:id="@android:id/empty"

android:layout width="wrap content"

android:layout height="wrap content" android:text="@string/no notes"/> </LinearLayout>

• The @ symbol in the id strings of the ListView and TextView tags means that the XML parser should parse and expand the rest of the id string and use an ID resource.

• The ListView and TextView can be thought as two alternative views, only one of which will be displayed at once. ListView will be used when there are notes to be shown, while the TextView (which has a default value of "No Notes Yet!" defined as a string resource in res/values/strings.xml) will be displayed if there aren't any notes to display.

• The list and empty IDs are provided for us by the Android platform, so, we must prefix the id with android: (e.g., @android:id/list).

• The View with the empty id is used automatically when the ListAdapter has no data for the ListView. The ListAdapter knows to look for this name by default. Alternatively, you could change the default empty view by using setEmptyView(View) on the ListView.

More broadly, the android.R class is a set of predefined resources provided for you by the platform, while your project's R class is the set of resources your project has defined. Resources found in the android.R resource class can be used in the XML files by using the android: name space prefix (as we see here).

0 0

Post a comment