243

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
1
  • 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! stackoverflow.com/questions/35098789/… Commented Jan 30, 2016 at 19:05

7 Answers 7

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.

12
  • 15
    The layouts are also in your SDK installation Commented Sep 8, 2010 at 0: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. :) Commented Sep 8, 2010 at 1:06
  • 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. Commented Sep 8, 2010 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! Commented Sep 25, 2010 at 18:37
  • 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
    Commented Mar 23, 2011 at 22:17
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> 
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...

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

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);

0
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.

0
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.