In the end, I found a solution to the problem by looking at Android docs. I originally used LinearLayout, which contained the image inside it (in fact, it had a lot more content, but in this example I will be just simple :), which looked something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/cover" android:layout_width="@dimen/cover_width" android:layout_height="@dimen/cover_height"/> <TextView android:id="@+id/title" android:gravity="top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="@dimen/title_font_size" android:scrollHorizontally="true" android:maxLines="1" android:ellipsize="end" /> </LinearLayout>
I would like to be able to dynamically change whether the text was next to or below the image, depending on the screen size of the device. In other words, I wanted to dynamically change android:orientation dynamically. My first thought that I posted in my question was that in res / values /dimen.xml a string variable should be specified as
<string name="orientation">horizontal</string>
and another string variable declared in res / values-large / dimen.xml as
<string name="orientation">vertical</string>
Then when I set the orientation to LinearLayout, I thought I could use
android:orientation="@string/orientation"
But that did not work. What I ended up was splitting the layout. Initially, I had doubts about having two separate layouts, because I thought I would have a lot of duplicate code for one simple change. That was before I found out about inclusion and integration. First I created a generic layout file, which was an image and text in res / layout / content.xml, which looked like this:
<merge xmlns:android="http://schemas.android.com/apk/res/android" > <ImageView android:id="@+id/cover" android:layout_width="@dimen/cover_width" android:layout_height="@dimen/cover_height"/> <TextView android:id="@+id/title" android:gravity="top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="@dimen/title_font_size" android:scrollHorizontally="true" android:maxLines="1" android:ellipsize="end" /> </merge>
(Sidenote: I was initially confused by what I did the merge tag. It does not combine what is inside the merge tag (in this example, the image and text) basically says that any parent file includes this file, merge the contents between the tags into the parent file)
Then I created two separate LinearLayout-only files that included an XML image and description file. One in res / layout / container.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/library_item_container" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <include layout="@layout/content"/> </LinearLayout>
and one in res / layout-large / container.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/library_item_container" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <include layout="@layout/library_item_contents"/> </LinearLayout>
Note that the difference between the two container.xml files is that the orientation has changed from vertical to horizontal. Now there is minimal code that repeats and the problem is solved!