Different screensize and language in android - android

I want to support different screen sizes and languages at the same time. So I got some folders for my layout like
layout (German)
layout-en (English)
layout-fr (French)
etc...
and I got layout-large for big screens. So when I open another language in the large screen I got the layout from the small screen. Is there a way to manage this? layout-large-fr or layout-fr-large did not work...
And of course I defined translated strings.

Try this one:
Create on layout file for each language. Create on layout.xml in res/values-xx with this content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="main_layout" type="layout">#layout/activity_main_xx</item>
</resources>
The result should looks like this:

Related

When to use attrs.xml, when dimens.xml?

I like to integrate ads with AppLovin into my Android App.
In their documentation they say:
Declare the base banner height of 50dp in res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">50dp</dimen>
</resources>
I also have a dimens.xml present with other <dimen... value's for my app. They work from there as well.
What is the difference between attrs.xml and dimens.xml? What to use in a situation like this?
attrs.xml is a file that allows you to define custom attributes for your views in XML layout files. These attributes can be used to customize the appearance and behavior of your views, and can be accessed programmatically in your Java code.
dimens.xml on the other hand, is a file that allows you to define dimension values for use in your app. These values can be used to set the size and layout of views in your XML layout files, and can also be accessed programmatically in your Java code.
In this situation, you should use dimens.xml to define the banner_height dimension, because you will use this value to set the height of the banner ad view in your layout.
You could also use attrs.xml to define the banner_height attribute, but since you are defining a dimension value, not an attribute, it would be better to use dimens.xml instead.
It doesn't actually matter - <attr> and <dimen> are both value resources, and you can put them in any file in the res/values folder. They'll all be combined into the same set of resources, so the actual file you use is up to you! So you can do this if you want:
# res/values/whatever.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="someDimension">48dp</dimen>
<attr name="someColour" format="color"></attr>
</resources>
and it'll work exactly the same as if you used attrs.xml and dimens.xml. It's just convention to put attrs and declare-styleable things in a file called attrs.xml, all your dimension resources in dimens.xml, all your strings in strings.xml...
But you don't have to do that, it's up to you! For example, you might want certain resource strings to be stored in a different file, for organisation (maybe they shouldn't be translated). Or maybe some values are common to all configs and you want those in a single file, and you want a separate file for the qualified stuff (that goes in folders based on API level, night theme etc)

Android is ignoring /res/values-large/strings.xml

I want to use a different string resource on small screens but Android always uses the default string. Am I doing it wrong?
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue</string>
</resources>
/res/values-large/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue to Next Page</string>
</resources>
#string/continue always resolves to "Continue", even on large screens. It does, however use the dimensions in /res/values-large/dimens.xml so I'm sure I'm using the right resource qualifier. I also tried /res/values-w550dp/strings.xml and it didn't work either.
Am I doing something wrong or is this a limitation of Android?
values-large is only available for dimens and styles, but not for string resources.
one could possibly add custom string-arrays and then look them up accordingly.
see Android Resource Types.

Hiding views declaratively based on screen size in Android

In android xml:ish
Is there any way to change a visibility attribute based on the layout size/orientation in the xml directly?
I have a button in a fragment that should be visible for small screens sizes only. On larger sizes, let's say layout-large, I want it to be hidden.
Sure, I can write code for this without any problem but for academic reasons I would like to know it it's possible to do something like this.
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="<magic expression here>" />
Thanks
// Johan
This answer is based off the explanation provided here by Flávio Faria.
The visible, gone, etc can be values mapped to their corresponding enum values in a string resource - which means you can create a visibilty.xml with string resources for each layout you want, and Android will automatically resolve the one you want for each screen type.
I'd recommend the following:
/res/values/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Enum values pulled from Android source -->
<string name="visibility_visible">0</string>
<string name="visibility_invisible">1</string>
<string name="visibility_gone">2</string>
<string name="product_info_footer_button_visibility">#string/visibility_visible</string>
</resources>
/res/values-large/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="product_info_footer_button_visibility">#string/visibility_invisible</string>
</resources>
And then you can reference the visibility as follows for your button:
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#string/product_info_footer_button_visibility" />
Warning: This depends on the device having the same enum values for visibility (0/1/2) as defined in the AOSP source. Device manufacturers and custom ROM creators can change these values, in which case this code will likely not work as desired.
The android:visibility attribute is an int (like many attributes) so you can do the following :
Define a resource file named visibility.xml in values-port and values-land resource directories. The content of this file is like this :
values-port/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">0</integer> <!-- 0 is the value for android:visible -->
</resources>
values-land/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">1</integer> <!-- 1 is the value for android:invisible -->
</resources>
and in your layout.xml :
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#integer/buttonvisibility" />
It works : btn_check_availability is visible in portrait and invisible in landscape.
Note : this example use layout orientation as discriminator, but you can of course do it with any resource qualifier (like dimension, density, ...)
There is no magic expressions available in XML. If only.
There are two approaches to this problem:
a/ use the drawable folder system. Drawable folders can be copied and named to be DPI aware following the conventions dictated here: Supporting Multiple Screens.
b/ Do it programmatically. On runtime check for screen DPI and show/hide view accordingly.
Have you looked at using includes and multiple layouts organized into the appropriate size/orientation layout folders? Some layouts could either simply not have the button or have it hidden by default.
Re-using Layouts with include
Providing Alternative Resources

Whats the best way to build for multiple screen sizes?

I'm building an app now and I want it to work well across multiple screen sizes (phones, 7inch tablets and 10inch tablets). I've looked at the document on Supporting Multiple Screens but i still have some questions.
Right now I have 5 different layout folders, layout-: normal, large, xlarge, sw600dp and sw720dp. Is this the best way to build for all screen sizes?
And when i try to optimize each layout say the normal size which covers some 3 and 4 inch screens I get issues where I can't get my layout to look right on all of them.
What am I doing right and what could I change?
I don't believe that there's any best way, it will be just your way to do it. If you are planing to make the Images, buttons, textview smaller or bigger you can use the method you use or style.xml.
Style.xml example:
You will have some folders like:
values
values-large
values-normal
values-small
values-xlarge
where inside the folders you have a styles.xml with different values:
<style name="BigTextStyle">
<item name="android:textSize">60dp</item>
</style>
<style name="SmallTextStyle">
<item name="android:textSize">48dp</item>
</style>
<style name="ImageSize">
<item name="android:layout_width">100dp</item>
<item name="android:layout_height">100dp</item>
</style>
just change the size depending on the screen you are working with, then in the layout.xml add this:
<ImageView
android:id="#+id/imageSD"
style="#style/ImageSize" />
<TextView
android:id="#+id/imageNameSD"
style="#style/BigTextStyle" />
I believe you can use this resource alias sir.
Suppose you have two different images for sw320dp and sw600dp.
image1_sw320dp.png
image1_sw600dp.png
Put all image resource to folder drawable.
The in the folder values-sw320dp, create an xml resource. (also maybe you need for values-small, just copy the xml from values-320dp)
<resources>
<item type="drawable" name="image1">#drawable/image1_sw320dp</item>
</resources>
In folder values-sw600dp, create an xml resource.
<resources>
<item type="drawable" name="image1">#drawable/image1_sw600dp</item>
</resources>
Then use on the activity with R.drawable.image1. This can prevent for creating duplicate resources for example sw320dp, small, and normal.
Mr Rotary has a good way to work with multiple screen size.
Furthermore You can use Fragment in your application.
You can learn how to use fragment here : http://developer.android.com/guide/components/fragments.html

Storing dimensions in xml file in Android

I am trying to consistently use the same dimension in all the views in my android app (e.g., a left margin of 20dp). If I were using HTML, I would simply use a CSS file, but I am at a loss of how to do this on Android.
Is there a way I can store this value in a xml file inside res/values and then use it in layouts?
e.g., I thought of storing them in strings.xml like
<string name="app_wide_left_padding">20dp</string>
and then using the following text in layout.xml
android:paddingLeft="#string/app_wide_left_padding"
but I am not sure it will work. Am I on the right track?
Thanks in advance for your answers.
Create a file called res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="icon_width">55dip</dimen>
<dimen name="icon_height">55dip</dimen>
<dimen name="photo_width">170dip</dimen>
<dimen name="photo_height">155dip</dimen>
</resources>
Then reference them in your other xml:
android:padding="#dimen/icon_width">
You can use styles (res/values). This article will be a good start.

Categories

Resources