All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Custom Listview In Android

Last Updated : Mar 11, 2024

Custom Listview In Android

In this article we will show you the solution of custom listview in android, Android's ListView allows for the display of lists with scrollable items. You can use it to display the data as a scrollable list. Afterwards, users can click on any list item to choose it.

Since ListView is by default scrollable, we don't need to utilise a scroll view or any other add-ons.

The ListView component is frequently used in Android apps. The phone contact book serves as a very popular illustration of a ListView because it shows a list all your contacts and user information when you click on one of them.

Before diving into the listview example, it is important to understand what a listview is.

A listview is indeed a collection of things that are taken from databases, arraylists, or lists.

Most listview applications involve a vertical collection of objects that may be scrolled through and clicked on individually.

Almost all Android applications will use the Android ListView, making it one of the controls that are used the most frequently in Android.

Mobile phones have rather small screens, thus there isn't much content that's able to be displayed on them at once.

Step By Step Guide On Custom Listview In Android :-

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity
{
    ListView list;
    String[] web =
    {
        "Google Plus",
            "Twitter",
            "Windows",
            "Wordpress",
    };
    Integer[] imageId =
    {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
        R.drawable.image6,
        R.drawable.image7
    };
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomList adapter = new CustomList(MainActivity.this, web, imageId);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView <? > parent, View view,
            int position, long id)
            {
                Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
            }
        });
    }
}
  1. Create a new project in Android Studio called Empty Activity .
  2. Utilizing the Activity_main.xml contains a list view as its root view. Put the following code in the activity_main.xml file and be sure to include the correct ListView ID.
  3. Constructing a unique ListView View, The folder creates a layout named custom list view.xml under layout and executes the code below.
  4. Create a unique class for a unique layout We manually call the getter and setter for the custom list view layout by constructing this custom class. Make a custom class called NumbersView in the Application's package folder and run the subsequent code.
  5. Next, develop a specialized NumbersView-based ArrayAdapter class that extends the ArrayAdapter class. This class should be created in the same package as the NumbersViewAdapter.java file.
  6. Using the MainActivity.java file In this situation, a custom ArrayList must be made out of all the items that are Image for ImageView, Text for TextView 1, and Text for TextView 2, respectively.

Conclusion :-

Android Listview can be used to display a lot of data sequentially when there is a need to.

Users of Android Listview can scroll the data by sliding their finger up and down, but the original data will scroll off the edge of the screen.

I hope this article on custom listview in android helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪