Phil
Phil

Reputation: 4069

LayoutInflater view not stretching to match_parent

I'm having difficulty getting the view I'm inflating to match parent width. The layout I'm using on setContentView of the Activity contains the following

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    android:scrollbars="none" >

    <TableLayout
        android:id="@+id/tableRequestContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </TableLayout>

</ScrollView>

The layout I'm inflating looks like the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutRequestItemContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/Blue">

<TextView
    android:id="@+id/RequestItemUnit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="10dp" />

<TextView
    android:id="@+id/lblRequestItemCost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"        
    android:textSize="16sp" />

</LinearLayout>

Then in my activity, I loop over an ArrayList that contains my data and inflate the view each iteration, create a new TableRow, add the view to the TableRow and then add the TableRow to the parent Table. Code I use below:

public class Requests extends Activity {
    View view;
    LayoutInflater inflater;        
    RequestData requestData;
    TableLayout requestTable

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         setContentView(R.layout.request_layout);
         requestTable = (TableLayout) findViewById(R.id.tableRequestContainer);
         // HERE I POPULATE THE DATA OBJECT requestData with values
         requestData = getRequestData();
         for (int x=0; x<requestData.size(); x++) {
             TableRow tr = new TableRow(this);
             tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
             view = LayoutInflater.from(this).inflate(R.layout.request_item_layout, tr, false);
             // HERE IS THEN FIND THE TEXTVIEWS using view.findViewById(R...) and setText on each to the value in my data object.  This works fine
             // Add the view to the table row
             tr.addView(view);
             // Add the tablerow to the table
             requestTable.addView(tr);
         }
    }
}

The problem is the view is inflating properly and I am able to set the values of the TextViews, however the container is using wrap_content for width instead of match_parent as I specified in the XML. I have no idea why and have been trying everything I can think of and still can not get the inflated view to stretch the width to match_parent. The goal is the view I inflate should be the full width of the users screen.

Any help is greatly appreciated! Thank you!!!

Upvotes: 1

Views: 2643

Answers (2)

If Set Layoutparams not work you can try following:

Check whether you have set the following property of TableLayout dynamically :

tableLayout.setStretchAllColumns(true);

Upvotes: 3

Phil
Phil

Reputation: 4069

Figured it out myself. By changing the following line

tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

to this instead

tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));

and also adding the same layoutparams to the view once it is inflated, it stretches to match parent as desired.

Upvotes: 1

Related Questions