
Those messages in the listview are sent by my crush to me.
Yes, that was the best dream I ever had ;-).
Iâm just kiddingâŠ
Letâs back to the topic,
When beginners make an app for showing a collection of data, Most of them prefer ListView. It is easy to work with listview when comparing its successor RecyclerView. Although ListView becomes old now, the uses of ListView still goes on.
You donât need to code the adapter to make ListView. Android API already provides an easy way to build a listview.
In this tutorial you will learn to construct listview easily and less code.
The android:entries XML attribute helps us achieve our target.
-
-
- Easy to populate ListView, Spinner, Gallery and ListPreference.
- Only static, unchanging string data allowed. So you canât use with data from internet, database,
shared preferences etc.
-
How android:entries works ??

As shown in the picture, android:entries attribute needs an array resource name, not the file name which contains array resource. You can define the array resource in strings.xml. Not only listview, Spinner, ListPreference, and gallery also use android:entries attribute. But gallery deprecated in API level 16. As a successor of ListView, RecyclerView doesnât hold entries attribute.
When the user runs the app, the Listview constructor takes the array resource referred in the entries attribute. Then the constructor makes an adapter itself with the simple_list_item_1 layout in SDK. Then assign it to the ListView.
Actually, we just point our data to the ListView, listview does all other work.
This is the way how it works.
How To Make A ListView In Android Studio
Step 1
Create a new project
In Android Studio, create a new project File -> New -> Project
Application Name: Simple ListView Ex
Company Domain: androidride.com
Android convention to use the reverse of the domain name. thatâs never a problem for you.
Click Next
Step 2
Choose your minimum SDK
It is necessary to choose the minimum SDK that you want. tick the phone and tablet checkbox for making the app for only phone and tablet.
Click next
Step 3
Select empty activity template
For this project, we just need an empty activity. So just select the empty activity template and click next.
Step 4
Create a new empty activity and layout
Next screen prompt for making an activity class file and its layout.
Activity Name : MainActivity
Tick the generate the layout checkbox if itâs not checked. Click finish. Now, you will see the MainActivity class
Step 5
Change toolbar and status bar color
You can skip this step if you donât want to alter the default style. Open res/values/colors.xml and paste the code below to make a red colored toolbar.
#f82900
#bc0000
#f82900
Step 6
Make a string array to hold the contents of the listview.
Open res/values/Strings.xml, and paste the below code between .
Hi Helloo How are you Heyy Why didn\'t you reply to me??
Use escape sequence \â to get the apostrophe in the last string.
Step 6
Add android: entries attribute and its value in listview XML definition.
open res/layout/activity_main.xml, delete the existing code and paste the code below.
<!--?xml version="1.0" encoding="utf-8"?-->
If you wish to add other widgets to your layout, then you can add ViewGroups like RelativeLayout, LinearLayout and other. Then just replace the XML namespace ( xmlns:android=âhttp://schemas.android.com/apk/res/androidâ) from listview to the root element.
Run the app. You will get the output like below.
How does ListView know did user click on list item ??
Based on tapping sound.
No, Here comes Listener. Listener waits for the events. Such as tapping, touching events.
Here we use the OnItemClickListener nested class. Its onItemClick(âŠ) method will trigger when an item in the listview clicked. So place the code that wants to run when click event happens.
Step 7
Create and Instantiate listview variable
In onCreate() method, create listview variable and instantiate using findViewById(int id) method. In this case, id is listview, we must use the same id in XML description. Type cast the returned View object to the ListView.
ListView mListView=(ListView)findViewById(R.id.listview);
Step 8
Add listener to listview for listening click events
Add OnItemClickListener and Implement OnItemClick(âŠ) method. Use listviewâs setOnItemClickListener() method to register the listener.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
}
});
Step 9
Display the content in the clicked item.
In onItemClick(âŠ) method, Implement code to reveal the content in listview row. First, Use adapterviewâs getItemAtPosition(int position) method to retrieve the data from listview item row. getItemAtPosition() returns data as object in the given position.
Convert it to string using toString() method;
String message = listview.getItemAtPosition(position).toString();
Show the retrieved message using Toast.makeText(context,CharSequence text,int duration) method.
Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();

Run it nowâŠ
Letâs wrap it up, You can use this attribute if you want a static listview otherwise we would prefer adapter.
Download SimpleListViewEx
For more reference
ListView
RecyclerView
Spinner, Gallery, and ListPreference
If you like this article, please share.
Thank you for scrollingâŠ
Â
<