0

I'm facing an issue setting margin to an inflated relative layout. Here is the code:

for(Produit prod : GlobalVariables.getInstance().getPanier())
        {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout layoutToInflate = (RelativeLayout)inflater.inflate(R.layout.cart_product_layout, null);

            layoutToInflate.setTag(prod);

            ManagedNetworkImageView prodPicture = (ManagedNetworkImageView) layoutToInflate.findViewById(R.id.productImage);
            prodPicture.setImageUrl(prod.getImageDefaultUri(), GlobalVariables.getInstance().getImageLoader());

            ImageView addBtn = (ImageView) layoutToInflate.findViewById(R.id.addBtn);
            ImageView removeBtn = (ImageView) layoutToInflate.findViewById(R.id.removeBtn);
            //Ajouter les actions sur les boutons de gestion de panier.

            TextView productTitle = (TextView) layoutToInflate.findViewById(R.id.productTitle);
            productTitle.setText(prod.getNom());

            TextView productSpecs = (TextView) layoutToInflate.findViewById(R.id.productSpecs);
            //Ajouter les spec choisi par l'utilisateur.

            TextView productPrice = (TextView) layoutToInflate.findViewById(R.id.priceTextView);
            productPrice.setText(Float.toString(prod.getPrixTtc()));

            RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            relativeParams.setMargins(0, 10, 0, 0);  // left, top, right, bottom
            layoutToInflate.setLayoutParams(relativeParams);


            productInCartContainer.addView(layoutToInflate);

        }

The goal is to inflate as much layouts as I have objects in my list, into a LinearLayout (productInCartContainer --> The parent view) with vertical orientation. All the layout have to be separated by a margin of 10.... I don't know what I'm doing wrong by there's no margin at all... I saw many post on that subject but nothing works... Does somebody sees what I'm doing wrong ?

Thanks in advance !

Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63
  • Inflate and add the RelativeLayout using productInCartContainer as the second parameter for the inflate method(where you currently pass null). – user Sep 08 '15 at 16:09
  • if the above does not work, you may also try [MarginLayoutParams](http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html) – Klotor Sep 08 '15 at 16:17
  • I've just tried, it gives me an error when I'm adding the inflated layout, on the last line. It says that the parent has already a child. – Hubert Solecki Sep 08 '15 at 16:20
  • Using the inflate method like I said above means you're already adding the RelativeLayout to the container. – user Sep 08 '15 at 17:31
  • Thanks it works but not for the bottom margin or top margin... All the layouts are stuck together – Hubert Solecki Sep 09 '15 at 08:24

1 Answers1

1

I've just found the solution. Indicating at which position we would like to add the inflated layout in the root layout, solves the issue. Without indicating that, only left and right margins were working. I found the solution thanks to that post:

Solution is here...

Anyway, I wanted to add the whole solution again as everybody can notify the changes:

int index = 0;
        for(Produit prod : GlobalVariables.getInstance().getPanier())
        {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout layoutToInflate = (RelativeLayout)inflater.inflate(R.layout.cart_product_layout, null);

            layoutToInflate.setTag(prod);

            ManagedNetworkImageView prodPicture = (ManagedNetworkImageView) layoutToInflate.findViewById(R.id.productImage);
            prodPicture.setImageUrl(prod.getImageDefaultUri(), GlobalVariables.getInstance().getImageLoader());

            ImageView addBtn = (ImageView) layoutToInflate.findViewById(R.id.addBtn);
            ImageView removeBtn = (ImageView) layoutToInflate.findViewById(R.id.removeBtn);
            //Ajouter les actions sur les boutons de gestion de panier.

            TextView productTitle = (TextView) layoutToInflate.findViewById(R.id.productTitle);
            productTitle.setText(prod.getNom());

            TextView productSpecs = (TextView) layoutToInflate.findViewById(R.id.productSpecs);
            //Ajouter les spec choisi par l'utilisateur.

            TextView productPrice = (TextView) layoutToInflate.findViewById(R.id.priceTextView);
            productPrice.setText(Float.toString(prod.getPrixTtc()));

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(10, 20, 10, 0);
            layoutToInflate.setLayoutParams(params);

            productInCartContainer.addView(layoutToInflate, index);
            index++;

        }
Community
  • 1
  • 1
Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63