Search code examples
javaandroidxmlandroid-layoutandroid-linearlayout

LinearLayout addview() disables margin on first child


I'm having a problem with adding views dynamically to LinearLayout. I'm trying to inflate cardViews from xml. I'm adding 4 CardViews and the every view is OK except the first which have no margins. I have no idea why. (picture below)

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("Select Sale");

        ShowSales(getSales());
    }

    private void ShowSales(Sale[] sales) {
        LinearLayout sale_layout = (LinearLayout) findViewById(R.id.sale_layout);
        for (Sale sale : sales) {
            CardView saleCard = (CardView) getLayoutInflater().inflate(R.layout.sale_card, (ViewGroup) findViewById(R.id.sale_card), false);
            ((TextView) saleCard.findViewById(R.id.sale_name)).setText(sale.name);
            ((TextView) saleCard.findViewById(R.id.sale_desc)).setText(sale.desc);
            ((TextView) saleCard.findViewById(R.id.sale_price)).setText(sale.price + getString(R.string.dollar));
            sale_layout.addView(saleCard);
        }
    }

    //temp method to get sales - in future will get sales from server
    private Sale[] getSales() {
        return new Sale[]{
                new Sale("Sale number 1", "desc desc desc desc", 14.90),
                new Sale("Sale number 2", "desc desc desc desc", 19.90),
                new Sale("Sale number 3", "desc desc desc desc", 25.90),
                new Sale("Sale number 4", "desc desc desc desc", 17.90),
        };
    }

    class Sale {
        String name, desc;
        double price;

        public Sale(String name, String desc, double price) {
            this.name = name;
            this.desc = desc;
            this.price = price;
        }
    }
}

activity_main.xml

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

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/sale_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:orientation="vertical" />

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:clickable="true"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>
</ScrollView>

sale_card.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sale_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:clickable="true"
    android:foreground="?attr/selectableItemBackground"
    app:cardCornerRadius="@dimen/cardview_default_radius">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp">

        <TextView
            android:id="@+id/sale_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_margin="8dp"
            android:text="Sale name"
            android:textSize="27sp" />

        <TextView
            android:id="@+id/sale_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@id/sale_name"
            android:text="this is description of the sale" />

        <TextView
            android:id="@+id/sale_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:text="100&#36;"
            android:textSize="23sp"
            android:textStyle="bold" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

enter image description here


Solution

  • Instead of:

    CardView saleCard = (CardView) getLayoutInflater()
        .inflate(R.layout.sale_card, (ViewGroup) findViewById(R.id.sale_card), false);
    

    Perform:

    CardView saleCard = (CardView) getLayoutInflater()
        .inflate(R.layout.sale_card, sale_layout, false);