2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • You must call requestLayout() for any layout changes to take effect. – Nanoc May 17 '16 at 13:16
  • I tried. But as you can see in screenshot, items and its images are not having the same with to each other. – Wai Yan Hein May 18 '16 at 10:28
  • If you want to make it with weights you will need to add another view so it can make them 50% width, anyway thats ok for a xml where you dont have much control but in code you should just calc and use pixel values. – Nanoc May 18 '16 at 10:57

3 Answers3

1

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

Ken
  • 1,303
  • 13
  • 15
  • Not working. I already tried by setting width to match_parent – Wai Yan Hein May 18 '16 at 11:02
  • Sorry because of my mistake, you need also change your LayoutParams when adding item view from WRAP_CONTENT to FILL_PARENT(or MATCH_PARENT). See my updated answer. – Ken May 18 '16 at 11:57
0

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.

Ravi
  • 34,851
  • 21
  • 122
  • 183
  • It gives me the same. I edited the screenshot. Problem is I want each image take half of screen no matter what size it is. But not woorking. Please how can I fix that? – Wai Yan Hein May 18 '16 at 10:31
  • @WaiYan this answer is right, but also you need to add another view with weight – Nanoc May 18 '16 at 11:00
0

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