0

I was trying to understand deeply the difference between @android:id/.. and @+id/.. and I have understood that using @android:id/.. allows you to use an already created id by the android OS, but @+id/.. creates a new id in my R file.

The question is: Is it more efficient, memory wise, to use @android:id/.. whenever it is possible, since this will use already exists id?

I expect that even if the answer is yes, it is more economical, the saved storage can be neglected. But I am keen on understanding.

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • By using `@+id` you reference a View **anticipately** (a View which hasn't yet been created in the xml, therefore creating a **temporay** id, which will be **then** assigned). I prefer creating the View **before and then** refer it by using `@id`. It **seems less** straighforward, but I think it's **more**. – Phantômaxx Mar 31 '15 at 15:20
  • @DerGolem .. can you give an example as an answer for creating the view before then refer to it. And why you prefer this way? – Sami Eltamawy Mar 31 '15 at 15:23
  • I prefer this way because so you don't have to **prepare an id and the assign it later**. You `just use what you already have created first`. – Phantômaxx Mar 31 '15 at 15:32

1 Answers1

3

Your notion of the two ways of creating id's is not correct:

+@id

You use it whenever you are setting your own id to an element.

@android:id

You use it when you are setting an id of an element that is already predefined in android's framework

nem035
  • 34,790
  • 6
  • 87
  • 99
  • What do you mean by "that is already predefined in android's framework" and does this means that this is more efficient or not? – Sami Eltamawy Mar 31 '15 at 15:15
  • I mean, android framework comes with built-in views, such as lists for example, that already have a certain structure and that you can re-use instead of creating your own. Their efficiencies cannot be compared logically because they do not do the same thing. – nem035 Mar 31 '15 at 15:17
  • 1
    I think you might have confused `@id` with `@android:id`. Look at **@DerGolem's** comment – nem035 Mar 31 '15 at 15:23
  • No, I am asking for the advantage of using `@android:id` over the other ways – Sami Eltamawy Mar 31 '15 at 15:25
  • 1
    Ok, lets say you want to build an art museum, inspired by Picasso. And you already have a bunch of Picasso's paintings in your basement and they all have a unique name (`@android:id`). However, you are also painting your own paintings and giving them a unique name (`@id`). Now, each time you want to decorate a wall (`android view`), you select either an existing painting that Picasso already made for you, or you select the one you painted yourself. The advantage of selecting the Picasso painting is that you need to do less painting (coding) to make it look good, no obvious storage differences. – nem035 Mar 31 '15 at 15:31
  • I'm about to get it except the part of `less painting (coding)`. In your example, you mean that I'm going to use already exist frame (use already created id by android) and paint instead of creating a frame and paint from scratch (Creating id in R file) ? – Sami Eltamawy Mar 31 '15 at 15:37
  • 1
    `@androd:id` is a **system** element, while `@id` is an element **you** provide. – Phantômaxx Mar 31 '15 at 15:38
  • 1
    @Tamawy yes, that is, in essence, what I mean. [here is a list of available pre-built views in android](http://stackoverflow.com/a/9529970/3928341) that you can use to quickly built lists and other elements without creating their layouts from scratch. These built-in elements in the android framework can, for example, be loosely compared to built-in tags in HTML like `

    `. Of course you can create a `div` and give it a CSS style to behave exactly like `

    ` but why would you go through the trouble when it's already made for you.

    – nem035 Mar 31 '15 at 15:43
  • @nem you are right. but what If I created a `ListView` for example and gave it the it id=`@android:id/list` .. what is that means? – Sami Eltamawy Mar 31 '15 at 15:47
  • 2
    It means that you want to use a ListActivity or a ListFragment. Which gives you less flexibility than putting a ListView in an Activity or a Fragment. (you have **no choice on the name** - it MUST be `@android:id/list` and you can have ONLY ONE in the layout) – Phantômaxx Mar 31 '15 at 15:49