Next Tutorial
ListView with Toast Message
Prev Page
View All Tutorials
Android Tutorial, Coding a Simple ListView
If you are just arriving at this page; and you are not familiar with
creating Android Apps,we suggest you begin with our
tutorials introduction:
About Our Tutorials.
From there follow along with each tutorial lesson.
This is the App - ListView Simple
When To Use a ListView
ListView is a code class for a scrollable user interface, usually containing one or two rows of data per sectioned item. In its simplest form, the
List Activity
class of ListView method provides
a single rowed layout which can be used to add information.
The List Activity also includes a default layout; so you don't need to include the
ListView element
in your layout file's code. The code in your java file, (typically MainActivity.java) includes the list named items(array list) and will populate them into the layout for you.
If you want to implement a scrollable layout with additional features like; customizable dividers with color; then you must use the
ListView code class method, which includes these attributes. The ListView element would be coded in your layout file.
For this tutorial app, we are coding the simple
List Activity method; which adds a single rowed sectioned layout to our view, and the items we want to add
to each row are added in the java file as an array list. The array is then attached to the layout using the array adapter.
android.R.layout.simple_list_item_1 This code is a android default built-in code for ListViews. It means that the layout will have one row - item_1. If it had
read item_2, then the row would have 2 items, meaning 2 rows per each section. Our app has 1 row per each section.
For our data, we added some Spanish word translations from English to Spanish. We added many words so that our rows exceeded the physical boundry of our view; thus
our listed items become scrollable. You can add as many rows of data as you want.
Additional Reading at Android Developer website:
ListView - ListActivity
Creating - Naming Your App Using AIDE
When creating your app,(from left menu) choose:
Create New Project;
Then, choose
New Android App(gradle, android Java/xml)
or
Hello World App Java/xml
if your using the original version of AIDE
You can name your app whatever you like; if your using the name as shown in our example code use;
ListView Simple for the App Name and,
for the package name(next line) use:
com.aac.listviewsimple
The Coded Pages
Code for the app - ListView Simple
For this app we need to edit three pages, the
layout file, java file, and strings.xml file.
1 layout file,
main.xml; located at res/layout/main.xml, already created, just replace the code on this page with the code from our Example Code section and save the page.
1 java class page;
MainActivity.java; located at app/src/java/MainActivity.java, already created; we have to copy and replace the code on this page with the code
from our Example Code section. Save the page once the code is pasted.
The java page has the array containing our items for each row in our ListActivity. And, the array is attached and populated into our view using the array adapter as shown in this image.
We must include our
import statements into our java code for each code class we implement. For this app we are using the
ListActivity so that must be included
as an import as shown in this image. And, you can see we also added the import for our
Array Adapter.
If we do not include the imports, or the proper ones, when we build and run our code, we will get error messages.
1 strings file, required for this App: The string file at res/values/strings.xml has the required app_name string for the app name.
Just remove the string that reads - hello_world, as this is not required for our app tutorial. Save the page.
EXAMPLE CODE - Code For This Tutorial
JUST copy the code for this tutorial
App Name is
'ListViewSimple - located at AppProjects/LanguagesLocal in AIDE, open the AIDE app, then open App Projects,
THEN select the 'ListViewSimple',
app from your files hierachy(left menu); then click on any file from the app, like strings.xml Make sure at top of page, the app name 'ListViewSimple' is there next to
AppProjects like so:
AppProjects/ListViewSimple, then you know you have the proper App.
Once a file is in the editor, you can edit, save it, then RUN your APP, Install,
and Open your app.
Make sure you SAVED EACH PAGE,(or choose automatic saving in AIDE settings) before you RUN your code. SAVE, RUN, INSTALL, OPEN, your App. Once it has been
installed on your tablet, you can open it Simply by Clicking on the Android little green App Icon,with the wording 'ListViewSimple' on your tablet. It will
be with your other installed apps.
If you make further changes to this App, you need to SAVE, RUN, UPDATE, INSTALL, OPEN the app.
This free script provided by
JavaScript
Kit