Rewind
Rewind

Reputation: 2814

How do I get a reference to a LinearLayout?

I am trying to get a reference to a LinearLayout so I can add an element.

Here is my xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">

Is it acceptable to have the line android:id="@+id/myLayout" in there?

My incorrect attempt to try and get a reference to my layout is with the following:

LayoutInflater myLayoutInflater;
LinearLayout myLayout;

if((myLayoutInflater = m_Context.getLayoutInflater()) != null) {
    myLayout = (LinearLayout) myLayoutInflater.inflate(R.id.myLayout, null);
}

It underlines the R.id.myLayout in the inflate() line in red saying:

Expected resource of type Layout. Ensure resource ids passed to APIs are of the right type.

Upvotes: 0

Views: 1479

Answers (3)

guipivoto
guipivoto

Reputation: 18677

There's a misunderstanding about those methods.

  • LayoutInflater.inflate

This method expects the id of a layout file (and not of a layout view). So, you should call:

myLayoutInflater.inflate(R.layout.<NAME_OF_THE_XML_LAYOUT_FILE>, null);

That method will return whole view that was inflated. So, now that you have the inflated view, you can search Views inside of it. You can search view by their ID.

  • findViewById()

This method expects the id of the View. So, here, you should call:

View inflatedView = myLayoutInflater.inflate(R.layout.<NAME_OF_THE_XML_LAYOUT_FILE>, null);
LinearLayout linearLayout = inflatedView.findViewById(R.id.myLayout); // Assuming you added android:id="@+id/myLayout" to the LinearLayout

Note that first, we inflated the xml file and then, we started to seach for views inside of it.

HOWEVER

If your view is part of an Activity, you don't need to inflate that layout. You can instead:

public void onCreate() {
    ....
    // This will inflate and add your layout to the actvity
    setContentView(R.layout.<NAME_OF_THE_LAYOUT_FILE);

    // After that line, you can call:
    LinearLayout linearLayout = inflatedView.findViewById(R.id.myLayout); // Assuming you added android:id="@+id/myLayout" to the LinearLayout

    // Since your view was added to the activity, you can search for R.id.myLayout
    // If you search for any view before setContentView(), it will return null
    // Because no view was added the screen yet
}

Upvotes: 2

P Fuster
P Fuster

Reputation: 2334

It is ok to find a view by id for a layout just like you do for a view:

LinearLayout layout = (LinearLayout) findViewById(R.id.myLayout);

This will do the trick if you are in an Activity context.

To add a view to it you can then do:

layout.addView(...)

You are getting that error because the LayoutInflater expects the name of the layout file not your layout id, so something like R.layout.item_layout. You also don't want to pass null for the parent view group in most cases so I would not recommend inflating it this way unless you know the parent layout.

Upvotes: 1

Hardik Chauhan
Hardik Chauhan

Reputation: 2746

Try something like this,

LayoutInflater myLayoutInflater = LayoutInflater.fromContext(mContext);
LinearLayout myLayout = myLayoutInflater.inflate(R.layout.layout_file, null);
View view = (LinearLayout)view.findViewById(R.id.myLayout);

Upvotes: 1

Related Questions