Scroll listview to specific position in android programmatically

How to swipe down listview automatically to a given item position in android on button click.

In this tutorial we are creating a listview contain multiple items in it now we are creating a custom button and setting up on click function on button. Inside that function we will define the item position on which we want to scroll down when user click on button. So here is the complete step by step tutorial for Scroll listview to specific position in android programmatically.

android-project-download-code-button

How to Scroll listview to specific position in android programmatically.

Code for MainActivity.java file.

 package com.scrolllistviewspecificposition_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 {

 ListView listView;
 Button button;
 String[] listValue = new String[] {
 "ONE",
 "TWO",
 "THREE",
 "FOUR",
 "FIVE",
 "SIX",
 "SEVEN",
 "EIGHT",
 "NINE",
 "TEN",
 "ELEVEN",
 "TWELVE",
 "THIRTEEN"
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listView = (ListView)findViewById(R.id.listView1);
 button = (Button)findViewById(R.id.button1);
 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listValue);
 
 listView.setAdapter(adapter); 
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 listView.smoothScrollToPosition(7);
 
 }
 });
 }
 
}

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.scrolllistviewspecificposition_android_examples.com.MainActivity" >

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="375dp"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >
 </ListView>

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/listView1"
 android:layout_alignParentBottom="true"
 android:text="Click Here To Scroll listview to specific position in android programmatically" />

</RelativeLayout>

Screenshots:

scroll-listview

Scroll listview to specific position in android programmatically

Click here to download Scroll listview to specific position in android programmatically project with source code.

Leave a Reply

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