Android developer
Android developer

Reputation: 1322

XML layout cannot be reconstructed programmatically

So at first I made an XML document and designed my layout there. It works perfectly and here it is:

<?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"
            android:id="@+id/scrollView">
        <LinearLayout
                android:id="@+id/list"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <LinearLayout style="@style/Row">
                <LinearLayout style="@style/EntryContainer">
                    <ImageView style="@style/Pic"
                               android:id="@+id/imageView42" android:src="@drawable/ic_launcher"/>
                    <TextView style="@style/Rate"/>
                </LinearLayout>
                <LinearLayout style="@style/EntryContainer">
                    <ImageView style="@style/Pic"
                               android:id="@+id/imageView423" android:src="@drawable/ic_launcher"/>
                    <TextView style="@style/Rate"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout style="@style/Row">
                <LinearLayout style="@style/EntryContainer">
                    <ImageView style="@style/Pic"
                               android:id="@+id/imageView2" android:src="@drawable/ic_launcher"/>
                    <TextView style="@style/Rate"/>
                </LinearLayout>
                <LinearLayout style="@style/EntryContainer">
                    <ImageView style="@style/Pic"
                               android:id="@+id/imageView3" android:src="@drawable/ic_launcher"/>
                    <TextView style="@style/Rate"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

And there are styles:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Row">
        <item name="android:orientation">horizontal</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
    </style>

    <style name="EntryContainer">
        <item name="android:orientation">vertical</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:layout_weight">1</item>
    </style>

    <style name="Pic">
        <item name="android:layout_width">100dp</item>
        <item name="android:layout_height">100dp</item>
        <item name="android:layout_gravity">center_horizontal</item>
    </style>

    <style name="Rate">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:text">@string/underpic_rate</item>
        <item name="android:gravity">center_horizontal</item>
    </style>
</resources>

Ok but I need to add these rows on runtime so at first I tried making new Views and passing concrete styles to its constructors (3rd parameter). It just didn't work. It looked like it hadn't added any style at all.

Then I tried using LayoutParams and it almost worked, but I can't change orientation of my LinearLayouts. When I do something like:

myLinearLayoutObject.setOrientation(LinearLayout.HORIZONTAL);

It just doesn't listen to me.

I can post more of my code but I'm not sure if it's needed. Basically I don't know why passing style to View's constructor doesn't work. It would be the simplest solution.

Upvotes: 0

Views: 99

Answers (2)

n1nsa1d00
n1nsa1d00

Reputation: 876

You can use LayoutInflater.

Create another xml layout file representing the row - firstrow.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"
          style="@style/Row">
            <LinearLayout style="@style/EntryContainer">
                <ImageView style="@style/Pic"
                           android:id="@+id/imageView42" android:src="@drawable/ic_launcher"/>
                <TextView style="@style/Rate"/>
            </LinearLayout>
            <LinearLayout style="@style/EntryContainer">
                <ImageView style="@style/Pic"
                           android:id="@+id/imageView423" android:src="@drawable/ic_launcher"/>
                <TextView style="@style/Rate"/>
            </LinearLayout>
        </LinearLayout>    

Then in your code do this:

LayoutInflater inflater = getActivity().getLayoutInflater();
LinearLayout rootView=(LinearLayout)inflater.inflate(R.layout.firstrow, null);
final LinearLayout container = (LinearLayout)findViewById(R.id.list);
container.addView(rootView);

Upvotes: 1

Stephen
Stephen

Reputation: 4249

You should look into using a RecyclerView instead. This will allow you to have a list of similar view elements generated automatically based on the underlying data set.

If you have a dynamic set of list elements that will change throughout the runtime of your app, this will provide a much more flexible and elegant solution than manually creating a list with views yourself

It will take a bit more time to learn, but it will be worth it in the long run.

Upvotes: 1

Related Questions