Wai Yan Hein
Wai Yan Hein

Reputation: 14831

Setting layout weight to dynamically added view is not working in Android

I am developing an Android App. In my app, I am adding view dynamically and setting its weight in code as well. But it is not working.

This is my XML for container of dynamic views:

<ScrollView>
.
.
.

<LinearLayout
        android:orientation="vertical"
        android:id="@+id/id_related_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></LinearLayout>

.
.
.
</ScrollView>

This is how I am adding controls dynamically to it.

private void addRelatedItemViews(final ArrayList<Item> relatedItems)
    {
        LinearLayout subContainer= new LinearLayout(this);
        LinearLayout.LayoutParams initialParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        subContainer.setLayoutParams(initialParam);
        relatedItemsContainer.addView(subContainer);
        for(int i=0;i<relatedItems.size();i++)
        {
            if(i>0 && i%2==0)
            {
                //initialize sub container
                subContainer = new LinearLayout(this);
                LinearLayout.LayoutParams subContainerParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                subContainer.setLayoutParams(subContainerParam);
                relatedItemsContainer.addView(subContainer);
            }
            final int index = i;
            View view = layoutInflater.inflate(R.layout.realated_item_view,null);
            ImageView imageView = (ImageView)view.findViewById(R.id.iv_related_item_image);
            TextView tvName = (TextView)view.findViewById(R.id.tv_related_item_name);
            TextView tvPrice = (TextView)view.findViewById(R.id.tv_related_item_price);
            TextView tvLikeCount = (TextView)view.findViewById(R.id.tv_related_item_like_count);
            Picasso.with(getBaseContext()).load(relatedItems.get(i).getMediumImageUrl()).into(imageView);
            tvName.setText(relatedItems.get(i).getName());
            tvPrice.setText(CommonHelper.formatCurrency(relatedItems.get(i).getPrice()));
            tvLikeCount.setText(CommonHelper.formatLikeCount(relatedItems.get(i).getLikeCount()) + " like");
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openItemActivity(relatedItems.get(index).getId());
                }
            });
            LinearLayout itemContainer = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
            itemContainer.setLayoutParams(params);
            itemContainer.addView(view);
            subContainer.addView(itemContainer);
        }
    }

This is my realated_item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_weight="1"
    android:padding="3dp"

    android:layout_height="wrap_content">
    <LinearLayout
        android:background="@drawable/item_bg"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/iv_related_item_image"
            android:scaleType="centerCrop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:padding="5dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:textStyle="bold"
                android:textSize="17dp"
                android:id="@+id/tv_related_item_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_marginTop="3dp"
                android:textSize="13dp"
                android:id="@+id/tv_related_item_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_marginTop="3dp"
                android:textSize="10dp"
                android:id="@+id/tv_related_item_like_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

But it is not settings its weight properly. Actually, each row has two column and each column must be half of screen width. But it is not working.

This is the screenshot:

enter image description here

As you can see, it not taking screen half by half. How can I fix it?

Upvotes: 2

Views: 3441

Answers (3)

Ken
Ken

Reputation: 1393

Try this below in your realated_item_view.xml (wrap_content to fill_parent)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="fill_parent"
   android:layout_weight="1"
   android:padding="3dp"

And change your Java code:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
        itemContainer.setLayoutParams(params);
        itemContainer.addView(view);
        subContainer.addView(itemContainer);

But maybe your image will be stretched by the width. But it will fill half of the width as you want.

UPDATE:

I dont know why you tried failed with above solution. So I write a small demo for referencing. It uses ScrollView as you want. But I strongly recommend you should use GridView instead for better perfomance!!!

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_layout);

    LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container_layout);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    LayoutInflater inflater = getLayoutInflater();
    for (int i = 0; i < 4; i ++){
        LinearLayout subLayout = new LinearLayout(this);
        subLayout.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams subParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        for (int j = 0; j < 3; j ++){
            View v = inflater.inflate(R.layout.test_item_layout, null, false);
            if (j == 1){
                ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
                imageView.setImageResource(R.drawable.icon);
            }
            subLayout.addView(v, subParams);
        }
        containerLayout.addView(subLayout, params);
    }

}

test_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
    android:id="@+id/imageView"
    android:src="@mipmap/ic_launcher"
    android:scaleType="centerCrop"
    android:layout_width="match_parent"
    android:layout_height="100dip" />
<TextView
    android:text="Test label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

test_layout.xml

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

    </LinearLayout>
</ScrollView>

</LinearLayout>

Screenshot: enter image description here

Upvotes: 1

vipul parmar
vipul parmar

Reputation: 1

try this use straggerdgridlayout manager

RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true);

    gaggeredGridLayoutManager = new StaggeredGridLayoutManager(column, 1);
    recyclerView.setLayoutManager(gaggeredGridLayoutManager);

column replaces your requirement of column

Upvotes: 0

Ravi
Ravi

Reputation: 35589

Instead of ViewGroup.LayoutParams try it with LinearLayout.LayoutParams and also set layout width to 0

LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);

It might work.

Upvotes: 0

Related Questions