I am quite familiar with Android layouts and the fact you can declare an app wide theme in the manifest or indirectly via the Styles resource such as "Halo", or "Material", etc... .
I also know that style can be done on a per layout basis. Android Studio "Layout Editor" even allows you to select a theme for the layout.
Question:
Where is the selected theme stored when applied per layout?
The Android Documentation seems to suggest its in the manifest where the activities are declared. For example:
<activity android:name=".MainMenu" `android:theme="#android:style/Theme.Holo.NoActionBar"/>`
However, when I select a different theme, even after I compile and run the application I see no changes in the manifest, or respective layout, or the Activity sub class.
So where is the association between a layout and the chosen theme for it being stored?
tl;dr
AndroidManifest.xml references styles.xml which in turns references colors.xml. The layout editor modifies styles.xml, you're not seeing the results because of possible overrides in styles.xml.
-- Modify the parent style in styles.xml and removes unwanted overrides within. --
After investigating this issue as I was also coming across this problem, here is the conclusion that I came to:
Starting with a new "Empty Activity" you will see the following in your AndroidManifest.xml file: android:theme="#style/AppTheme". That is not directly related to changes in the Layout Editor in the "Design View" of your current activity.
AndroidManifest.xml
Hovering over the theme definition you will this below:
styles.xml
Clicking on it will take us to styles.xml. By default, you will see the following:
This is where AppTheme is coming from that is being referenced in the AndroidManifest.xml. As you see, Theme.AppCompat.Light.DarkActionBar is the parent style, and if you want to change the overall theme you will need to modify that, e.g. "#android:style/Theme.Material" or "#android:style/Theme.Holo".
It is worth noting that as long as the override colors are still there, you will not notice much changes when the app is launched, even though the changes did go through.
To see the changes commend the following lines:
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
By deleting or commenting:
<!-- <item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>-->
Theme Layout Editor
For some reason Android Studio 2.2 got rid of the refresh button on the layout causing many difficulties in the update of the style.
For the time being, manually override the parent style in styles.xml and remove any overrides that are being imported from colors.xml. Not ideal, but it's a solution.
For a full list of themes you can check this developer R.style link or by clicking on Open Editor to get a visual representation of the different themes.
Which displays the following:
I believe the answer to this question is that Android Studio does not currently modify anything when you select a layout in the preview, it is just a preview and that is it.
To make the change one has to manually edit the theme for the activity entry in the manifest.
Related
In my current project, when I select the Design tab in Android Studio 2.2.2 for a particular layout I get an error that says
Missing styles. Is the correct theme chosen for this layout?
and it goes on to say
Failed to find style 'textEditSuggestionItemLayout' in current theme
(42 similar errors not shown)
But, the actual layout renders OK in the Designer and at runtime.
There are lots of other S.O. posts on this, such as here and here and most of the answers seem to involve clearing the caches and restarting Android Studio, or selecting a different theme from the dropdown. I I have tried the first one but it didn't help. I haven't tried the second one yet because I don't really understand what a theme is.
Questions:
The error implies that there's an error in the theme itself. What
is that? Is the theme file part of my project, i.e., is it one
that I should be creating, editing, and that gets built and shipped
as part of my APK or is it only used in the developer IDE?
If I select a different theme from the dropdown how do I know what the
"correct" one is?
Since my project builds and runs OK as is, can I just ignore these
errors? In other words are these errors in my code or just a
problem with the development environment?
Edit: Some additional information after responding to comments, below:
The only place the string 'theme' is used in my manifest is
android:theme="#style/Theme.FullScreen"
... and FullScreen is the theme specified in the dropdown.
I did a search in my project for the string "textEditSuggestionItemLayout" and Android Studio found no occurrences of it.
If you've recently updated AS or any of its components, then try to 'Invalidate caches & restart'.
Else, try choosing AppTheme from the Design tab or change it to AppTheme from the default one. Choose the one that conforms to your parent app theme in your styles.xml.
The cause of the error could be anything from malfunctioning IDE to selecting a theme not mentioned in your styles.xml (most likely).
You could do a project-wide search for textEditSuggestionItemlayout, or the other styles that it claims to miss.
These are either in your own res/values/styles.xml and/or res/values/themes.xml files, or they are provided within the SDK, which is why you see some suggestions to uncheck 'Automatically Pick Best' and pick a version you have installed. In some cases, the latest API version's rendering tools don't work.
As an example, if you have your style set at Theme.Holo, but you are using the AppCompatActivity, you'll get a rendering error that you must use a Theme.AppCompat (or descendant). These themes are changeable from the dropdown of the design editor.
You can see what themes/styles are applied to your Activities within the AndroidManifest.xml.
res/values/styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/PrimaryColor</item>
<item name="colorPrimaryDark">#color/PrimaryDarkColor</item>
<!-- Customize your theme here. -->
</style>
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
Note: android:theme= and #style/
If you are still missing themes, then you may be missing some dependency, such as
compile 'com.android.support:design:<your_version_here>'
If your code runs fine, then sure, ignore it, but I think getting the layout designer working again shouldn't be ignored.
I know how to set the theme for an Application and/or Activity in Android, but I'm wondering if it's possible to change it for an Activity using the Graphical Layout editor in Eclipse 4.2, ADT 20.0.1. No matter what I change the drop-down to, the AppTheme style from styles.xml is always used when I run the app, despite the display preview changing based on what I select. Is it currently not possible because the theme gets set in the AndroidManifest?
styles.xml:
<resources>
<style name="AppTheme" parent="android:Theme.Light" />
</resources>
The drop down box in the GUI is used to see what it looks like, if you want the theme changed then you have to edit the Android Manifest yourself
If you want to edit the theme using a drop down, in one of the tabs in the Android Manifest, I think its "Application" there is an option to change the theme and it has a list of themes.
Yep, the theme must be set in the Manifest or in Java.
The drop-down on the layout editor is only there to help debug and test theme issues and whatnot, it doesn't have any effect on your actual app.
I've perused ALL of the questions here and countless tutorials on Styling and Themes for Android. The odd hierarchy between Themes and Styles is still a mystery to me.
My Manifest calls out this "Style" (android:theme, "#style/rforderstyle") [this is formatted from the manifest editor in Basic4Android] (not my choice but its what I have to use.).. however the manifest is still accessible should I need to.
I mention this because I've noticed that you can style a specific activity. I actually don't see a way to force have multiple "wigets" have their own "theme" while they all exist on the same activity.
Please advise if so then I my need to make my "little creations" as whatever "fragment" or "wigets".
What I'm attempting is a windows mobile app type form with a ton of User controls on it..so this set at the bottom happens to use a mid sized black text font.. but the control over here in the right upper is a different text size and color on & On & on ...
So regarding the style xml file, I've read that,,, its important that,,,, my parent be some all encompassing Android theme and I override individual "properties" in this section::
Could that mean that although I'm concerned with text size, since I have labels and editcontrols present the absence of a "parent" style that addresses these will cause me an error as I attempt to use this referenced parent style ??
How does a theme get into this mix...?
<resources>
<style name="rforderstyle" parent="#android:style/TextAppearance">>
<item name="android:textSize"> 21.0dp</item>
<item name="android:typeface">serif</item>
<item name="android:textColor">#FF9900 </item><!--#00FF00-->
</style>
</resources>
This is oddly confusing because the best examples reference layout files for the views but I don't really have layout files in Basic4Android I'm programmatically creating all of these views. If I don't need the depth of theme and style then fine but ...is the manifest supposed to point to a Style or a theme?
I'm trying to make a custom titlebar for my first Android application.
While I can find lots on the web about how to make them so you can change colours etc, I want my titlebar to look the same as the "standard" titlebar, but with a button that I can configure. This means copying the device's currently active themes to be able to style it in exactly the same way.
Not all devices simply use a gradient in the titlebar style, so adding a gradient (as suggested in other SO questions) doesn't really make sense.
Does anyone have any pointers how to read the style information?
try to extend an existing theme e.g.
create your own style which can ofcourse extend from existing from an existing theme. change the windowNoTitle to true.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="noTitleBarStyle" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/darkGrey</item>
<item name="android:colorForeground">#ff3333</item>
</style></resources>
or try to do it runtime as discussed here
Android - change custom title view at run time
I hope this helps.
I tried changing the appearance of a spinner and I partly succeeded. I'm doing this via overriding parts of the theme. I managed to change the text size of the spinner item (i.e. the text size in the drop down button) with my themes.xml and styles.xml:
My themes.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="#android:Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:spinnerItemStyle">#style/CustomSpinnerItem</item>
</style>
</resources>
My styles.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomSpinnerItem" parent="#android:Widget.TextView.SpinnerItem">
<item name="android:textAppearance">#style/CustomTextAppearance</item>
</style>
<style name="CustomTextAppearance">
<item name="android:textSize">30dp</item>
</style>
</resources>
However I cannot find the attributes that are responsible for the text appearance of the items in the dropdown list of the spinner. I tried dropDownItemStyle amongst other things. In my opinion the attribute names are not self-explanatory, so I wanted to know whether there is a documentation of what attribute does what in a style to find out which attributes to override. I found it very cumbersome to trace back all the styles used in a theme via the themes.xml and styles.xml of the platfrom and then try to find the right attributes via trial and error.
I know that one can change the appearance by passing layouts to the adapter, however, this is not really what I was looking for, since (as far as I know), you can only use inheritance in styles and not in layout xml files. If I created a custom layout for the adapter I'd have to create 9-patch images etc., which I think is a bit too time consuming in case I only want to change the text size.
Of course it's possible that I misunderstood the whole concept, since I'm new to Android ;)
You probably have found out the answer since you asked but for others looking at similar questions:
I do not know of a list of attribute names with good explanation of what they do (R.attr's page mostly gives information that is already in the name) but the way I do it is:
Start from the element I give to setDropDownViewResource(), in my case: android.R.layout.simple_spinner_dropdown_item and find.
Find its layout definition in \sdk\platforms\android-17 (specific platform version to avoid redundant results).
Get its style from the layout file. In this case: ?android:attr/spinnerDropDownItemStyle
We now have the attribute name we need.
It's better to do it that way rather than try to guess what attribute to use because you know which attribute the system itself use so it's very likely to be the correct one (unless there's a bug).
If I created a custom layout for the adapter I'd have to create
9-patch images etc.
Well, no, the layout determines what kind of GUI element you would have (a textfield, a spinner, an imagebutton, a custom element...), not how they are styled (nine-patch backgrounds, text colors...), so you still would have to mess with styles to get the right appearance.
For example, for visual consistency I ported the button, checkbox and spinner style from Theme.Holo to Gingerbread, yet I did not mess with layout, all I did was the aforementioned steps plus looking up the result (spinnerDropDownItemStyle in the above example) in themes.xml, which gave me the style name (e.g.: Widget.Holo.DropDownItem.Spinner).
Then I looked that up in styles.xml and imported it (and any parent*) in my project's styles.xml, searching and copying any Holo specific reference in my project and adjusting the namespace accordingly (add android: to attributes and replace ?android:attr with #style for what I copy to my styles.xml file).
So far I haven't had to mess with layouts at all (even the presence of radio buttons in spinner dialogs on Gingerbread is determined by an xml attribute: android:checkMark).
If a style has no parent attribute (like Widget.Holo.DropDownItem.Spinner) then its parent is the same style minus the last element (e.g.: Widget.Holo.DropDownItem)