177

I've had severe trouble getting LayoutInflater to work as expected, and so did other people: How to use layoutinflator to add views at runtime?.

Why does LayoutInflater ignore the layout parameters I've specified? E.g. why are the layout_width and layout_height values from my resources XML not honored?

Community
  • 1
  • 1
andig
  • 13,378
  • 13
  • 61
  • 98
  • True- does that hurt or did I pick the wrong place? Just thought I'd share my results. Must admit tutorials are not specifically mentioned of the faq page.. – andig Feb 17 '11 at 09:43
  • 3
    see last paragraph of the first question in faq. post it as a question and then post the tutorial as an answer. of course, you can edit this question cutting and pasting the tutorial in the answer. I'll upvote if you fix this. – bigstones Feb 17 '11 at 10:22

3 Answers3

409

I've investigated this issue, referring to the LayoutInflater docs and setting up a small sample demonstration project. The following tutorials shows how to dynamically populate a layout using LayoutInflater.

Before we get started see what LayoutInflater.inflate() parameters look like:

  • resource: ID for an XML layout resource to load (e.g., R.layout.main_page)
  • root: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
  • attachToRoot: Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

  • Returns: The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

Now for the sample layout and code.

Main layout (main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>

Added into this container is a separate TextView, visible as small red square if layout parameters are successfully applied from XML (red.xml):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:background="#ff0000"
    android:text="red" />

Now LayoutInflater is used with several variations of call parameters

public class InflaterTest extends Activity {

    private View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);
      ViewGroup parent = (ViewGroup) findViewById(R.id.container);

      // result: layout_height=wrap_content layout_width=match_parent
      view = LayoutInflater.from(this).inflate(R.layout.red, null);
      parent.addView(view);

      // result: layout_height=100 layout_width=100
      view = LayoutInflater.from(this).inflate(R.layout.red, null);
      parent.addView(view, 100, 100);

      // result: layout_height=25dp layout_width=25dp
      // view=textView due to attachRoot=false
      view = LayoutInflater.from(this).inflate(R.layout.red, parent, false);
      parent.addView(view);

      // result: layout_height=25dp layout_width=25dp 
      // parent.addView not necessary as this is already done by attachRoot=true
      // view=root due to parent supplied as hierarchy root and attachRoot=true
      view = LayoutInflater.from(this).inflate(R.layout.red, parent, true);
    }
}

The actual results of the parameter variations are documented in the code.

SYNOPSIS: Calling LayoutInflater without specifying root leads to inflate call ignoring the layout parameters from the XML. Calling inflate with root not equal null and attachRoot=true does load the layout parameters, but returns the root object again, which prevents further layout changes to the loaded object (unless you can find it using findViewById()). The calling convention you most likely would like to use is therefore this one:

loadedView = LayoutInflater.from(context)
                .inflate(R.layout.layout_to_load, parent, false);

To help with layout issues, the Layout Inspector is highly recommended.

andig
  • 13,378
  • 13
  • 61
  • 98
  • 23
    Excellent answer. Been working on Android for 3 years and counting and still had something to learn from this. – Reuben Scratton Sep 27 '12 at 17:03
  • Hi is there any way to do the same thing to with java code instead of inflating with R.layout. because i don't have layout. I've created the view with java code and i wants to add the same view for 300 times. – Raj Nov 14 '12 at 14:00
  • Thanks #andig. I think the official doc is quite misleading. – coderek Feb 15 '13 at 09:57
  • @Raj You can subclass any view to hold all of the parameters you need for it's layout. If needed you can also create an array of the newly created view for iteration purposes. This is better off as a question of it's own though instead of via comments. – lodlock Apr 06 '13 at 17:55
  • @andig Why " Calling LayoutInflater without specifying root leads to inflate call ignoring the layout parameters from the xml" ? What is the reason ? – Paweł Brewczynski Sep 15 '13 at 16:15
  • @Marcin-Orlowski: would you care to explain why `this` can be used instead of `getBaseContext()` according to you edit? – andig May 17 '14 at 20:51
  • 1
    @andig: `Activity` is subclass of `Context` and the answer's examples are in scope of `Activity`, so to make it simpler I replaced `getBaseContext()` with `this` as, for the sake of this answer, it's equivalent. – Marcin Orlowski May 17 '14 at 22:17
  • Thanks, I can't recall I ever had to use it this way :(. Rather just attach and edit. ah well. – Mathijs Segers Dec 22 '14 at 10:45
  • This is absolutely the highest quality answer on any stackexchange I've ever seen. I know comments are supposed to be more constructive than this, but seriously. The best. – kdbanman Feb 01 '15 at 04:55
  • 5
    container has layout_width and layout_height set to "match_parent"; then comment says "// result: layout_height=wrap_content layout_width=match_parent". Am I missing sth? – Marian Paździoch Mar 09 '15 at 08:58
  • What should I do if I wish to change the text each time ? How to call the `setText()` here ? – Pranit Bankar Mar 12 '15 at 18:50
  • 1
    Thanks for doing the research and sharing the results! Also, I second Marian's question. – LarsH Sep 21 '15 at 20:45
  • It should both be match_parent but I cannot retest right now. Thanks for the hint, feel free to edit if confirmed. – andig Sep 22 '15 at 07:25
  • @andig i have checked the example code and i see textview takes**layout_height=wrap_content** in spite of **layout_height=match_parent** set for parent linearlayout to which textview is getting added. Its bit weird to understand this behavior. – Rajen Raiyarela Jan 21 '16 at 13:10
  • using the inflate method of View Group can also lead to the inflate it with default values. – Meghal Shah Apr 28 '17 at 21:09
9

andig is correct that a common reason for LayoutInflater ignoring your layout_params would be because a root was not specified. Many people think you can pass in null for root. This is acceptable for a few scenarios such as a dialog, where you don't have access to root at the time of creation. A good rule to follow, however, is that if you have root, give it to LayoutInflater.

I wrote an in-depth blog post about this that you can check out here:

https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/

seanjfarrell
  • 231
  • 3
  • 8
0

wanna add to main answer above
I tried to follow it but my recyclerView began to stretch every item to a screen
I had to add next line after inflating for reach to goal

itemLayoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));

I already added these params by xml but it didnot work correctly
and with this line all is ok

Kirguduck
  • 748
  • 1
  • 9
  • 20