How to Use Custom Listview in Android to Show Data in Paged Format With Load More functionality

0
Shares
How to Use Custom Listview in Android to Show Data in Paged Format With Load More functionality
4 (80%) 1 vote

custom listview in android

Have you ever needed to display a group of related items in a list?

Many times, right?

Displaying a list is necessary to the function of almost any app that queries a set of data and returns a list of results. Therefore, an app needs to do this at one point or another.

For example, you have a chat app that allows users to find friends and chat with them. But, you’ll have to display the list of user’s friends in a list that let the user select which friend to connect with.

For this, custom listview in Android is used to show data in paged format. Custom listview in Android easily creates scrollable lists in app to show data in paged format.

In today’s Android app tutorial, we’ll learn how to use Custom listview in Android to show data in paged format with load more functionality.

Let’s Get Started

Create a new project in Android Studio.

screenshot-from-2017-05-22-17-18-04

Select target Android device.

screenshot-from-2017-05-16-11-46-15

Add an activity to mobile => Select Empty Activity.

screenshot-from-2017-05-16-10-45-27

Customize the activity.

screenshot-from-2017-05-16-10-45-45

Add dependecy in build.gradle file

compile 'com.android.support:appcompat-v7:25.3.1'

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity.class

package com.spaceo.loadmorebutton;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.spaceo.loadmorebutton.adapter.ListViewAdapter;
public class MainActivity extends AppCompatActivity {
ListView lv;
int lastPosition;
ArrayList<String> listItem;
ListViewAdapter listViewAdapter;

Button btnLoadMore;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItem=new ArrayList<String>();
lv = (ListView) findViewById(R.id.lv);
listItem = new ArrayList<String>();
popuLateList();
listViewAdapter=new ListViewAdapter(this,listItem);
lv.setAdapter(listViewAdapter);
btnLoadMore = new Button(this);
btnLoadMore.setText(getResources().getString(R.string.btnLoadMore));
// Adding Load More button to lisview at bottom
lv.addFooterView(btnLoadMore);
btnLoadMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
loadMoreItems();
}
});
}
private void loadMoreItems() {
lastPosition = lv.getFirstVisiblePosition();
for(int i=1;i<11;i++)
{
listItem.add("item");
}
setAdapter();
lv.setSelection(lastPosition+1);
}
private void setAdapter() {
listViewAdapter=new ListViewAdapter(this,listItem);
lv.setAdapter(listViewAdapter);
}
private void popuLateList() {
for(int i=1;i<11;i++)
{
listItem.add("item");
}
setAdapter();
}
}

ListViewAdapter.class

package com.spaceo.loadmorebutton.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.spaceo.loadmorebutton.R;
public class ListViewAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> list;
private static LayoutInflater inflater = null;
public ListViewAdapter(Activity activity, ArrayList<String> list) {
this.activity = activity;
this.list = list;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.list_item, null);
TextView name = (TextView) vi.findViewById(R.id.name); // title
name.setText((list.get(position)) + (position + 1));
return vi;
}
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<!-- List item name -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:textStyle="bold"
android:gravity="left"
android:padding="10dip"
android:textSize="16dp">
</TextView>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spaceo.loadmorebutton">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Build and Run!

There are other ways to create lists such as ListFragment and ListActivity. Both these links will take you to Android developer site so that you can learn more about them.

Now, if you want to know more about the inner workings or how to add Android custom listview in a complex Android app, hire Android app developer to make sure the Android custom listview is implemented efficiently in your Android app.

Get a free copy of Android Listview Example from Github.

 
0
Shares
 

Have an Idea for Android App? Let's Discuss!

Get your free consultation now