Android Layout: use dimen just for some resolution - android

I'm trying to have an image which preserves its size in all the resolution except one... what i would like is to use the "wrap_content" attribute for height and override it with a dimen attribute but just for a particular case (like values-xlarge). I did like this:
<ImageView android:id="#id/image_frame"
android:layout_width="match_parent"
android:layout_height="#dimen/overlayImageHeight" />
But of course it crashes if i don't define the overlayImageHeight in all the dimen.xml files.
I tried to put a "wrap_content" string for other dimen.xml files but again it fails since dimen files just accept numeric values inside.
Is there a way to define this behaviour inside an xml file? Other way to do it programmatically?
Any help appreciated!

You should give it a style, define the style in res/values/styles.xml, then make a different version of the style in res/values-xhdpi/styles.xml.

Related

How to use different percent values for guidelines in ConstraintLayout?

I use constraint layout in my activity and I would like to set different percent values for my guidelines (app:layout_constraintGuide_percent="0.5"). I want to put this values to different dimens files (dimens-lands, dimens-sw600dp etc.) The problem is that I can't find any way how to put this percent value to dimens.xml. Is there any way how to keep different percent values for multiple screens in my res/? This is how percents are declared - it is a float value:
<android.support.constraint.Guideline
android:id="#+id/center_guideline"
style="#style/Layout.Guideline"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
The answer above does not work. It does not even show up in autocomplete when typing there app:layout_constraintGuide_percent="..."
Tried cleaning and rebuilding as well. Still no luck. Weird that it worked for you.
EDIT: What worked is changing the type from dimen to integer :
<item name="some_percentage" type="integer" format="float">0.6425</item>
In the end I found out a way how to store value with decimal point (float) in res/values. It must be stored this way:
<resources>
<item format="float" name="guideline_right" type="dimen">0.84</item>
</resources>

Android Studio reset my #dimen in xml

I have some problem with setting params to my view in xml file with values from dimen files.
For example when i'm adding layout_height param to my EditText in laytout xml file:
<EditText
....
android:layout_height="#dimen/et_height"
....
/>
File dimens:
<resources>
.........
<dimen name="example">16dp</dimen>
<dimen name="et_height">20dp</dimen>
.........
</resources>
It works fine. But sometimes when i open this file again AndroidStudio replace #dimen/et_height with value 20dp in layout xml file. And i must change it again to #dimen/et_height.
How can i fix this ?
This is the normal case:
But in my case AndroidStudio replaces with value:
Having the same issue. Thought it is the ConstraintLayout but the issue also appears in every other layout though..
related post from me :
Android ConstraintLayout #dimens replaced with hardcoded values
Also some other user said it just shows the dp value, i am completely in your situation i know the grey text, but it does really replace the value..

How to synchronize layout changes?

I start more or less the development of android. I needed to create different layouts celon the android versions but now I would like to move a button. How to move this button on all the corresponding layouts whatever version of android at one time?
If there is already a post on it, sorry but I have not found it.
It's for the same screen size of course
Example: I have a linear layout of which I want to modify the right margin and I want this to do on the linear layout for versions <21 AND >21 by only doing it once and not two
Thanks !
If you have two layout files:
res/layout-v11/layout.xml AND
res/layout/layout.xml
You'll need to change the margin values in both files if they are hard coded.
Otherwise, you can have:
<LinearLayout
...
android:layout_marginEnd="#dimen/left_margin" >
and in res/values/dimens.xml have
<dimen name="left_margin">16dp</dimen>
and just change the value once in the dimens.xml file.

Make changes to all layout files at once in android

I know that we can have different layout files for supporting different screen sizes in Android.
Does anyone know if there is an option to change all other layout files when I make changes to the original layout file? For example, say I have a layout file - main.xml under layout, layout-large, layout-sw600dp and layout-sw720dp directories. If I make some changes to the main.xml in the layout directory, is there any setting which would automatically make that change in the other layout directories as well?
For the formatting capabilities I use an answer for this. This refers to the comment above and elaborates on Aleksey's answer.
<include
android:id="#+id/some_id_if_you_nee_one"
layout="#layout/some_other_xml_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
You may add any additional (orientation or resolution specific) formatting like below the layout= statment.
Everything that is not specific to the current resolution/orientation and common for all resolutions/oriantations should go into some_other_xml_file.xml
This works for all full views and subclasses of view. If you want something similar only for groups of style attributes then you can start a styles.xml and refer to the styles with style="..." statements.
The answer is no. And i'm not sure there is such tool. Exceptions: naming of params (strings, drawables, etc.).
Some hint: compose layouts from small parts, that inserted using
<include />.
So when you change small parts – all layout changes (not sure with different sw-*** layouts, but in one folder it works).

Using resource values as layout attributes (config.xml)

Looking in the android sdk folders, I've found a file called values/config.xml. This seems to be somewhere that you can define values for later use in layouts and animations.
Given the config.xml:
<resources>
<string name="config_somePadding">50dip</string>
</resources>
How would I reference this to use as the layout_height in a layout xml file?
#string/config_somePadding is actually the only one I've found that doesn't throw an error in Eclipse (even though there isn't a config_somePadding in values/strings.xml), but it appears to just put an empty string.
In the sdk, they use an integer for animation duration. They reference it like this: android:duration="#android:integer/config_longAnimTime". Is there a way to use values that aren't integers in the layout_height attribute?
How would I reference this to use as
the layout_height in a layout xml
file?
You wouldn't. Use a dimension resource instead.
Is there a way to use values that
aren't integers in the layout_height
attribute?
You have to use a dimension resource for dimensions.

Categories

Resources