I have a style that includes textColor, textSize, textStyle and typeface. When applied directly to an EditText widget, the color of the text is as specified (as well as the other attributes), but when applied as a theme to the activity or the entire application, the size is fine but the color is not applied. What I am missing?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="fap" parent="android:Theme.Holo">
<item name="android:textSize">24sp</item>
<item name="android:textColor">#FF0000</item>
<item name="android:textStyle">normal</item>
<item name="android:typeface">normal</item>
</style>
</resources>
This is quite simple : you are not overriding android default style, your just creating a new one which extends android:Widget.EditText. Thus, the style is not applied.
To correct this, into your theme definition, just add :
<item name="android:editTextStyle">#style/fap</item>
Now, each time Android instanciate an EditText, when it load default style values, it will find your fap style.
Edit:
searching through android's source code is very usefull. Check https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/attrs.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/themes.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/EditText.java
for example.
EditText widget just can't get these parameters from an activity theme. It gets its default style from the android:editTextStyle parameter of the activity theme. So you have to create your own style:
<style name="MyEditText" parent="android:Widget.EditText">
<item name="android:textSize">24sp</item>
<item name="android:textColor">#FF0000</item>
<item name="android:textStyle">normal</item>
<item name="android:typeface">normal</item>
</style>
And then set it as EditText style in the activity theme:
<style name="fap" parent="android:Theme.Holo">
<item name="android:editTextStyle">#style/MyEditText</item>
</style>
Hope this will work because I haven't tried this code.
Related
I am trying to understand how to change the look of all checkboxes within my application for a certain theme.
I would like to change the checkbox check color and the checkbox border color.
If I create this style:
<style name="MyCheckBox" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="colorControlNormal">#8AFFFFFF</item>
<item name="colorControlActivated">#android:color/white</item>
</style>
And apply it as a theme on my checkbox it works great.
<CheckBox
android:id="#+id/status_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyCheckBox"/>
However I don't really want to apply that theme to every checkbox, I would like to set the theme for all checkboxes.
I tried to set the checkboxStyle in my theme however this doesn't work.
<style name="MyTheme" parent="Theme.AppCompat">
...
<item name="checkboxStyle">#style/MyCheckBoxStyle</item>
...
</style>
<style name="MyCheckBoxStyle" parent="Base.Widget.AppCompat.CompoundButton.CheckBox">
<item name="colorControlNormal">#8AFFFFFF</item>
<item name="colorControlActivated">#android:color/white</item>
</style>
Is there a way to set a default theme for all checkboxes?
Unfortunately, colorControlNormal and colorControlActivated are theme attributes, not style attributes, so they only work if they're defined in the view's theme. There is no way that I know of to set a "default theme" for all views of a certain type; attributes like checkboxStyle can only set a default style for all checkboxes. Additionally, you can't "trick" the system by writing something like:
<style name="MyTheme" parent="Theme.AppCompat">
...
<item name="checkboxStyle">#style/MyCheckBoxStyle</item>
...
</style>
<style name="MyCheckBoxStyle" parent="Base.Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:theme">#style/MyCheckboxTheme</item>
</style>
<style name="MyCheckboxTheme">
<item name="colorControlNormal">#8AFFFFFF</item>
<item name="colorControlActivated">#android:color/white</item>
</style>
Your choices are:
Modify colorControlNormal and colorControlActivated in your app's theme
Modify colorControlNormal and colorControlActivated in your activity's theme
Manually set the android:theme attribute to every checkbox you want to change (or to the parent viewgroup holding these checkboxes)
Edit: potential workaround
Depending on your exact requirements, you might be able to get away with this:
<style name="MyTheme" parent="Theme.AppCompat">
<item name="checkboxStyle">#style/MyCheckboxStyle</item>
</style>
<style name="MyCheckboxStyle" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="buttonTint">#8AFFFFFF</item>
</style>
This will change the color of every checkbox in your app without requiring the use of theme attributes. You can even use a color selector resource for buttonTint if you want different colors for checked/unchecked:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_checked="true"/>
<item android:color="#8AFFFFFF"/>
</selector>
For example when I define a style tag in XML, all views of all types get that theme. Is there any solution to difference between styles for view types? here is some code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="buttonTheme" parent="android:Theme.Holo">
<item name="android:drawableLeft">#drawable/ic_launcher</item>
</style>
<style name="textTheme" parent="android:Theme.Holo">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
I think there's a small confusion in terms between a style and a theme. You are talking about defining custom styles for a widget in your application. A Theme is a collection of these styles applied globally to an Activity or Application. The custom styles you have created for a button and text view can be applied to a new custom theme so all buttons and text items share the same attributes.
I think what you are looking for is something more like this.
<style name="ApplicationTheme" parent="android:Theme.Holo">
<item name="buttonStyle">#style/MyButtonStyle</item>
<item name="textAppearance">#style/MyTextAppearance</item>
</style>
<style name="MyButtonStyle" parent="android:Widget.Button">
<item name="android:drawableLeft">#drawable/ic_launcher</item>
</style>
<style name="MyTextAppearance" parent="android:TextAppearance">
<item name="android:textColor">#ff0000</item>
</style>
Here, we have created two new styles (one for the button appearance and one for default text appearance), and then applied those as attributes in a new custom theme. You can now apply this theme in your manifest or in View constructors by referencing #style/ApplicationTheme or R.style.ApplicationTheme
You can do this by adding the attribute android:theme="" to the specific activity in your AndroidManifest.xml file
Example:
<activity android:theme="#style/CustomTheme">
I'm trying to make all EditText's in my application have a consistent look. I'm aware that I can do something like this:
<style name="App_EditTextStyle">
<item name="android:background">#drawable/filled_roundededges_box_dark</item>
<item name="android:textColor">#808080</item>
<item name="android:layout_height">45dip</item>
</style>
Then I can make a particular EditText have this style by doing this:
<EditText ...
style="#style/App_EditTextStyle
...>
But this way I have to remember to set the style individually for each and every EditText in my application which is tedious, if not error prone.
Is there some way I could make this a part of a theme or something? This is so I don't have to associate this style to every EditText. Something like this fictitious code block:
<style name="App_Theme" parent="#android:style/Theme.Holo">
...
<item name="android:EditTextSyle">#style/App_EditTextStyle</item>
...
<style>
And then in my AndroidManifest.xml I have something like:
<application
....
android:theme="#style/App_Theme">
And Voila! all my EditText's have the consistent style without me having to specify the style for each instance.
Override the attribute pointing to the EditText style(named editTextStyle :) ) in your custom theme:
<style name="App_Theme" parent="#android:style/Theme.Holo">
<item name="android:editTextStyle">#style/App_EditTextStyle</item>
</style>
and make your custom style to extend Widget.EditText:
<style name="App_EditTextStyle" parent="#android:style/Widget.EditText">
<item name="android:background">#drawable/filled_roundededges_box_dark</item>
<item name="android:textColor">#808080</item>
<item name="android:layout_height">45dip</item>
</style>
Edit:
If you're using the much newer AppCompat related themes use the editTextStyle attribute without the android prefix:
<style name="App_Theme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="editTextStyle">#style/App_EditTextStyle</item>
</style>
#Luksprog answer is correct but not working for me. After some experimentation, I found out that removing the android namespace from editTextStyle made it work for me.
<style name="App_Theme" parent="#android:style/Theme.Holo">
<item name="editTextStyle">#style/App_EditTextStyle</item>
</style>
and make your custom style to extend Widget.EditText or if using the AppCompat theme Widget.AppCompat.EditText:
<style name="App_EditTextStyle" parent="#android:style/Widget.EditText">
<item name="android:background">#drawable/filled_roundededges_box_dark</item>
<item name="android:textColor">#808080</item>
<item name="android:layout_height">45dip</item>
</style>
First, define the style for your EditText. Make sure that the parent style is android:Widget.EditText
<style name="CustomEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#0F0F0F</item>
<!-- ... More items here if needed ... -->
</style>
After that, override the attribute android:editTextStyle in your custom theme. Be aware, if you are using the support library, you will also need to override the attribute editTextStyle (without the android namespace).
<style name="App_Theme" parent="...">
<item name="android:editTextStyle">#style/CustomEditTextStyle</item>
<item name="editTextStyle">#style/CustomEditTextStyle</item> <!-- For compatibility with the support library -->
</style>
If you just need to set a few simple parameters, like text color, the Android namespace has a few parameters that will do that without the need to declare a separate style for the edit text. Eg
<style name="MyStyle" parent="android:Theme.Material.NoActionBar">
<item name="colorPrimary">#color/black</item>
<item name="colorPrimaryDark">#color/white</item>
<item name="colorAccent">#color/white</item>
<item name="android:textColor">#color/black</item>
<item name="android:editTextColor">#color/black</item>
<item name="android:editTextBackground">#color/black</item>
....
</style>
<style name="App_Theme" parent="#android:style/Theme.Holo">
<item name="android:editTextBackground">#drawable/filled_roundededges_box_dark</item>
</style>
I'm writing an App which needs the following two things:
The general text (in TextViews) needs to be one color (white, in this case - dark background)
The text on a Spinner needs to be a different color (black, since white's too hard to read)
I used a theme, applied at the Application level in the manifest, to perform the first item above.
<resources>
<style name="GlobalTheme" parent="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
Worked great. Except it also makes the text on the spinner white, which is hard to read.
OK, so I want the spinner color to still be black but everything else be white.
I found this question which showed how to set the Spinner text color, and it works, but only when I'm not also setting the global textColor as well.
So the following does not work:
<resources>
<style name="GlobalTheme" parent="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<item name="android:textColor">#FFFFFF</item>
<item name="android:spinnerItemStyle">#style/GlobalThemeSpinnerItem</item>
</style>
<style name="GlobalThemeSpinnerItem" parent="android:Widget.TextView.SpinnerItem">
<item name="android:textAppearance">#style/GlobalThemeTextAppearanceSpinnerItem</item>
</style>
<style name="GlobalThemeTextAppearanceSpinnerItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
<item name="android:textColor">#000000</item>
</style>
</resources>
I guess I was hoping that this would be like CSS and things would cascade down (i.e., "all text shall be white, except that which is in a spinner"). If I remove the android:textColor line in the main theme, the spinner color trick works fine.
It looks like the SpinnerItem derives from TextView so I tried to come up with a textViewStyle-type separation similar to the spinnerItemStyle separation, but didn't have any luck.
Unlike most people who ask about this, I want to keep it in the XML as much as possible. Does anyone know what I'm doing wrong?
Try this:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="GlobalTheme" parent="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<item name="android:spinnerItemStyle">#style/GlobalThemeSpinnerItem</item>
<item name="android:textViewStyle">#style/GlobalThemeTextViewItem</item>
</style>
<style name="GlobalThemeTextViewItem" parent="android:Widget.TextView">
<item name="android:textAppearance">#style/GlobalThemeTextAppearanceTextViewItem</item>
</style>
<style name="GlobalThemeTextAppearanceTextViewItem" parent="android:TextAppearance.Widget.TextView">
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="GlobalThemeSpinnerItem" parent="android:Widget.TextView.SpinnerItem">
<item name="android:textAppearance">#style/GlobalThemeTextAppearanceSpinnerItem</item>
</style>
<style name="GlobalThemeTextAppearanceSpinnerItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
<item name="android:textColor">#000000</item>
</style>
This will make the text in your TextViews white and the text in your Spinners black.
I'm using Theme.Dialog on one of my activities. I'm using setTitle(mMyTitle) to set the title. I would also like to set the textAppearance. How would I go about this?
I'm not sure if I can make my own title layout for this, since I'm already using Theme.Dialog.
Create a new style and extend Theme.Dialog then override what you want to change. Example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyActivityDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/MyTitleStyle</item>
</style>
<style name="MyTitleStyle" parent="#android:style/DialogWindowTitle">
<item name="android:textAppearance">#style/MyTextApperance</item>
</style>
<style name="MyTextApperance" parent="#android:style/TextAppearance.DialogWindowTitle">
<item name="android:textColor">#ffff0000</item>
<item name="android:textSize">36sp</item>
</style>
</resources>
Anything you don't specify will use the Dialog defaults. I've only specified the colour and font size of the textApperance in this example.
Now just use MyActivityDialogTheme (or whatever you call your style) as the theme for your activity instead of Theme.Dialog.
Check this out first, but I believe what you're looking for is about 3/4 the way down when it discusses tweaking a built-in resource by referencing it as a parent for your custom theme. As such:
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Dialog">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
Hope it's what you're looking for.