Why Does Layoutinflater Ignore the Layout_Width and Layout_Height Layout Parameters I'Ve Specified

Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified?

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.

LayoutInflater ignoring parameters?

Well after nearly two days I have found the culprit :

As I actually suspected the new LayoutParams() is a problem if you already have "formatted" your View in the XML declaration...
So after leaving the new LayoutParams() out of the equation my Table would be shown just fine...

So just use :

grid.addView(row);

Android : inflate() ignoring utilizing root layout width/height defined in style sheets. BInary XML error

Ok,

So I've not gotten any responses, here is my work around:

Change this:

myView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.bearing_layout, parentView, false);

To this:

myView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.bearing_layout, null, false);

This will properly load your resources the FIRST TIME. Note: This WILL not detect layout changes for your lowest level view on orientation changes, though all subsequent view should work fine. To handle this we add a helper function:

genLayoutParams(yourView);

private void genLayoutParams(View reuse) { // apply whatever layout parameters you see fit, these are just examples.
if (activity.getResources().getConfiguration().orientation == activity.getResources().getConfiguration().ORIENTATION_LANDSCAPE) {
reuse.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.FILL_PARENT));

}
else {
if (android.os.Build.VERSION.SDK_INT >= 13) {
reuse.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
250));
}

else{
reuse.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
300));
}
}
}

Hope that saves somone some frustration.

addView() by inflating a layout causes the layout parameters to fail

Change your XML file with that:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="@+id/cdvConfirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
card:cardBackgroundColor="#fff"
card:cardCornerRadius="2dp"
card:cardElevation="2dp"
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_vertical|center_horizontal">

<CheckBox
android:id="@+id/ckbConfirm"
android:layout_width="20dp"
android:layout_height="20dp"
android:button="@drawable/checkbox_default"
android:scaleType="fitCenter"

/>


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.70">

<TextView
android:id="@+id/tvShipmentType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/btnDetail"
android:layout_toStartOf="@+id/btnDetail"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Title"
android:textStyle="bold"
android:textColor="@android:color/black"
/>

<TextView
android:id="@+id/tvShipmentDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/btnDetail"
android:layout_toStartOf="@+id/btnDetail"
android:layout_below="@+id/tvShipmentType"
android:text="No Appointment Date No Appointment Date No Appointment Date No Appointment Date"
android:singleLine="false"
android:ellipsize="end"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"
android:textStyle="bold"
/>

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.30"
android:gravity="center_vertical|center_horizontal">

<Button
android:id="@+id/btnDetail"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="10dp"
android:text="รายละเอียด"
android:background="#81C784"
android:src="@drawable/menu_complete"
android:scaleType="fitCenter"
/>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp">

<TextView
android:id="@+id/tvAlert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvShipmentDescription"
android:textColor="#D50000"
android:text="** ยังไม่ได้ทำการเลือกรายการนี้"
android:visibility="invisible"
/>
</LinearLayout>
</LinearLayout>


</RelativeLayout>

</LinearLayout>

</android.support.v7.widget.CardView>

It's a better option because I used to weight instead numbers on height or width.

Weight it's like a percents on screen that XML will be adapted to any screen resolution.

Why using LayoutInflater is not same to xml?

Try this instead:

View view = inflater.inflate(R.layout.item, list, false);
list.addView(view); //(R.id.list), LinearLayout list

Explanation:

The second argument to inflate() is the intended parent of the view to be inflated. When you pass null as the second argument, the inflated view does not get any LayoutParams because inflate() doesn't know what the parent eventually will be and thus cannot create the appropriate LayoutParams (almost every ViewGroup defines its own LayoutParams subclass).

When you call addView(), the LinearLayout checks if the child has LayoutParams and whether they are of the appropriate type. If not, it generates some default LayoutParams to set on the view being added. Whatever default it gives the child is causing the unexpected behavior.

In short, the solution is to pass list instead of null when you call inflate().

Clarification about layout inflater -attach to root?

No, something is missed!

When you pass true as 'attach to root', inflater will inflate specified layout (represented by its ID) and then attach it to root of parent and finally return the parent

But when you left 'attach to root' to false. the parent hierarchy won't changed and only inflated layout will be returned.

I don't understand the main difference between true of false for the third parameters of inflate method

Let's suppose we have this very simple layout:

view_red_square.xml

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FA8072" />

And this layout used by our main activity:

activity_main.xml

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

Now let's try to inflate the view_red_square.xml layout in MainActivity and understand what happens.

MainActivity.kt

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val container = findViewById<FrameLayout>(R.id.container)
val inflatedView = layoutInflater.inflate(R.layout.view_red_square, container, false)
Toast.makeText(this, "Is the view inflated: $inflatedView", Toast.LENGTH_SHORT).show()
}
}

The third boolean parameter for LayoutInflater.inflate, as the documentation points, stands for:

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.

So, going back to our main activity. The false boolean value indicates that the view inflated shouldn't be added to the root, also specified in the inflate method.

Using the above code for MainActivity. We can verify, with the layout inspector, that the view is not added to the container when inflating the view with attachToRoot set to false.

A solution for adding the view to our container would be to manually add the view to our view group, by using container.addView(inflatedView).

attach to root false with the layout inspector

Now let's see what happens when we switch that attachToRoot boolean to true. Let's again use the layout inspector.

val inflatedView = layoutInflater.inflate(R.layout.view_red_square, container, true)

attach to root true with the layout inspector

As you can see, the view is now added to the root which is our container. Therefore, we can conclude that attachToRoot, adds the inflated view to the given root if its value is true.



Related Topics



Leave a reply



Submit