Android - Simple Custom List View With Image

Operating System
In this tutorial we will create a Simple Custom List View With Image using Android. This simple app display a list with image using custom array. Android is basically a piece of software which allows your hardware to function. It used in several gadget like smartphone, tablet, and even television. The android is an open source operating system it's free and user friendly to mobile developers. So let's now do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     tools:context="com.razormist.simplelistviewwithimage.MainActivity">
  7.  
  8.     <ListView
  9.         android:layout_width="match_parent"
  10.         android:layout_height="match_parent"
  11.         android:id="@+id/lv_content"/>
  12.  
  13. </RelativeLayout>
Now that we're done with the main layout we will create then a layout that display the value of the list. First right click on resource then create a new layout resource namely laptop_list then write these codes inside your layout file.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     android:orientation="horizontal" android:layout_width="match_parent"
  5.     android:layout_height="match_parent">
  6.  
  7.  
  8.     <LinearLayout
  9.         android:orientation="horizontal"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content" >
  12.  
  13.     <TextView
  14.         android:id="@+id/tv_list"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:text="TextView"
  18.         android:layout_marginTop="30dp"
  19.         android:fontFamily="monospace" />
  20.  
  21.         <LinearLayout
  22.             android:orientation="horizontal"
  23.             android:layout_width="match_parent"
  24.             android:layout_height="wrap_content"
  25.             android:gravity="right">
  26.  
  27.             <ImageView
  28.                 android:id="@+id/iv_image"
  29.                 android:layout_width="wrap_content"
  30.                 android:layout_height="wrap_content"
  31.                  app:srcCompat="@mipmap/ic_launcher"
  32.             android:padding="3dp"/>
  33.  
  34.         </LinearLayout>
  35.     </LinearLayout>
  36.  
  37. </LinearLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.razormist.simplelistviewwithimage">
  4.  
  5.     <application
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:roundIcon="@mipmap/ic_launcher_round"
  10.         android:supportsRtl="true"
  11.         android:theme="@style/AppTheme">
  12.         <activity android:name=".MainActivity"
  13.             android:configChanges="orientation"
  14.             android:screenOrientation="portrait">
  15.             <intent-filter>
  16.                 <action android:name="android.intent.action.MAIN" />
  17.  
  18.                 <category android:name="android.intent.category.LAUNCHER" />
  19.             </intent-filter>
  20.         </activity>
  21.     </application>
  22. </manifest>

Creating the Custom Adapter

This code handle the list of data that will be needed to display. First create a new java file namely ListHandler, then open the newly create file and extend the class by adding extends ArrayAdapter. Next write these variables inside the ListHandler class
  1. String[] name_list;
  2. int[] laptop;
  3. Context context;
After assigning the variables we will now create a certain methods to handle each data that will be listed. To do that write these methods inside the class.
  1. public ListHandler(Context context, String[] name_list, int[] laptop) {
  2.         super(context, R.layout.laptop_list);
  3.         this.name_list = name_list;
  4.         this.laptop = laptop;
  5.         this.context = context;
  6.     }
  7.  
  8.     @Override
  9.     public int getCount() {
  10.         return name_list.length;
  11.     }
  12.  
  13.     @NonNull
  14.     @Override
  15.     public View getView(int position, View convertView, ViewGroup parent) {
  16.         TagHolder tagHolder = new TagHolder();
  17.         if(convertView == null) {
  18.             LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  19.             convertView = inflater.inflate(R.layout.laptop_list, parent, false);
  20.             tagHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
  21.             tagHolder.tv_list = (TextView) convertView.findViewById(R.id.tv_list);
  22.             convertView.setTag(tagHolder);
  23.         }else{
  24.             tagHolder = (TagHolder)convertView.getTag();
  25.         }
  26.  
  27.         tagHolder.iv_image.setImageResource(laptop[position]);
  28.         tagHolder.tv_list.setText(name_list[position]);
  29.         return convertView;
  30.     }
  31.  
  32.     static class TagHolder{
  33.         ImageView iv_image;
  34.         TextView tv_list;
  35.  
  36.     }

The Main Function

This code contains the main function of the application. This code will automatically call the list method to display it inside the resource layout. To start with first locate your MainActivity java file and open it, then write this variable inside the MainActivity class.
  1.  ListView lv_content;
  2.  
  3.     String[] name_list = {
  4.             "ASUS VivoBook Max X441UR",
  5.             "Acer Aspire One D270-268",
  6.             "Lenovo Ideapad 100S 11",
  7.             "Dell Inspiron 11-3162",
  8.             "HP 11-D002TU",
  9.             "MSI GV62 7RC",
  10.             "Sony Vaio VPCW126AG"
  11.     };
  12.  
  13.     int[] laptop = {
  14.             R.drawable.asus_vivobook_max_x441ur_l,
  15.             R.drawable.acer_aspire_one_d270_268_l,
  16.             R.drawable.dell_inspiron_11_3162_l,
  17.             R.drawable.lenovo_ideapad_100s_11_ph_l,
  18.             R.drawable.hp_11_d002tu_l,
  19.             R.drawable.msi_gv62_7rc_l,
  20.             R.drawable.sony_vpcw_126ag_l_5
  21.     };
Finally, initialize the require methods inside the onCreate method to run the application.
  1. lv_content = (ListView)findViewById(R.id.lv_content);
  2.         ListHandler listHandler = new ListHandler(MainActivity.this, name_list, laptop);
  3.         listHandler.sort(new Comparator<String>() {
  4.             @Override
  5.             public int compare(String lhs, String rhs) {
  6.                 return lhs.compareTo(rhs);
  7.             }
  8.         });
  9.         lv_content.setAdapter(listHandler);
Try to run the app and see if it worked. There you have it we have created a Simple Custom List View With Image using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Add new comment