0

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);
            }
Qiteq
  • 675
  • 1
  • 6
  • 22
  • That's because you pass ```null``` as the ViewGroup when you inflate the view. You should pass the container of that view as the ViewGroup. – danypata May 12 '16 at 11:40
  • I did something like that: ViewGroup viewGroup = (ViewGroup) findViewById(R.id.historyView); But now i cant even start activity, i am getting IllegalStateException: "The specified child already has a parent. You must call removeView() on the child's parent first.", in line with addView method – Qiteq May 12 '16 at 11:46

1 Answers1

0

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

Jahnold
  • 7,623
  • 2
  • 37
  • 31
  • But what if i am getting IllegalStateException? "The specified child already has a parent. You must call removeView() on the child's parent first.", in line mainView.addView(layout); – Qiteq May 12 '16 at 11:58
  • I've updated the code in the example. It now uses the full LayoutInflater rather than the View convenience method so that it doesn't attach to the root immediately. – Jahnold May 12 '16 at 12:02
  • I will try this when I go home – Qiteq May 12 '16 at 12:07
  • I had to change parameters order in inflater.inflate method to inflater.inflate(R.layout.history_bar, mainView, false); and it works. But in list with textViews, every one of them has the same R id: i.imgur.com/UFlP2FJ.png, and i can edit only the last one. Any idea to fix it? – Qiteq May 12 '16 at 16:58