Wednesday, September 7, 2016

Android ListView Attributes (entries)



1. Attributes of the Android ListView.

Attributes Description
divider Drawable or color to draw between list items.
dividerHeight Height of the divider.
entries Reference to an array resource that will populate the ListView.
footerDividersEnabled When set to false, the ListView will not draw the divider before each footer view.
headerDividersEnabled When set to false, the ListView will not draw the divider after each header view.
cacheColorHint Indicates that this list will always be drawn on top of solid, single-color opaque background.
choiceMode Defines the choice behavior for the view.
drawSelectorOnTop When set to true, the selector will be drawn over the selected item.
fastScrollEnabled Enables the fast scroll thumb that can be dragged to quickly scroll through the list.
listSelector Drawable used to indicate the currently selected item in the list.
scrollingCache When set to true, the list uses a drawing cache during scrolling.
smoothScrollbar When set to true, the list will use a more refined calculation method based on the pixels height of the items visible on screen.
stackFromBottom Used by ListView and GridView to stack their content from the bottom.
textFilterEnabled When set to true, the list will filter results as the user types.
transcriptMode Sets the transcript mode for the list.


2. Loading a ListView items from an Array Resource. (entries)

When we made the example adding items to the ListView in [Android ListView. (How to use an Android ListView.)], we wrote java codes that use an Adapter. But there is a way to add items directly from a resource instead of using the Adapter.
It is just to use "entries" attribute.
  * android:entries - Load items from an array resource.
        > Simpler way than using Adapter in a java code.
        > Useful to static contents, such as the menu.
The use of "entries" attribute is simple.
First, create a resource XML file that contains "<string-array>" into "/res/values" and set the name of the array resource to the ListView's "entries" attribute.
An example of using "entries" is shown below.

2.1 Create a resource XML file that contains the string array into "/res/values"

In this example, I create the resource XML file in the name of "my_array.xml".
And "myarray" is the name of the string array.
[STEP-1 for "entries"] "my_array.xml" - Create "/res/values/my_array.xml"
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="myarray">
        <item>LIST1</item>
        <item>LIST2</item>
        <item>LIST3</item>
    </string-array>
</resources>

2.2 Set the name of the array resource to the ListView's "entries" attribute.

[STEP-2 for "entries"] "activity_main.xml" - setting "entries".
    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:entries="@array/myarray" />
It's done.

2.3 Screenshot

You can see the same result as we wrote in [Android ListView. (How to use an Android ListView.)].
ListView Attributes entries

3. References.


.END.

No comments:

Post a Comment