Scroll down listview to bottom programmatically android

Dynamically scroll down to bottom of listview move to last element on button click.

Automatic listview scroll down feature mostly sees in chatting android apps like What’s App and Hike. Inside this feature a button display a the right side of android application screen and when app user clicks on that button it will automatically scrolls down the whole list and move to the last element of list view. So here is the complete step by step tutorial for Scroll down listview to bottom programmatically android.

android-project-download-code-button

How to Scroll down listview to bottom programmatically android.

Code for MainActivity.java file.

 package com.scrolldownlistview_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

 Button scrollDown;
 ListView CustomList;
 String[] CustomListElements = new String[] {
 "ONE",
 "TWO",
 "THREE",
 "FOUR",
 "FIVE",
 "SIX",
 "SEVEN",
 "EIGHT",
 "NINE",
 "TEN",
 "ELEVEN",
 "TWELVE",
 "THIRTEEN",
 "FOURTEEN",
 "FIFTEEN"
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 scrollDown = (Button)findViewById(R.id.button1);
 CustomList = (ListView)findViewById(R.id.listView1);
 
 final ArrayAdapter<String> adapter = new ArrayAdapter<String>
 (MainActivity.this, android.R.layout.simple_list_item_1, CustomListElements);

 CustomList.setAdapter(adapter);
 
 scrollDown.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 CustomList.smoothScrollToPosition(adapter.getCount());
 
 }
 });
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.scrolldownlistview_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:text="Scroll Down on Button click" />

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >

 </ListView>

</RelativeLayout>

Screenshots:

SCROLL-DOWN-1

Scroll down listview to bottom programmatically android

Click here to download Scroll down listview to bottom programmatically android project.

 

Leave a Reply

Your email address will not be published. Required fields are marked *