Pages

Chapter 8.1: Custom ListvVew In Android

Custom ListView In Android

In this tutorial we will learn how to implement custom listview using BaseAdapter in Android Studio with an example. We will create a separate layout that displays a image and text and inflates that layout in listview using BaseAdapter.

Example:

  • Create a new project and name as CustomListViewInAndroid.
  • Design a Listview in main layout.
  • Create a new layout, right click on layout -> New -> Layout resource file -> and name it as list_cell.
  • Design a ImageView and two TextView's in list_cell.
File: activity_main.xml:


<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"
    tools:context="com.example.hss_24.customlistviewinandroid.MainActivity">

    <ListView
        android:id="@+id/custom_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</RelativeLayout> 

File: list_cell.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal"
    android:padding="10dp">

    <ImageView
        android:id="@+id/img"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingLeft="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="name"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="22sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txt_api"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="api"
            android:textColor="@color/colorAccent" />

    </LinearLayout>


</LinearLayout>

  • Lets go for the coding part, create an array to display names and another to display api names and one more array to display images.
//   declare Array
    String[] names_of_android = {"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb",
            "IceCream Sandwich", "Jelly Bean","Kitkat", "Lollipop", "Marshmallow"};

    //   declare api_array
    String[] api_names = {"3", "4", "5", "8", "9", "11",
            "14", "16", "19", "21", "23"};

    //   declare images array
    int [] images = {R.drawable.cupcake,R.drawable.donut,R.drawable.eclair,R.drawable.froyo,R.drawable.gingerbread,
            R.drawable.honeycomb,R.drawable.icecream_sandwich,R.drawable.jellybean,R.drawable.kitkat,
            R.drawable.lollipop,R.drawable.marsh_mallow};
  • Create a new class MyAdapter() which extends BaseAdapter.
public class MyAdapter extends BaseAdapter {
        public MyAdapter() {
            super();
        }

        @Override
        public int getCount() {
            // set position of the list items
            return names_of_android.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // create a layout inflater and inflate the layout list_cell
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            convertView = inflater.inflate(R.layout.list_cell,null);

            // define the imageview and textviews
            ImageView imageView = (ImageView)convertView.findViewById(R.id.img);
            TextView textView_name = (TextView)convertView.findViewById(R.id.txt_name);
            TextView textView_api = (TextView)convertView.findViewById(R.id.txt_api);

            // define position of the items
            imageView.setImageResource(images[position]);
            textView_name.setText(names_of_android[position]);
            textView_api.setText(api_names[position]);
            return convertView;
        }
    }
  • Do the following code in MainActivity.java file.
File: MainActivity.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //   declare Array
    String[] names_of_android = {"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb",
            "IceCream Sandwich", "Jelly Bean","Kitkat", "Lollipop", "Marshmallow"};

    //   declare api_array
    String[] api_names = {"3", "4", "5", "8", "9", "11",
            "14", "16", "19", "21", "23"};

    //   declare images array
    int [] images = {R.drawable.cupcake,R.drawable.donut,R.drawable.eclair,R.drawable.froyo,R.drawable.gingerbread,
            R.drawable.honeycomb,R.drawable.icecream_sandwich,R.drawable.jellybean,R.drawable.kitkat,
            R.drawable.lollipop,R.drawable.marsh_mallow};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView_custom = (ListView)findViewById(R.id.custom_list);
        listView_custom.setAdapter(new MyAdapter());
    }

    public class MyAdapter extends BaseAdapter {
        public MyAdapter() {
            super();
        }

        @Override
        public int getCount() {
            // set position of the list items
            return names_of_android.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // create a layout inflater and inflate the layout list_cell
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            convertView = inflater.inflate(R.layout.list_cell,null);

            // define the imageview and textviews
            ImageView imageView = (ImageView)convertView.findViewById(R.id.img);
            TextView textView_name = (TextView)convertView.findViewById(R.id.txt_name);
            TextView textView_api = (TextView)convertView.findViewById(R.id.txt_api);

            // define position of the items
            imageView.setImageResource(images[position]);
            textView_name.setText(names_of_android[position]);
            textView_api.setText(api_names[position]);
            return convertView;
        }
    }
}

  • Run the Application.
Output:


Download project here
Thank you!!!
Please like and share...


Unknown

Author: Nouman Baig

Editor: Mohammad Jabir

No comments:

Post a Comment