phadaphunk
phadaphunk

Reputation: 13313

FindViewById in different layout

I am building an android app and I am accssing the UI elements with the following method :

FindViewById

The thing is it looks like I cannot access elements that are not in the currently opened layout.
Is that possible ? If so, how can I access elements that are not in the currently loaded layout.

SetContentView (Resource.Layout.CameraLayout);

Button button1 = FindViewById<Button> (Resource.Id.firstButton);
Button button2 = FindViewById<Button> (Resource.Id.secondButton);

button1.Click += (s,e) => {//do stuff}; // In CameraLayout layout.
button2.Click += (s,e) => {//do stuff}; // Not in CameraLayout layout.

This line will throw the null exception : button2.Click += (s,e) => {//do stuff};

But if I change it to :

SetContentView (Resource.Layout.AnotherLayout);

Button button1 = FindViewById<Button> (Resource.Id.firstButton);
Button button2 = FindViewById<Button> (Resource.Id.secondButton);

button1.Click += (s,e) => {//do stuff}; // Not AnotherLayout layout.
button2.Click += (s,e) => {//do stuff}; // In AnotherLayout layout.

This line will throw the null exception : button1.Click += (s,e) => {//do stuff};

So I can only access elements form the currently loaded layout. I highly doubt that there is no way to access the other elements but I still can't find how.

Upvotes: 0

Views: 2761

Answers (5)

Divins Mathew
Divins Mathew

Reputation: 3186

The workaround is to access the root of a layout using LayoutInflater.

The code is the following, where this is an Activity:

LayoutInflater factory = (LayoutInflater)Application.Context.GetSystemService(LayoutInflaterService);

View mView = factory.Inflate(Resource.Layout.TheOtherLayout, null);

And now you can get required references by:

TextView mTextView = mView.FindViewById<TextView>(Resource.Id.textView);
ImageView mImageView = mView.FindViewById<ImageView>(Resource.Id.imageView);

//And so on....

Upvotes: 0

Szymon
Szymon

Reputation: 43023

General design is that you inflate one layout and use setContentView for that one layout only. Then you have access to all visual elements in that layout.

You may consider redesigning your solution if the current design forces you to inflate more than one layout.

Upvotes: 1

Pete Houston
Pete Houston

Reputation: 15089

Each Activity has its own layout UI and it is defined by SetContentView() inside OnCreate() method.

In case you want to work with Objects (Views...) in another layout, you need to call those layout out, that is, inflate() to get the root layout, from here, use the root layout to access inner elements.

View rootInAnotherLayout = this.LayoutInflater.Inflate(
                                Resource.Layout.AnotherLayout,  // blah blah ...);

Button button1 = rootInAnotherLayout.FindViewById<Button> (Resource.Id.firstButton);
Button button2 = rootInAnotherLayout.FindViewById<Button> (Resource.Id.secondButton);

So button1 and button1 are the two Views from AnotherLayout, not this layout.

Upvotes: 1

user3110424
user3110424

Reputation:

Inflate the layout whose element you want to access and then access it by FindViewById otherwise you will get only the current layout element which you have set in SetContentView.

Upvotes: 2

poortechworker
poortechworker

Reputation: 669

I am not sure if I understood your question properly. But hope my answer helps.

Before you access your view by using findViewById you have to add the view id in the resource file by using @+id. This can be done as follows:

<Button
    android:id="@+id/firstButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="receiveMessage"
    android:text="@string/button2_text" />

and then save the file. You will be able to access this in your method. To know more here is the literature from google

The at sign (@) is required when you're referring to any resource object from XML. It is followed by the resource type (id in this case), a slash, then the resource name (edit_message).

The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's gen/R.java file that refers to the EditText element. Once the resource ID is declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.

Hope this helps.

Upvotes: 0

Related Questions