Xml based bean creation possible wiht RoboGuice? - android

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.

Related

Android Studio reusable xml value

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

Multilanguage and multitheme at same time

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

What is the purpose of layout.xml?

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).

android - How to create xml's id?

I'm developing a dynamic keyboard application using soft keyboard sample. My application changes layout. For example I have a keyboard(has one key) then I set up the app and I can use keyboard(has one key).
I want to create an XML file after compilation and use this file in the application (reading XML file with xmlpullparser or xmlresourceparser). However, keyboard class needs XML's id. How do I create an XML id?
It can be defined in ids.xml or any custom file like constants.xml inside your res\values folder.
Examples:
<resources>
<item type="id" name="keyboard" />
</resources>
and can be accessed in the code as following:
R.id.keyboard
You can define ids using an ids.xml file in your res/values directory. Here's an example:
<resources>
<item type="id" name="my_keyboard" />
</resources>
In code, you would set the id like so:
keyboardView.setId( R.id.my_keyboard );
XML files are compiled (binary XML) and thus you don't create them at runtime.
If you want to change your keyboard layout dynamically, you'll do that programmatically rather than in XML.

How to use a theme defined variable in res/values/dimens.xml?

Howdy.
In my themes.xml definition, I have the following:
<style name="mythemename">
<item name="d_myvar">100dip</item>
</style>
I would like to be able to reference this in res/values/dimens.xml like so:
<dimen name="myvar">?d_myvar</dimen>
Alas, this doesn't work. When I try to use the #dimen/myvar as the height of a LinearLayout, the app crashes with the error "You must supply a layout height attribute."
I have also tried
<dimen name="myvar" value="?d_myvar" />
But that won't compile.
How can I define #dimen/myvar in my xml so that it loads the ?d_myvar variable defined in the theme?
Thanks!
I saw your help request on the Italian Startup Scene.
Unfortunately, according to the syntax of the dimen tag:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen
name="dimension_name"
>dimension</dimen>
</resources>
you simply can't do it. In fact, you can reference theme attributes when the syntax specifies:
?[package:][type:]name
Solutions:
Gix's answer would be the standard way of defining and reusing dimensions.
Maybe you can reorganize your code to reuse d_myvar through inheritance?
As a last and desperate resort, I would go for a shell script that automates the process of variable substitution using xml command line tools. I have never personally used them, but see for example xmllint, xmlstarlet or this article.
Put the 100dip part in your dimens.xml like so:
<dimen name="myvar">100dp</dimen>
then in your theme you can reference it as #dimen/myvar if you need, or you can reference it in code using R.dimen.myvar
In other words, you don't set the dimension in theme and then reference it in dimens.xml, but you go the other way around. You set the dimension in dimens.xml and then reference that in your theme/style xml.
In styles.xml
<resources>
<attr format="dimension" name="exampleDimension"/>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="exampleDimension">100dp</item>
...
</style>
</resources>
Then in your other xml to use the new attribute, you would use it like this
android:padding="?attr/exampleDimension"

Categories

Resources