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...
Related
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
In my app, i need to keep an action to change the font size. I am trying to keep three set of values for text sizes (small,medium and large). How should i switch between them? My idea is to use one set of font resource at a time.
For more clarification:
In Theme.AppCompat.DayNight.DarkActionBar, to shift between night mode on and off in i can call
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
and
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
Here, each will use different set of resource values for night mode on and off. I am looking for somewhat such a method to shift between different font sizes. Any help is appreciated.
There's something like that built into the operating system. If you go to Settings > Accessibility you'll find a Large text toggle or a slider to make text bigger (depending on phone). This will work in your app as long as you use sp for your font sizes (for example android:textSize="16sp").
If you're going to use that, you'll have to remember that the setting is in the phone settings, I don't think there's a way for you to change it from your app. And it applies to all the apps, not just to yours, so everything in the system would get bigger. Which might be not what you require.
Otherwise you can try to do this manually in your app. One way would be to create three themes that have your different font sizes. Then you would set an appropriate theme on the context in your Activity.onCreate() based the setting you saved somewhere (in SharedPreference for example).
But I don't think you can just keep different font sizes in different resource subdirectories. The list of resource qualifiers is predefined and I don't think there's something for switching font sizes in there.
I have a Xamarin Android project where I am using dimension files to specify text sizes, paddings, margins etc for different screen sizes/resolutions.
I have noticed that there is a pronounced difference between setting the value directly within the layout and within code.
For example, if I add the following to a TextView the layout XML
android:textSize="#dimen/nav_tile_text_size"
then I would expect the result to be the same as doing this in code
myTextView.SetTextSize(ComplexUnitType.Sp, _context.Resources.GetDimension(Resource.Dimension.nav_tile_text_size))
The dimension file contains the following:
<dimen name="nav_tile_text_size">17sp</dimen>
but the resulting UI shows that the size has not been correctly applied
What am I missing here??
Thats correct. You would need to either use
myTextView.SetTextSize(Android.Util.ComplexUnitType.Sp, Resources.GetDimension(Resource.Dimension.nav_title_text_size)/Resources.DisplayMetrics.Density);
Unit, otherwise gets multiplied by the density of the phone. I have personally checked this code, and it works
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.
i have used absolute layout in order to display image buttons in my application's main.xml.
how exactly it affects to using my app on different density screens..
Absolute Layout is deprecated and should not be used, it will make a mess of handling various screens.
Best practices for screen layouts
That is right, absolute layout will always make problem, that is why it is not at all recommended to use.
Please refer this Supporting Multiple Screens