53

I am new to Android development. In the Notepad sample, I saw the following code snippet:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor,
        new String[] { Notes.TITLE }, new int[] { android.R.id.text1 });

and in the notelist_item.xml file:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"   <-----------HERE
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
    android:singleLine="true"
/>

So, I am wondering, what is this android.R.id.text1?

Also, I found android.R.id.button1, button2, button3 in the android.jar file.

Are they some kind of well known IDs for some 3rd party controls?

Thanks

Kirby
  • 15,127
  • 10
  • 89
  • 104
smwikipedia
  • 61,609
  • 92
  • 309
  • 482

7 Answers7

40

android.R.id.text1 is just an identifier defined in the Android framework.

In the framework, android.R.id.text1 is an used for TextView views. You can find it in many layouts from the framework (select_dialog_item, select_dialog_singlechoice, simple_dropdown_item_1line, etc.). In Android framework xml, it is represented by @+id/text1.

Hence, if you use one of these layouts and want to change the text, you will need to use this id.

// probably in a custom ListAdapter that uses 
View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView textView = (textView) view.findViewById(android.R.id.text1);
textView.setText("Oh no! not hello world again");

Also, you can use this same identifier to identify a TextView (or anything) in your custom layouts. See in the sample "Notepad", the layout file noteslist_item.xml.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:singleLine="true"  
/>

And actually, you could use R.id.text1 as an identifier of anything else, but that would be confusing.

rds
  • 26,253
  • 19
  • 107
  • 134
  • I have asked a related question [android.R or cutom R?](http://stackoverflow.com/questions/7082888/what-is-better-android-r-or-cutom-r) – rds Nov 23 '11 at 11:12
  • I have a somewhat related question, is it possible to do something like `R.id.Interger.parseInt("someID");`? I haven't seen a way that you can do it using R.id, but is there a similar way to do this? – Mahmud Ahmad Jan 25 '17 at 22:33
5

It's a build-in layout (android.R.layout.simple_list_item_1) view id used for default lists etc.

Dmitri Gudkov
  • 2,093
  • 16
  • 15
4

android.R.id.text1 is a TextView in layout (android.R.layout.simple_list_item_1) you can see this in

(path to ur Android SDK)\platforms\android-\data\res\layout

folder

Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30
2

android.R.id.text1 is the id of the TextView defined in the android's predefined layout android.layout.simple_list_item1. so it is just like give an id which is already described rather than giving a new id.

Neeraj Nama
  • 1,562
  • 1
  • 18
  • 24
  • And I am very curious to know if it is [a good practice to reuse framework id](http://stackoverflow.com/questions/7082888/what-is-better-android-r-or-cutom-r) – rds Aug 18 '11 at 21:08
1

android.R.id.text1 is the id of TextView which is defined in notelist_item.xml. You can find and use the widget by its id.

  • +1 because it is actually correct in the context of this question, although incomplete – rds Nov 23 '11 at 11:15
1

The text1 part (and the other buttons) are the reference for your java code to the textView defined in your layout.xml. the rest of that Android.R.id tells the code to find an Android resource with an Id of 'text1' for example.

FireEnigmaX
  • 567
  • 2
  • 7
  • 17
0

android.R.id.Text1 is a actually id of simple_dropdown_item_1line in the android if you click on simple_dropdown_item_1line with ctrl+touchpad(if you use laptop) you see the there is a id of simple_dropdown_item_1line you use the same text1 in your xmlfile give your styles in your xmlfile then you will run it easily