Introduction

Android is one of the most popular operating systems for the mobile. The maximum number of Applications contains the Custom List View module. It shows our information or the data with the images in the ListView. I will show you how to create Custom ListView in an Android Application, using Android Studio. Android is the kernel-based operating system. It allows the user to modify the GUI components and the source code.
Requirements
Steps to be followed are given below
Follow the steps given below to create Custom ListView in your Android Application, using an Android Studio.
Step 1
Open Android Studio and start the new project.
Android
Step 2
Put the Application name and the company domain. If you wish to use C++ to code the project, include C++ support, followed by clicking Next.
Android
Step 3
Select the Android minimum SDK. Afterward, you need to choose the minimum SDK. It will show an approximate percentage of the people using the SDK, followed by clicking Next.
Android
Step 4
Choose the basic activity, followed by clicking Next.
Android
Step 5
Put the activity name and the layout name. Android Studio basically takes the Java class name, which you provided in the activity name and click Finish.
Android
Step 6
First, you need to design the Application. Thus, go to activity_main.xml, followed by clicking the text bottom. This XML file contains designing the code for an Android app. In the activity_main.xml, copy and paste the code given below.
Activity_main.xml code
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  4. android:paddingRight="@dimen/activity_horizontal_margin"
  5. android:orientation="vertical"
  6. android:paddingTop="@dimen/activity_vertical_margin"
  7. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
  8. <ListView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:id="@+id/listView" />
  12. </LinearLayout>
The code given above is to create the ListView in Activity_main.xml page.
Android
Step 7
Create a new list_layout.xml file (File ⇒ New ⇒Activity⇒Empty_activity).
Go to list_layout.xml, followed by clicking the text bottom. This XML file contains the designing code for an Android app. In the list_layout.xml, copy and paste the code given below.
list_layout.xml code
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <ImageView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_rowSpan="2"
  9. android:id="@+id/imageView"
  10. android:layout_row="0"
  11. android:layout_column="0" />
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="New Text"
  16. android:layout_columnWeight="1"
  17. android:id="@+id/textViewName"
  18. android:layout_row="0"
  19. android:layout_column="1" />
  20. <TextView
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="New Text"
  24. android:layout_columnWeight="1"
  25. android:id="@+id/textViewDesc"
  26. android:layout_row="1"
  27. android:layout_column="1" />
  28. </GridLayout>
The code given above is used to assign List View text messages and the images. Add your images into the drawable folder.
Android
Step 8
In MainActivity.java, copy and paste the code given below. Java programming is the backend language for Android. Do not replace your package name, else an app will not run.
MainActivity.java code
  1. package ganeshannt.customlistviewsample;
  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.view.Menu;
  5. import android.view.MenuItem;
  6. import android.view.View;
  7. import android.widget.AdapterView;
  8. import android.widget.ListView;
  9. import android.widget.Toast;
  10. public class MainActivity extends AppCompatActivity {
  11. private ListView listView;
  12. private String names[] = {
  13. "HTML",
  14. "CSS",
  15. "Java Script",
  16. "Wordpress"
  17. };
  18. private String desc[] = {
  19. "The Powerful Hypter Text Markup Language 5",
  20. "Cascading Style Sheets",
  21. "Code with Java Script",
  22. "Manage your content with Wordpress"
  23. };
  24. private Integer imageid[] = {
  25. R.drawable.html,
  26. R.drawable.css,
  27. R.drawable.js,
  28. R.drawable.wp
  29. };
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. CustomList customList = new CustomList(this, names, desc, imageid);
  35. listView = (ListView) findViewById(R.id.listView);
  36. listView.setAdapter(customList);
  37. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  38. @Override
  39. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  40. Toast.makeText(getApplicationContext(),"You Clicked "+names[i],Toast.LENGTH_SHORT).show();
  41. }
  42. });
  43. }
  44. @Override
  45. public boolean onCreateOptionsMenu(Menu menu) {
  46. // Inflate the menu; this adds items to the action bar if it is present.
  47. getMenuInflater().inflate(R.menu.menu_main, menu);
  48. return true;
  49. }
  50. @Override
  51. public boolean onOptionsItemSelected(MenuItem item) {
  52. // Handle action bar item clicks here. The action bar will
  53. // automatically handle clicks on the Home/Up button, so long
  54. // as you specify a parent activity in AndroidManifest.xml.
  55. int id = item.getItemId();
  56. //noinspection SimplifiableIfStatement
  57. if (id == R.id.action_settings) {
  58. return true;
  59. }
  60. return super.onOptionsItemSelected(item);
  61. }
  62. }
Step 9
Create a new CustomList.java file (File ⇒ New ⇒Java class).
In the CustomList.java, copy and paste the code given below. Java programming is the backend language for Android. Do not replace your package name, else an app will not run.
CustomList.java code
  1. package ganeshannt.customlistviewsample;
  2. import android.app.Activity;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. public class CustomList extends ArrayAdapter<String> {
  10. private String[] names;
  11. private String[] desc;
  12. private Integer[] imageid;
  13. private Activity context;
  14. public CustomList(Activity context, String[] names, String[] desc, Integer[] imageid) {
  15. super(context, R.layout.list_layout, names);
  16. this.context = context;
  17. this.names = names;
  18. this.desc = desc;
  19. this.imageid = imageid;
  20. }
  21. @Override
  22. public View getView(int position, View convertView, ViewGroup parent) {
  23. LayoutInflater inflater = context.getLayoutInflater();
  24. View listViewItem = inflater.inflate(R.layout.list_layout, null, true);
  25. TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
  26. TextView textViewDesc = (TextView) listViewItem.findViewById(R.id.textViewDesc);
  27. ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
  28. textViewName.setText(names[position]);
  29. textViewDesc.setText(desc[position]);
  30. image.setImageResource(imageid[position]);
  31. return listViewItem;
  32. }
  33. }
Step 10
In Strings.xml, add the code given below. In this string, provide the app settings bar.
Strings.xml code
  1. <resources>
  2. <string name="app_name">CustomListViewSample</string>
  3. <string name="hello_world">Hello world!</string>
  4. <string name="action_settings">Settings</string>
  5. </resources>
Step 11
Create a new dimens.xml file into the values folder (File ⇒ New ⇒Activity⇒values resource file).
Go to dimens.xml, followed by clicking the text bottom. This XML file contains the designing code for an Android app. In the dimens.xml, copy and paste the code given below.
dimens.xml code
  1. <resources>
  2. <!-- Default screen margins, per the Android Design guidelines. -->
  3. <dimen name="activity_horizontal_margin">16dp</dimen>
  4. <dimen name="activity_vertical_margin">16dp</dimen>
  5. </resources>
Step 12
Create the menu folder into the resource folder.
Create a new menu_main.xml file into the values folder (File ⇒ New ⇒Activity⇒values resource file).
Go to menu_main.xml, followed by clicking the text bottom. This XML file contains the designing code for an android app. In the menu_main.xml, copy and paste the code given below.
menu_main.xml
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
  4. <item android:id="@+id/action_settings" android:title="@string/action_settings"
  5. android:orderInCategory="100" app:showAsAction="never" />
  6. </menu>
The code given above provides the settings functionality to our Application.
Android
Step 13
This is our user interface of the Application. Click Make project option.
Android
Step 14
Run the Application, followed by choosing the virtual machine. Click OK.
Android
Deliverables
Here, we successfully created Custom ListView in an Android Application and is executed.
Android
If you click any one of the ListView, I will show you the toast notification in the Application given below.
Android
If you have any doubts, just comment below.