I want to implement night mode in my app. I referred to this site:
DayNight Theme Android Tutorial with Example
I see we need to make the values-night directory and override the styles.xml file inside it. I already have other different values directory like values-v27 also overriding the styles.xml file. How do I implement the night theme for those specific directories? Like is it values-v27-night or is it values-night-v27? Is it even possible?
Of course you can have specific values for the night and for any other resource qualifier. For your example, the resource directory name will be values-night-v27.
If you are unsure of the order, you can right click the res node in your project structure and go to New > Android Resources Directory, then fill you criteria and the name will be automatically generated.
values-night-v27 puts an and clause between two conditions v27 and night , so it doesn't matter if you use values-v27-night.
Also if you put your values separately in values-v27 and values-night an or clause will be applied
Related
I have one theme -> theme1
it has to pick the drawable from the drawable-theme1 folder.
is there any way?
Currently there is no way to use a theme name as drawable folder qualifier
The available qualifiers are in the table 2 at this link:
https://developer.android.com/guide/topics/resources/providing-resources#AlternativeResources
If you can't find a valid alternative using the provided ones maybe you should think if a "drawable-theme" folder is the right approach to solve your problem
I am using this icon in my code:
android.R.drawable.ic_menu_close_clear_cancel
But it looks outlined, not simple cross:
I want to make it like this: https://storage.googleapis.com/spec-host-backup/mio-design%2Fassets%2F1Xlq7_lXkjBJDE6arihpaS3YWm4TSf2-S%2Fdialogs-fullscreen-behavior.mp4
Should I use another resource or tinting?
You can do it either way but Android advices against using platform resource ids. You can copy the icon into your drawables and use that resource id instead (and in your case tint it).
Android docs about the matter: https://developer.android.com/guide/practices/ui_guidelines/icon_design_menu
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.
I inherited the values folder structure shown below.
For over an hour I've Googled and looked and pulled down menus in Android Studio 1.2.2 and tried creating files and folders but nothing I have done lets me add, for instance, dimens.xml (sw720dp).
I deleted dimens.xml (sw720dp - landscape) because landscape didn't suit my needs.
What steps did whoever created the folder structure take in order to get dimens.xml (sw600dp) to appear in the structure?
What steps do I need to take to get a dimens.xml file with the sw<N>dp file naming convention? How do I get dimens.xml (sw720dp) where it belongs?
You can ignore the rest of this. I'm just showing that I've TRIED stuff, none of which works right.
Am I supposed to right-click values or dimens.xml (2)? What then?
Here's what I did and it didn't do as I need:
I also tried:
EDIT
I also tried copying the sw600dp file. No luck:
IT CAN'T BE THIS HARD.
What steps do I need to take to get a dimens.xml file with the swdp file naming convention?
First, you will probably be happier with life if you change the drop-down above your project tree from "Android" to "Project", which shows you an actual filesystem view of your project.
To add a resource directory from within Android Studio:
Right-click over res/
Choose New > "Android resource directory" from the context menu to bring up the New Resource Directory dialog:
Choose your resource type in the "Resource type" drop-down (in this case, values)
Click on your desired qualifier (in this case, "Smallest screen width") in the "Available qualifiers" list.
Click the >> button
In the field that appears, fill in your numeric value (in this case, 600)
Click OK to close the dialog and create the directory
To add dimens.xml to that directory, right-click over the newly-created directory and choose New > "Values resource file" from the context menu.
I have had MANY failures trying to do stuff OUTSIDE Android Studio, but this time I got away with it:
I will now see how #Commonsware says I could have done it.
EDIT
And as always, #Commonsware bailed me out with good advice (I have no idea when or why or how I lost the File structure "tab" along the left margin).
Here's what I now see, and (I was right:) it's NOT hard to see what to do ONCE YOU SEE THIS:
And, following his advice, I get to here and am ready to create the dimens.xml file that goes inside:
I've come across some android native code where the strings.xml in android would be named strings-global.xml and likewise the styles.xml would instead be called styles-global.xml. how is android identifying this ? what does it mean ?
The naming of files within the values\ folder is arbitrary. What resources the file provides is defined by the contents of the file itself, not the name. Which resources are used in a given configuration is determined by qualifiers on the values\ folder (e.g. values-fr), not the file name.
You could even mix and match resource types in a single file if you really wanted to, though I recommend following convention.
From the Providing Resources documentation for the values\ folder:
Because each resource is defined with its own XML element, you can
name the file whatever you want and place different resource types in
one file. However, for clarity, you might want to place unique
resource types in different files.
In this case, it sounds like the developers discovered that their styles.xml was getting too large and/or they found that they had a few distinct categories of styles that they wanted to separate, and thus put them in different files to keep them more organized.
I noticed the UI color (eg Button background/text color) all changes from device to device, based on the current theme that is being used in a device.
What is the best practice to apply custom UI colors for Android app, so that I have same color scheme for my app in all Android devices. I can set text/background color on a UI item. I'm wondering if there is a single place where I can define all the colors which will override the current theme applied on the phone.
thx.
Yes, there is a single place where you can define these values for your app. See Styles and Themes in the Android docs for how it works.
A style is just a mapping of values to predefined names. If you find yourself repeating a number of common attributes in your layouts, you can factor that out into a style. For example, you might have a special button style that defines a specific background and text color.
A theme is a sort of meta-style. It can be applied to an Activity or even a whole application through your AndroidManifest.xml. Among other things it defines the default styles for widgets and values that control other parts of the look and feel for your UI.
When you're trying to blend in with the system in an otherwise custom UI for your app, you can query the current theme for values. Just like you use the # reference syntax #android:drawable/foo when referring to a system resource, you can use the syntax ?android:attr/foo when you want to use the value stored in the system theme attribute foo.
In your case, if you want to change the primary text color across your app, apply a custom theme that sets the attribute textColorPrimary. If you just want to be sure that an element of your app is using the primary text color as defined by the device your app is running on, you can set android:textColor="?android:attr/textColorPrimary". The same principles apply elsewhere as well.
If you want to see what attributes are used in the system, they are defined as part of the Android framework in this file: frameworks/base/core/res/res/values/attrs.xml. Look at the children of the XML element <declare-styleable name="Theme"> at the top. To see examples of what the system sets these to, see themes.xml in the same directory. Finally, not all of these attributes are public - non-public attributes cannot be set by an app, they're implementation details of the Android framework. See public.xml for the complete list of which attributes are available for use in apps.
Best practice is to apply a custom theme to your application, and override as much of the default properties as you need.
Almost everything can be changed, except
The Menu
Some properties of AlertDialog (these can be changed using a custom dialog)
OS provided views such as the Quick Search Bar (QSB)
If you like the look of the default SDK resources then you can find these in sdk_folder/platforms/android-9/data/res/ (replace 9 with the SDK version you want the resources from) - copy the ones you want into your App and reference those.
You can take a look at the theme the SDK uses:
themes.xml
styles.xml