240

I've started learning Android development and am following a todolist example from a book:

// Create the array list of to do items
final ArrayList<String> todoItems = new ArrayList<String>();

// Create the array adapter to bind the array to the listView
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(  this, 
                                android.R.layout.simple_list_item_1,
                                todoItems
                            );
myListView.setAdapter(aa);

I can't understand exactly this code especially this line:

android.R.layout.simple_list_item_1
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Zakaria
  • 14,892
  • 22
  • 84
  • 125
  • But why is that a parameter? I just wanted to update my listview with my array, and one of the answers showed this. I am not sure why I need this...Here is my question. Thanks! http://stackoverflow.com/questions/35098789/how-to-update-the-listview-according-to-arraylist/35099000?noredirect=1#comment57929962_35099000 – Ruchir Baronia Jan 30 '16 at 19:05

7 Answers7

270

Zakaria, that is a reference to an built-in XML layout document that is part of the Android OS, rather than one of your own XML layouts.

Here is a further list of layouts that you can use: http://developer.android.com/reference/android/R.layout.html
(Updated link thanks @Estel: https://github.com/android/platform_frameworks_base/tree/master/core/res/res/layout )

You can actually view the code for the layouts.

Aaron Klap
  • 243
  • 2
  • 9
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 15
    The layouts are also in your SDK installation – CommonsWare Sep 08 '10 at 00:49
  • 11
    Heh, so they are. :P I'd tried looking for them before just by browsing the android jar within Eclipse and it just told me "Source Not Found". But yeah, they're under platforms > android-x > data > res > layout. Good call. :) – Kevin Coppock Sep 08 '10 at 01:06
  • Thanks for the answer. But what's the purpose to call this layout in an ArrayAdapter? – Zakaria Sep 08 '10 at 08:52
  • 8
    It tells the listview what layout to use for the individual rows. There are others with checkedtextviews for multiple selections, you can make custom layouts that include images, and multiple textviews for example. These android.R ones are just some easy to use, already created resources for you. – Kevin Coppock Sep 08 '10 at 14:00
  • 30
    Thanks! Wow, that's a lot of layouts. All the Android Reference seems to reveal about them (in R.layout.html) are the constant values for each id. Might there be any documentation that _illustrates_ each of these with a sample use case? (e.g., "Layout X looks like this [figure with field ids]. It is best used in cases a, b, and c. It can be seen in action in app Y.") Yes, it _is_ great to know I can plunder the vaults and hack this all out on my own, but a scannable list of illustrations (vs XML) would be such a big help! – Joe D'Andrea Sep 25 '10 at 18:37
  • That would be handy, I've not seen one as of yet though. Best bet would be to just keep swapping them out in your code, and running (painfully slow, I know :P) – Kevin Coppock Sep 26 '10 at 02:52
  • 13
    This is seems to be typical practice of Google. They release all this great technology and have all the documentation skills of IBM. – angryITguy Mar 23 '11 at 22:17
  • The GitHub link is useless because it always gives `"Sorry, this tree took too long to generate."` To see the XML files simply go to `android_sdk_path/platforms/android-17/data/res/layout` (for API 17) – tomrozb Feb 24 '13 at 00:26
  • 2
    I can't even count how many tutorials i stopped following simply because I couldn't find `simple_list_item_1` – Ojonugwa Jude Ochalifu Jan 16 '15 at 16:45
  • But why is that a parameter? I just wanted to update my listview with my array, and one of the answers showed this. I am not sure why I need this...Here is my question. Thanks! http://stackoverflow.com/questions/35098789/how-to-update-the-listview-according-to-arraylist/35099000?noredirect=1#comment57929962_35099000 – Ruchir Baronia Jan 30 '16 at 19:05
  • What are those layouts used for? – Pedro García Medina May 23 '16 at 23:12
  • @JoeD'Andrea A full gallery of these would be great and is sorely needed in the official documentation. Rob Gibbens has had a crack at it here: https://robgibbens.com/androids-built-in-list-item-layouts/ (the syntax is Xamarin C# not Java, but it's the same Android framework). – Rupert Rawnsley Oct 24 '18 at 09:26
37

This is a part of the android OS. Here is the actual version of the defined XML file.

simple_list_item_1:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/listItemFirstLineStyle"
    android:paddingTop="2dip"
    android:paddingBottom="3dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

simple_list_item_2:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/listItemFirstLineStyle"/>

    <TextView android:id="@android:id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/text1"
        style="?android:attr/listItemSecondLineStyle" />

</TwoLineListItem> 
Ziem
  • 6,579
  • 8
  • 53
  • 86
nikoo28
  • 2,961
  • 1
  • 29
  • 39
13

as answered above by: kcoppock and Joril

go here : https://github.com/android/platform_frameworks_base/tree/master/core/res/res/layout

just right click the layout file you want, then select 'Save As', save somewhere, then copy it in 'layout' folder in your android project(eclipse)...

you can see how the layout looks like :)

way to go...

Aaron Klap
  • 243
  • 2
  • 9
Nigel Crasto
  • 318
  • 3
  • 8
9

As mentioned by Klap "android.R.layout.simple_list_item_1 is a reference to an built-in XML layout document that is part of the Android OS"
All the layouts are located in: sdk\platforms\android-xx\data\res\layout
To view the XML of layout :
Eclipse: Simply type android.R.layout.simple_list_item_1 somewhere in code, hold Ctrl, hover over simple_list_item_1, and from the dropdown that appears select "Open declaration in layout/simple_list_item_1.xml". It'll direct you to the contents of the XML.
Android Studio: Project Window -> External Libraries -> Android X Platform -> res -> layout, and here you will see a list of available layouts.
enter image description here

Robert Verdes
  • 380
  • 3
  • 9
Saber
  • 5,150
  • 4
  • 31
  • 43
8

android.R.layout.simple_list_item_1, this is row layout file in your res/layout folder which contains the corresponding design for your row in listview. Now we just bind the array list items to the row layout by using mylistview.setadapter(aa);

Swathieswari
  • 111
  • 1
  • 2
5

No need to go to external links, everything you need is located on your computer already:

Android\android-sdk\platforms\android-x\data\res\layout.

Source code for all android layouts are located here.

Aaron Klap
  • 243
  • 2
  • 9
5

Per Arvand:
Eclipse: Simply type android.R.layout.simple_list_item_1 somewhere in code, hold Ctrl, hover over simple_list_item_1, and from the dropdown that appears select Open declaration in layout/simple_list_item_1.xml. It'll direct you to the contents of the XML.

From there, if you then hover over the resulting simple_list_item_1.xml tab in the Editor, you'll see the file is located at C:\Data\applications\Android\android-sdk\platforms\android-19\data\res\layout\simple_list_item_1.xml (or equivalent location for your installation).

CSchulz
  • 10,882
  • 11
  • 60
  • 114
Barry Holroyd
  • 1,005
  • 1
  • 11
  • 20