Tuesday, August 11, 2015

How to scroll at the bottom of a ListView in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#97c740"
    >
    <!--
        android:transcriptMode
            Sets the transcript mode for the list. In transcript mode, the list
            scrolls to the bottom to make new items visible when they are added.

            disabled : Disables transcript mode. This is the default value.
            normal : The list will automatically scroll to the bottom when a data
                     set change notification is received and only if the last
                     item is already visible on screen.
            alwaysScroll : The list will automatically scroll to the bottom,
                           no matter what items are currently visible.

        android:stackFromBottom
            Used by ListView and GridView to stack their content from the bottom.
            Must be a boolean value, either "true" or "false".
     -->
    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:transcriptMode="alwaysScroll"
        android:stackFromBottom="true"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get reference of widgets from XML layout
        final ListView lv = (ListView) findViewById(R.id.lv);

        // Initializing a new String Array
        String[] fruits = new String[] {
                "Abiu",
                "Batuan",
                "Black Mulberry",
                "Cape Gooseberry",
                "Desert banana",
                "Eastern May Hawthorn",
                "Fibrous Satinash",
                "Gooseberry",
                "Hairless rambutan",
                "Illawarra Plum",
                "Jelly Palm",
                "Kepel fruit",
                "Little gooseberry tree",
                "Mammee Apple",
                "Nagami Kumquat",
                "Oil Palm",
                "Peanut butter fruit",
                "Queensland Ebony",
                "Red Mulberry",
                "Sageretia",
                "Tahitian apple",
                "Ugni",
                "Vanilla",
                "Watermelon",
                "Yellow Granadilla",
                "Zig Zag Vine"
        };

        // Create a List from String Array elements
        final List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));

        // Create an ArrayAdapter from List
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, fruits_list);

        // DataBind ListView with items from ArrayAdapter
        lv.setAdapter(arrayAdapter);
    }
}
More android examples