Gnuey Gnuey - 1 year ago 60
Android Question

Are view ids local to the activity context?

What is the scope of the a view's identifier?

Is it local to its corresponding activity?

For example, if in activity_main.xml I specify

<EditText android:id="@+id/edit_message" />

Within my would I legally be able to access

When I test this I get an exception, but none of the messages are clearly telling me it's due to accessing an "id out of context", or something along those lines. I'm getting errors like

This question arose as I was following the beginner tutorial:

Thanks both for clarifying the difference between the scope of
and that of the widget with id
(in terms of accessing it by

I actually intended to ask "would I be able to call findViewById( in Activity A to access a widget in Activity B". And from your answers it appears the answer to that question is "no".

In my code, I did not include a view with id
in the ViewGroup of Activity B. That's probably why I got the errors.

Answer Source

What is the scope of the a view's identifier?

The identifier is an integer. It does not have a scope.

Within my would I legally be able to access

If we assume that you mean "would I be able to call findViewById( to access a widget?", you can certainly call it. Whether you get null or not will depend on whether the activity has a widget in its view hierarchy with that ID.

However, Activity A cannot call findViewById() to retrieve a widget from Activity B. The id is an integer and has no scope. However, findViewById() has a scope: the view hierarchy of whatever you call it on (e.g., an activity, a ViewGroup).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download