androidandroid-layoutviewandroid-linearlayoutlayout-editor

Inflating View into LinearLayout and editing cmponents in this view


I am trying to add multiple LinearLayouts into one declared in xml. Everyone has 3 textView which will be edited in code. My problem is, when i am inflating xml layout to View object in code, all margins are ignored. My second question: How can i dynamically set ids to textViews and then edit text in it?

LinearLayout xml which is inflating:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/pointsAwaiting"
    android:layout_marginTop="40dp"
    android:background="@drawable/background_blue"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#FFFFFF"
            android:textSize="20dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:textColor="#FFFFFF"
            android:textSize="15dp" />

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="2"
        android:padding="10dp"
        android:textColor="#FFFFFF"
        android:textSize="20dp" />

</LinearLayout>

He is inflating into this piece of code:

<

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:background="@drawable/background"
    tools:context="pl.com.qiteq.zielonomocni_rework.HistoryActivity">

<LinearLayout
        android:id="@+id/mainView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp">

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



        </LinearLayout>

    </LinearLayout>

</ScrollView>

And finnaly java code: (loop counter is for example)

LinearLayout mainView = (LinearLayout) findViewById(R.id.historyView);

            for (int i=0; i<=2; i++){
                View layout = View.inflate(this, R.layout.history_bar, null);
                mainView.addView(layout);
            }

Solution

  • The reason all your margins are being ignored is that you are passing null into your inflater here:

    View layout = View.inflate(this, R.layout.history_bar, null);
    

    When you do this all the LayoutParams of your view are thrown away. You should replace it with:

    View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
    

    To set text in the TextViews you don't need to set a different id for each one, just give each one an id. Then in your loop you can do something like:

    List<TextView> textViews = new ArrayList<>();
    LayoutInflater inflater = LayoutInflater.from(this); 
    
    for (int i=0; i<=2; i++){
    
        View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
        mainView.addView(layout);
    
        TextView textView = (TextView) layout.findViewById(R.id.text1);
        textViews.add(textView);
    }
    

    You would then have a reference to each of your TextViews in the List