Android: how to use xml string value to determine layout orientation

I have a simple linear layout that I would like to change based on the screen size of the device. What I'm trying to do is something like

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="@string/cover_orientation" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 

I created different dimen.xml files under the xlarge values ​​and values, so the cover_orientation variable will take a different value (either "vertical" or "horizontal") depending on the size of the screen.

 string name="cover_orientation">"vertical" 

But that does not work. I found a temporary job that includes checking screen size and manually changing the orientation:

 if(getResources().getString(R.string.screen_size) == "xlarge"){ ((LinearLayout)convertView).setOrientation(1); } 

but it looks like you should do it the first way (much more elegant / less code).

I thought I only had a layout for each screen size, but the layout is actually quite large, and this is the only change I need for different screen sizes. Therefore, I did not make sense to duplicate the entire layout.

Thanks!

+6
source share
4 answers

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!

+7
source

A good way to do this is to add

 android:orientation="@integer/cover_orientation" 

on your LinearLayout and defining it as shown below.

in values/consts.xml :

 <resources> <integer name="orientation_horizontal">0</integer> <integer name="orientation_vertical">1</integer> </resources> 

in values/something.xml :

 <resources> <integer name="cover_orientation">@integer/orientation_vertical</integer> </resources> 

in values-land/something.xml :

 <resources> <integer name="cover_orientation">@integer/orientation_horizontal</integer> </resources> 

This way you avoid hard coding zeros and ones in the orientation definitions of the orientation in the application.

+8
source

@odiggity, thanks for posting this question. I tried to do the same. The application crashed at startup.

I would suggest that this is a typing issue at runtime. In other words, for an orientation attribute, there are only two valid values ​​that are not reflected in the "type" string. What you probably need to do is introduce another type of specialized resource, similar to dimen or boolean.

I feel that there is an answer to your question that addresses more cases than your own answer above. You can use style inheritance to define all attributes except orientation in the orientation-independent parent style, and then add only orientation in the small orientation-oriented style definition with that as the parent.

Thus, duplication can be avoided even in difficult cases.

+2
source

Source: https://habr.com/ru/post/891150/


All Articles