Scroll To A Selected Position In Listview

Today I will explain different methods of scrolling. We will learn how to scroll top, bottom and any specific position in a listview in android. Sometimes in programming, we need our listview to scroll to any specific position.

Let’s now try this with a code example. Follow the steps below:

Step1: Create a new Android project in your android IDE.

Step2: Write following into your layout file:

 

<LinearLayout 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:orientation="horizontal"
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.example.androidlistviewscrolling.MainActivity" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<Button
android:id="@+id/scrolltop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scroll to Top" />
<Button
android:id="@+id/scrollbottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scroll to Bottom" />
<Button
android:id="@+id/scrollto"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>

 

Step3 Write following into your java file:

 

package com.example.scrollinglistview;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

String[] months = { "India", "Mumbai", "Indore", "Bhopal", "Hyderabad", "Banglore",
"Kolkata", "Pachmadi", "Banaras", "Varanasi", "Mathura", "Lakhnau" };

ListView listView;
int toPosition = months.length;

Button btnScrollTop, btnScrollBottom, btnScrollTo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, months);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
toPosition = position;
btnScrollTo.setText("Scroll to position " + toPosition);
}});

btnScrollTop = (Button)findViewById(R.id.scrolltop);
btnScrollTop.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
listView.smoothScrollToPosition(0);
}});

btnScrollBottom = (Button)findViewById(R.id.scrollbottom);
btnScrollBottom.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
listView.smoothScrollToPosition(listView.getCount()-1);
}});

btnScrollTo = (Button)findViewById(R.id.scrollto);
btnScrollTo.setText("Scroll to position " + toPosition);
btnScrollTo.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
listView.smoothScrollToPosition(toPosition);
}});
}
}

 

Step4: Run to see the output below:

scrolllist1

Click on any item in the list (possibly over 7th item) and click on the scroll to position button to see the scrolling working.

Check my previous post on Lock Screen notification in android 5.0.

Leave a Comment: