Different integer resource values for phone and tablet? - android

As described here: https://developer.android.com/guide/topics/resources/more-resources#Integer
I added a resource value for integer at /res/values/integers.xml
Now, I would like to add the same integers.xml for tablet devices? Like it is done for dimension resource files:
Is that possible?

Yes it's possible. Use resource qualifiers on the values like you do with your dimens, for example res/values/integers.xml, res/values-land/integers.xml and so on. That is easier to see when you view your project as a file tree rather than the Android project view you have in your screenshot.

Related

dimens.xml for different layouts don't work in my favor

I'm encountering a problem with creating dimens.xml for different layout:
If I use these files, the layout won't work properly. I have tried this method with my Redmi 5 Plus phone and my layout was just broken.
But if I delete all of these folders and leave the original dimens.xml file out, the layout will work properly like before. What have I done wrongly?
Hello henry there is no need to add values folder each time you add dimens for a
different version just create one value folder and add all the dimens in that.
You have to create different versions of dimens not the values.
Still if you face the error then show me the .xml code

How to automatically change drawables depending on a flavor?

I'm working on a really messy app and first time with flavors. App has like 4,5 flavors all with identical background colors, drawables, etc. Only logo is different.
Now I have to make a new flavor with completely different layout. Background color should be yellow instead of grey and drawable images should be black instead of white. The problem is, I have like 100 different white images. Is there a way I can change them to black automatically? (designer can redraw them or whatever to be black and I can put them in drawable folder of that flavor) Or I would have to programmatically check if new flavor is used then call setDrawable(newBlackImage) on every widget that uses white drawable? That seems like a very complicated way to do since all drawables are set via XML layout..
The way they've been using flavors was creating resource bool file and then check in code if some key has value true/false then hide/show some things... So I assume I should do something similar with this, or no?
This is what the project structure for one of my flavors looks like:
As you can see, every flavor only has different launcher and login screen icons. Also each has their own arrays, bools and strings resource files. But all of them share same layout resources and drawables with white icons.
I think you're already in the right way. By creating specific flavours for each group of drawable in your gradle file you'll just need to create a drawable folder for the flavours with specific files.
For example, the flavour 'black' will need a drawable folder in which all your black-background images will be stored.
In order to get it working, all resources must exist for all flavours so you won't need to change any part of your code.
You can find a quick guide here:
https://medium.com/#thiagolopessilva/the-handling-multiple-java-source-and-resources-using-flavors-on-gradle-18a4b581285b

Select layout xml files most appropriate, depending on screen size

I would like to know if You Could select a layaout xml Differently Depending on the size of the phone screen.
I try to be more clear, I make more layout xml file, and I would like my app handles this statement
setContentView (R.layout.main)
first verifying on that size of the display is performed, and Therefore You Set the most appropriate layout xml
What you could actually do is the following:
Create a new folder for each desired configuration and name that folder with qualifiers.
If you want your application to have a different main.xml layout for different screen sizes you should make the following folders in your project.
/res/layout-small
/res/layout-normal
/res/layout-large
/res/layout-xlarge
In each of those folders have your different main.xml file layout (or whatever resource you want). Android will pick the one it matches with the device is running your application.
You can even add different resources for different configurations for drawables and even for language.
More info: http://developer.android.com/guide/topics/resources/providing-resources.html
Have you looked at this page yet?
You can query the size of the device screen, and then select which xml layout you want to use for that size screen...

are color and dimension resources necessar?

In Android is it necessary to define the colors and dimensions as xml resources in res directory as opposed to just directly specifying the color code/dimension in the layout xml files.I understand with strings you define them in resources for localization.What about for these 2?
Actually it is not necessary, but it may help you in many situations, ie. if you want to change the textcolor of all your textviews, you only need to change it in your "color"-file.
is no necessary to have them in xml, but it's more useful like that, you write less code if you want different colors for different screen sizes and it's a standard... there are a lot of benefits of using xml...

Android - How to create layout as generic as possible?

I am having trouble in creating generic layouts for my application. As expected, it can be used in a variety of devices and I want it to work properly for each of them. There are several approaches to achieve this problem but I want to create an xml file (similar like web.config files) and at the very beginning of my application I want to take the device's screen width and height and calculate each control's (textview, spinner, button etc.) attributes (such as margin, padding,width, height...) according to this width and height and save these calculated values into my xml file. Finally I want to reach these values from my layout xmls so my layout's visual will be independent from the device and will work properly for each device. Can this be achieved? I could not find any similar solution on the internet. Can anyone help me?
Thanks in advance.
You can do most of this without hard coding values using RelativeLayout and similar mechanisms. The two pass dynamic layout system is made for exactly what you're describing.
However, when you need to be more specific, that's where the dynamic resource system can help you out. For everything you define in res/drawable, res/layout, res/values, etc, you can define specific implementations for device orientation, pixel densities, screen size or even language by qualifying sibling directories with the correct format. Provide a resource with the same name in different folders, and the system will decide which to use based on the runtime environment.
Give this a look:
http://developer.android.com/guide/topics/resources/providing-resources.html
I would not use custom measurements to dynamically set layout parameters. Android specifically has a variety of functionality to address this for you (including supplying multiple image resources, or layouts specific to a screen size).
I have discovered that the more you try to customize the Android layout with hard-coded values (always use DP if you do want to set specific parameters).
Bottom line, you should not try to re-invent the wheel, and just use the well-designed functionality that Android has already built-in to accomplish what you want.

Categories

Resources