how to create custom listview in android

how to create custom listview in android

 how to create custom listview in android :-

how to create custom listview in android



1. Create a Activity MainActivity :-

:- in XML file

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/side_nav_bar">

    <ListView
        android:id="@+id/study_mode_list_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="8dp"
        android:divider="#8A46A005"
        android:dividerHeight="1.5dp"
        android:footerDividersEnabled="true"
        android:layout_marginRight="8dp"/>

</RelativeLayout>

In JAVA File :-

package com.example.demodrawernaviga2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.demodrawernaviga2.R;

public class CategoryStudy extends AppCompatActivity {

    int book[] = {R.drawable.stude_mode, R.drawable.stude_mode, R.drawable.stude_mode,
            R.drawable.stude_mode, R.drawable.stude_mode, R.drawable.stude_mode, R.drawable.stude_mode, R.drawable.stude_mode, R.drawable.stude_mode,
            R.drawable.stude_mode, R.drawable.stude_mode, R.drawable.stude_mode,
            R.drawable.stude_mode};


    String subject[] = {"Hindi","English","Science","Maths","Biology","Physics","Chemistry","Geography","Citizenship",
            "Business Studies","Psychology","Sociology","Economics"};

    int next[] = {R.drawable.next, R.drawable.next, R.drawable.next, R.drawable.next, R.drawable.next, R.drawable.next,
            R.drawable.next, R.drawable.next, R.drawable.next, R.drawable.next, R.drawable.next, R.drawable.next,
            R.drawable.next};

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

        ListView listView = (ListView)findViewById(R.id.study_mode_list_item);

        CustomAdapterStudyMode customAdapter = new CustomAdapterStudyMode(getApplicationContext(),book,subject,next);
        listView.setAdapter(customAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {



                switch (i){

                    case 1:
                        Intent in1=new Intent(getApplicationContext(),GameCate.class);
                        startActivity(in1);
                        break;

                    case 2:
                        Intent in2=new Intent(getApplicationContext(),MainActivity.class);
                        startActivity(in2);
                        break;

                    case 3:
                        Intent in3=new Intent(getApplicationContext(),SpalaceAct.class);
                        startActivity(in3);
                        break;

                    case 4:
                        Intent in4=new Intent(getApplicationContext(),QuizCate.class);
                        startActivity(in4);
                        break;

                }
                //show the selected image in toast according to position
            }
        });



    }
}

2. Create a activity_list.xml File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/side_multi_color_list">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:weightSum="100">


        <ImageView
            android:id="@+id/study_mode"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/stude_mode"
            android:layout_weight="20"
            android:elevation="5dp"/>

        <TextView
            android:id="@+id/study_mode_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hindi"
            android:textStyle="italic|bold"
            android:fontFamily="serif-monospace"
            android:textSize="20dp"
            android:layout_weight="70"
            android:layout_marginTop="10dp"
            android:textColor="#ffffff"/>


        <ImageView
            android:id="@+id/study_mode_next"
            android:layout_width="20dp"
            android:layout_height="25dp"
            android:src="@drawable/next"
            android:layout_weight="10"
            android:layout_marginTop="7dp"
            android:elevation="5dp"/>





    </LinearLayout>


</LinearLayout>

3. Now Create a CustomAdapter JAVA Class :-

package com.example.demodrawernaviga2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class CustomAdapterStudyMode extends BaseAdapter {
    Context context;
    int book[];
    String subject[];
    int next[];
    LayoutInflater inflter;

    public CustomAdapterStudyMode(Context applicationContext, int[] book, String[] subject, int[] next) {
        this.context = context;
        this.book = book;
        this.subject = subject;
        this.next = next;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return subject.length;
    }

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.activity_study_list, null);
        ImageView icon = (ImageView) view.findViewById(R.id.study_mode);
        TextView tv = (TextView) view.findViewById(R.id.study_mode_text);
        ImageView icon1 = (ImageView) view.findViewById(R.id.study_mode_next);
        icon.setImageResource(book[i]);
        tv.setText(subject[i]);
        icon1.setImageResource(next[i]);
        return view;
    }
}