ListView using Custom ArrayAdapter Android

Photo by Glenn Carstens-Peters on Unsplash

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create a Blank Activity and click on Finish button.
Step 2 : Open res -> layout ->activity_main.xml and add following code :

< 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”

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.saud.android.listview_customarrayadapter.MainActivity”>

< ListView

android : id = “@+id/listView1”

android : layout_width = “match_parent”

android : layout_height = “wrap_content”

android : layout_centerHorizontal = “true”

android : layout_centerVertical = “true” >

< / ListView >

< / RelativeLayout >

Step 2 : Open res -> layout ->Create new listitem_row.xml and add following code :

<? xml version = “1.0” encoding = “utf-8” ?>

< LinearLayout xmlns : android = “http://schemas.android.com/apk/res/android"

android : layout_width = “match_parent”

android : layout_height = “match_parent”

android : layout_gravity = “center”

android : gravity = “center”

android : orientation = “horizontal” >

< TextView

android : id = “@+id/num_name”

android : layout_width = “wrap_content”

android : layout_height = “wrap_content”

android : layout_gravity = “center”

android : gravity = “left”

android : textStyle = “bold” / >

< ImageView

android : id = “@+id/num_images”

android : layout_width = “wrap_content”

android : layout_height = “wrap_content”

android : layout_gravity = “center”

android : contentDescription = “TODO” / >

< / LinearLayout >

Step 4 : Open src -> package ->MainActivity.java and add following code :

package com . saud. android .listview_customarrayadapter ;

import android . app . Activity ;

import android . os . Bundle ;

import android . view . View ;

import android . widget . AdapterView ;

import android . widget . ListView ;

import android . widget . Toast ;

public class MainActivity extends Activity {

ListView list ;

String [ ] numNames = { “Zero” , “One” , “Two” ,”Three” , “Four” , “Five” , “Six” ,

“Seven” , “Eight” , “Nine”

} ;

Integer [ ] numImage = { R . drawable . zero , R .drawable . one , R . drawable . two ,

R . drawable . three , R . drawable . four , R .drawable . five , R . drawable . six ,

R . drawable . seven , R . drawable . eight , R .drawable . nine } ;

@Override

protected void onCreate ( BundlesavedInstanceState ) {

super . onCreate ( savedInstanceState ) ;

setContentView ( R . layout . activity_main ) ;

CustomListAdapter adapter = newCustomListAdapter ( this , numNames ,

numImage ) ;

list = ( ListView ) findViewById ( R . id . listView1 );

list . setAdapter ( adapter ) ;

list . setDividerHeight ( 1 ) ;

list . setOnItemClickListener ( new AdapterView .OnItemClickListener ( ) {

@Override

public void onItemClick ( AdapterView < ? >parent , View view ,

int position , long id ) {

Toast . makeText ( MainActivity . this ,

“You Clicked at “ + numNames [ +position ] ,

Toast . LENGTH_SHORT ) . show ( ) ;

}

} ) ;

}

}

Step 5 : Open src -> package ->Create new class CustomListAdapter.java and add following code :

package com . skholingua . android .listview_customarrayadapter ;

import android . app . Activity ;

import android . content . Context ;

import android . view . LayoutInflater ;

import android . view . View ;

import android . view . ViewGroup ;

import android . widget . ArrayAdapter ;

import android . widget . ImageView ;

import android . widget . TextView ;

public class CustomListAdapter extends ArrayAdapter<String> {

private final Activity context ;

private final String [ ] listNumNames ;

private final Integer [ ] listNumImages ;

public CustomListAdapter ( Context cont , String [ ]listNumNames ,

Integer [ ] listNumImages ) {

super ( cont , R . layout . listitem_row ,listNumNames ) ;

this . context = ( Activity ) cont ;

this . listNumNames = listNumNames ;

this . listNumImages = listNumImages ;

}

static class ViewHolder {

public TextView txtTitle ;

public ImageView imageViewoff ;

}

@Override

public View getView ( int position , View rowView ,ViewGroup parent ) {

ViewHolder holder = null ;

if ( rowView == null ) {

holder = new ViewHolder ( ) ;

LayoutInflater inflater = context .getLayoutInflater ( ) ;

rowView = inflater . inflate ( R . layout .listitem_row , parent , false ) ;

holder . txtTitle = ( TextView ) rowView .findViewById ( R . id . num_name ) ;

holder . imageViewoff = ( ImageView ) rowView

. findViewById ( R . id . num_images ) ;

rowView . setTag ( holder ) ;

} else {

holder = ( ViewHolder ) rowView . getTag ( ) ;

}

holder . txtTitle . setText ( listNumNames [position ] ) ;

holder . imageViewoff . setImageResource (listNumImages [ position ] ) ;

return rowView ;

}

}

Step 6 : Open AndroidManifest.xml and add following code :

<? xml version = “1.0” encoding = “utf-8” ?>

< manifest xmlns : android = “http://schemas.android.com/apk/res/android"

package =”com.skholingua.android.listview_customarrayadapter”

android : versionCode = “1”

android : versionName = “1.0” >

< uses — sdk

android : minSdkVersion = “16”

android : targetSdkVersion = “19” / >

< application

android : allowBackup = “true”

android : icon = “@drawable/ic_launcher”

android : label = “@string/app_name”

android : theme = “@style/AppTheme” >

< activity

android : name = “.MainActivity”

android : label = “@string/app_name” >

< intent — filter >

< action android : name =”android.intent.action.MAIN” / >

< category android : name =”android.intent.category.LAUNCHER” / >

< / intent — filter >

< / activity >

< / application >

< / manifest >

--

--

--

I’m a good husband, father and Senior Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What Happened to my Subclass Android Application?

App Startup [All official development resources]

ListView using ListFragment Android

Flutin Live RTMP Pull Feature Guide — How You Can Pull Live Streams From Encoding Softwares

Jetpack ViewModel [All official development resources]

Google Play Billing [All official development resources]

How to automate your dependencies upgrades with GitHub Actions

Application Class

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodingwithSaud

CodingwithSaud

I’m a good husband, father and Senior Developer.

More from Medium

How to get started with Android Development

How to perform search using channel for Paging 3

Android Development — MockK 101 (Part 1)

Context and Shared Preferences in Android — An Intro