Android studio set custom attr into theme in manifest - android

I wonder if this is correct to use custom attr in theme in manifest. In theory everyfing on device works fine, but Android Studio IDE stops displaying colors by the line number. I would like to have my colors preview back and on the other hand keep my attr in theme in manifest. How to achieve that?
Here is what I have done:
AndroidManifest.xml
<application
(...)
android:theme="?attr/currentTheme">
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="notepad_light" parent="Theme.AppCompat.Light.NoActionBar">
(...)
<item name="currentTheme">#style/notepad_light</item>
</style>
<style name="notepad_dark" parent="Theme.AppCompat.NoActionBar">
(...)
<item name="currentTheme">#style/notepad_dark</item>
</style>
</resources>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="currentTheme" format="reference" />
</resources>

Related

Force style layout properties to be set by programmatically assigning parent beforehand

I have a style with specified margins that are not applied in a button because at the moment of creating the button it is not included in layout yet:
Custom attribute:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myButtonStyle" format="reference"/>
</resources>
Custom Style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="btn_program_style"/>
<style name="ProgrammaticallyStyle" parent="Widget.MaterialComponents.Button">
<item name="android:id">#id/btn_program_style</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">5dp</item>
</style>
</resources>
Attribute+Style assignation to theme:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.ProgrammaticallyMaterialButtonApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="myButtonStyle">#style/ProgrammaticallyStyle</item>
</style>
</resources>
Code:
btnStyleButton = MaterialButton(this, null, R.attr.myButtonStyle)
linearlayoutButtons.addView(btnStyleButton)
How can I assign beforehand that btnStyleButton will be included in linearlayoutButtons so its margin definitions will be set? Instancing button with different context? Could inflator work here somehow?
I know I can assign btnStyleButton.layoutParams = customLayoutParams after creating the button instance, but I want it to be taken directly from style.

Navigationbar and statusbar color for v21 in Android app (no theme libraries)

I'm working on some app that I did not create and I wanted to set navbar and notificationbar colors for API 21 and up. The theme of the app is pure XML without the new app theme stuff libraries. Can I even set those colors having such a weird theme setup and using only xml? Can someone explain noob friendly what to add where?
I tried various stuff with
<item name="android:navigationBarColor">#color/theme_color</item>
But I always seem to confuse things.
I only have themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.phttpp.TitleBar" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/windowTitleBackgroundStyle</item>
<item name="android:windowTitleStyle">#style/windowTitleStyle</item>
<item name="android:windowTitleSize">50dip</item>
</style>
<style name="Theme.dialog.TitleBar" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/windowTitleStyle</item>
<item name="android:windowTitleSize">50dip</item>
</style>
</resources>
and
styles.xml in values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="windowTitleBackgroundStyle">
<item name="android:background">#00BCD4</item>
</style>
<style name="windowTitleStyle">
<item name="android:textColor">#FFFFFF</item>
<item name="android:padding">12dip</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#00BCD4</item>
<item name="android:maxLines">1</item>
</style>
</resources>
I found a solution that works for API21 and up.
I did not change anything in themes and styles, but I added colors.xml to /values-v21
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#00BCD4</color>
<color name="colorPrimaryDark">#008394</color>
<color name="colorAccent">#33c9dc</color>
<color name="colorGray">#e0e0e0</color>
</resources>
And added this to my main activity to onCreate in my Java code
if (Build.VERSION.SDK_INT >= 21) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
getWindow().setNavigationBarColor(getResources().getColor(R.color.colorGray));
}

ActionBar color does not change on 2.3.3

I'm developing an Android application that targets the min. SDK version of 9. I'm trying to set the color of the action bar, and it does change on a Lollipop device, but not on a 2.3.3 device.
Following the instructions from here, I've set up the custom theme in the manifest:
<application
android:theme="#style/AppTheme" ...>
And here's the themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/AppActionBar</item>
<item name="actionBarStyle">#style/AppActionBar</item>
</style>
<style name="AppActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/my_orange</item>
<item name="background">#color/my_orange</item>
</style>
</resources>
The 2.3.3 phone shows the theme otherwise correctly; the light theme is applied with a dark action bar. However, the custom color is not applied.
You can use different styles for different API version. Create new folder with the name of values-v11, and move your file themes.xml there.
Then, create a new themes.xml file in the folder values and put there this code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="AppTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/AppActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/AppActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="AppActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
</style>
</resources>

Android Use custom themes to modify style attributes

I want to create two themes, GrayTheme and RedTheme, that modify one style attribute. For example here are my two themes, default font color is white, which is fine for both themes:
<style name="RedTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">#color/white</item>
</style>
<style name="GrayTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">#color/white</item>
</style>
But I have a style that I use for Header TextViews. If I am using the RedTheme I want the style HeaderFont to have textColor red and if it is the GrayTheme I want the HeaderFont textColor to be gray, without me having to modify the individual xml files that access this HeaderFont style.
<style name="HeaderFont" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/gray</item>
</style>
I've been searching around looking for a graceful solution to this, but haven't been able to find anything. Ideas?
After many failures of finding a clean solution I finally found an answer that works beautifully.
I created a custom attribute headerFontColor and added it to /res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="headerFontColor" format="reference|color" />
</resources>
Next, in the style for HeaderFont I added updated the textColor to be the new custom attribute (headerFontColor) instead of a specific color
<style name="HeaderFont" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">?headerFontColor</item>
</style>
Now, I can simply set the headerFontColor attribute based on the theme
<style name="RedTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">#color/white</item>
<item name="headerFontColor">#color/red</item>
</style>
<style name="GrayTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">#color/white</item>
<item name="headerFontColor">#color/gray</item>
</style>
After doing this, all TextViews that use the HeaderFont style get updated with the headerFontColor just by switching the Theme
This solution guided me: Setting a color based on theme
I used references from google's iosched project.
Created an attrs.xml file in res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Theme">
<attr name="headerFontColor" format="color" />
</declare-styleable>
</resources>
Then in themes.xml
<style name="RedTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="headerFontColor">#color/red</item>
</style>
<style name="GrayTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="headerFontColor">#color/gray</item>
</style>

How to change styles dynamically in android

I would like to change myTheme color dynamically. I need to replace the color #FF0000 with my dynamically generated color.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme">
<item name="android:textColor">#FF0000</item>
</style>
</resources>

Categories

Resources