Why do people use layout.xmls in their resources like:
<resources>
<item name="main" type="layout">#layout/main_twopanes</item>
</resources>
while there are folders for alternative resources to use particular XML for particular configuration?
this is called Layout Aliases link here:
To avoid this duplication of the same file for tablets and TVs (and the maintenance headache resulting from it), you can use alias files. For example, you can define the following layouts:
And add these two files:
res/values-large/layout.xml:
<resources>
<item name="main" type="layout">#layout/main_twopanes</item>
</resources>
res/values-sw600dp/layout.xml:
<resources>
<item name="main" type="layout">#layout/main_twopanes</item>
</resources>
These latter two files have identical content, but they don’t actually define the layout. They merely set up main to be an alias to main_twopanes. Since these files have large and sw600dp selectors, they are applied to tablets and TVs regardless of Android version (pre-3.2 tablets and TVs match large, and post-3.2 will match sw600dp).
Related
I've got this super simple question I can't find an answer to.
I want to make a variable which I could then use for multiple elements.
Something like this in strings.xml:
<resources>
<string name="textSize">20sp</string>
</resources>
...
<EditText android:textSize="#string/textSize" />
But this does not work.
I was able to accomplish what I wanted the following way in themes.xml:
<style name="textSize">
<item name="android:textSize">20sp</item>
</style>
...
<EditText android:theme="#style/textSize" />
But it seems too complicated for just a single value, is there a better way?
As suggested in the comments, this looks like a dimen property rather than a string.
Just like strings.xml, you can have another file (usually dimen.xml) with dimensions. Your case could look like this:
<resources>
<dimen name="bigText">20sp</string>
</resources>
It also allows you to have different settings for different configurations (for example, screen sizes here: How to define dimens.xml for every different screen size in android?).
You can find the documentation here:
https://developer.android.com/guide/topics/resources/more-resources?hl=en#Dimension
I want to define beans into xml file and want to inject it using RoboGuice in android app, is it possible with RoboGuice?
I believe, the answer is NO.
Though you can inject objects of simple types. A simple example:
Place this in your res/values folder:
<resources>
<item name="flag" type="bool">false</item>
</resources>
And this in your res/values-land folder:
<resources>
<item name="flag" type="bool">true</item>
</resources>
In your activity:
#InjectResource(R.bool.flag) private Boolean flag;
Now you can use flag to determine whether device is in landscape mode.
I'm developing an app that has multilanguage support (using the /res/values-** way) with success. Then I want to use Holo and falling legacy devices (2.3.* for example) to use the default one (using the /res/values-v11 way).
So, I end up with a structure similar to this one (the one without language is EN, as default):
/res/values
/res/values-v11
/res/values-de
/res/values-de-v11
/res/values-es
/res/values-es-v11
... where in each one I have the following:
strings.xml
themes.xml
... where strings.xml is where the localised text are defined, and themes.xml has:
For non-v11 directories (legacy devices)
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme">
<!-- Any customizations for your app running on pre-3.0 devices here -->
</style>
</resources>
For -v11 directories (+3.0 devices)
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo">
<!-- Any customizations for your app running on devices with Theme.Holo here -->
</style>
</resources>
Remembering to add this attribute into the application tag on AndroidManifest.xml
android:theme ="#style/MyTheme"
This is working in all languages and all devices, current and legacy, with correct theme picking and everything. Tested in several physical devices.
So, the question:
Don't you think that this is heavily maintainable? I mean, then we have 2 string.xml files for every language which are exactly identical, but for every new text we have to fill it twice, increasing the risk of typos. The same happens if you have analytics.xml, styles.xml, ... inside
Having the language handling so nice in Android using strings.xml, is there any other workaround to have this working multitheme and multilanguage in a nicer way?
Thank you.
I'm not sure why do you need something like
values-de-v11
I would just use something like this:
values
values-de
values-fr
values-es
values-cat
...
And put inside every strings.xml file with the translation.
On the other hand you can also add the folders:
values
values-v11
And inside you can add your themes.
The folder values should have both the strings.xml for the default language (usually english) and the fallback theme file for devices without holo.
You can check all the possibilites in the documentation:
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
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
I have icons for my Android menu. On Android 3+ I'm using a black ActionBar so the icons are white. However, on Android 2.x the menu is inherently white which means the icons are nearly invisible. How can I use different menu icons for different versions? I'm assuming I can do it using different drawable directories like res/drawable-mdpi-v11, but I'm wondering if there is another way so I don't have to create a bunch of different directories as I add versions or pixel densities.
EDIT: I put dark versions in res/drawable-mdpi and res/drawable-hdpi for use with Android 2.x and I put light versions in res/drawable-mdpi-v11 and res/drawable-hdpi-v11 for use with Android 3.x and higher, but my Android 2.1 (sdk 7) emulator is still showing the light version.
Any idea why?
You can Select a theme based on platform version, as outlined in the Styles and Themes dev guide. Define a style in your res/values/styles.xml like this:
<style name="ThemeSelector" parent="android:Theme.Light">
...
</style>
Then in a res/values-v11/ folder, select your theme (probably Holo, if you're dark)
<style name="ThemeSelector" parent="android:Theme.Holo">
...
</style>
Then add icons to that style. For instance, here's a snippet from the styles.xml file from the HoneycombGallery sample application.
<style name="AppTheme.Dark" parent="#android:style/Theme.Holo">
...
<item name="menuIconCamera">#drawable/ic_menu_camera_holo_dark</item>
<item name="menuIconToggle">#drawable/ic_menu_toggle_holo_dark</item>
<item name="menuIconShare">#drawable/ic_menu_share_holo_dark</item>
</style>
The bottom 3 elements are all icons in the drawable directories. You'll still need at least one folder per resolution-specific set of icons, but you can combine the light & dark icons into the same folder, but you won't have to have different folders of icons for each platform version. Also, you'll need to list them as references in the values/attrs.xml file, like this:
<resources>
<declare-styleable name="AppTheme">
<attr name="listDragShadowBackground" format="reference" />
<attr name="menuIconCamera" format="reference" />
<attr name="menuIconToggle" format="reference" />
<attr name="menuIconShare" format="reference" />
</declare-styleable>
</resources>
At which point you'll be able to refer to them within your layout XML using the "?attr/NameOfYourDrawable" dereference, like this:
<item android:id="#+id/menu_camera"
android:title="#string/camera"
android:icon="?attr/menuIconCamera"
android:showAsAction="ifRoom" />
Found on the android dev site: http://developer.android.com/guide/practices/ui_guidelines/icon_design_menu.html
Warning: Because these resources can change between platform versions, you should not reference these icons using the Android platform resource IDs (i.e. menu icons under android.R.drawable). If you want to use any icons or other internal drawable resources, you should store a local copy of those icons or drawables in your application resources, then reference the local copy from your application code. In that way, you can maintain control over the appearance of your icons, even if the system's copy changes. Note that the grid below is not intended to be complete.
/res/drawable-hdpi (for Android 2.2 and below)
/res/drawable-hdpi-v# (for Android 2.3 and above)
Have you also tried testing this on a 2.1+ phone and not an emulator? If you don't have a phone, try creating another AVD? I'm afraid that you're going to need the separate folders.
Hopefully this helps.