Scroll listview to top position in android programmatically

How to dynamically scroll list view again at the top of its list element item in android activity.

In this tutorial we are simply setting up smoothScrollToPosition(0) to listview on button click event. This method will enable the automatic scrolling functionality on list view and list view will dynamically scroll at its top element. So here is the complete step by step tutorial for Scroll listview to top position in android programmatically.

android-project-download-code-button

How to Scroll listview to top position in android programmatically.

Code for MainActivity.java file.

 package com.scrolllistviewtotopposition_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[] {
 "Android Examples",
 "Android Studio",
 "SDK Manager",
 "Eclipse",
 "Java",
 "JDK",
 "Android Examples",
 "Android Studio",
 "SDK Manager",
 "Eclipse",
 "Java",
 "Android Examples",
 "Android Studio",
 "SDK Manager",
 "Eclipse",
 "Java",
 "Android Examples",
 "Android Studio",
 "SDK Manager",
 "Eclipse",
 "Java",};
 
 @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) {
 // TODO Auto-generated method stub
 
 listView.smoothScrollToPosition(0);
 
 }
 });
 
 
 }
}

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

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Scroll listview to top position" />

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

</RelativeLayout>

Screenshots:

Scroll listview to top position in android programmatically

 

scroll-listview-2

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

Leave a Reply

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