Lollipop v21, FragmentDialog doesn't take my activity theme - android

I have my activity theme as below set.
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:colorButtonNormal">#color/colorPrimary</item>
</style>
The activity have a simple FragmentDialog that is started using (Kotlin code)
MyDialogFragment().show(supportFragmentManager, MyDialogFragment.TAG)
The MyDialogFragment does have a Button. Hence I expect the color of the Button is colorPrimary as per the theme. However the color of the Button (on v21) is only grey)
This works on Marshmallow (i.e. v23) and not Lollipop (v21). I haven't tried v22. ... So I guess the v21 doesn't automatically inherit the theme from the activity.
For KitKat and below, this doesn't apply, as it doesn't use 'android:colorButtonNormal'
How should I get my FragmentDialog get the theme that I set on my activity?

I found a way to do it, which is to explicitly define my FragmentDialog theme as below, on top of defining my activity theme.
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:colorButtonNormal">#color/colorPrimary</item>
</style>
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:colorButtonNormal">#color/colorPrimary</item>
</style>
Then I need to explicitly set it from my FragmentDialog onCreate().
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialogTheme)
}
Note: It has to be in onCreate as mentioned in https://stackoverflow.com/a/26582301/3286489
I'm still open to more elegant answer if there's any out there.

Related

ScrimBackground from Theme.MaterialComponents is not Working

I'm trying to change the scrimBackground that show up when you opened a dialog. And I know that in the new MaterialComponents-Theme is an attribute for this case (scrimBackground). But it is currently not working for me.
On the material.io website that background is also descriped as the scrimBackground and it looks like that it is customizable from the Theme. Has anyone an idea what I'm doing wrong?
https://material.io/design/components/dialogs.html#theming
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="scrimBackground">#android:color/holo_blue_light</item>
<item name="dialogTheme">#drawable/window_scrim</item>
<item name="android:dialogTheme">#style/DialogTheme</item>
<item name="alertDialogTheme">#style/AlertDialogTheme</item>
<item name="android:alertDialogTheme">#style/AlertDialogTheme</item>
</style>
<style name="DialogTheme" parent="Theme.MaterialComponents.Dialog">
<item name="scrimBackground">#drawable/window_scrim</item>
</style>
<style name="AlertDialogTheme" parent="Theme.MaterialComponents.Dialog.Alert">
<item name="scrimBackground">#drawable/window_scrim</item>
</style>
The Material code does not appear to use this attribute even though it is defined. This issue tracks this problem. In the meantime, you may be able to change this without a Material Theme using the advise in Changing default Android fade/scrim color when calling a Dialog.

navigationbar color not changing

I use Lg Q6. Version 7.1.1. Navigation bar color not change with code below. I try to change dynamically and using theme but nothing is change. I dont understand Where Im do something wrong ?
First try programatically
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(getResources().getColor(R.color.your_awesome_color));
}
Then try style.xml
Manifest.xml
<application
android:theme="#style/AppTheme">
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowNoTitle">true</item>
<item name="android:windowLightStatusBar">true</item>
<item name="android:navigationBarColor">#color/black</item>
</style>
Every time you want to change the color of a comment or item in the entire app, it's best to put a special style into it and introduce it to the original style of the program.
For Example : if you want change background of bottom Navigation Bar you can add under lines in style.xml v21 :
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:navigationBarColor">#color/colorAccent</item>
</style>
but if you want change background color of toolbar , you should define an style for it , for example :
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
.
.
.
<item name="toolbarStyle">#style/MyStyle</item>
</style>
<style name="MyStyle" parent="Theme.AppCompat.Light">
<item name="android:background">#024dfc</item>
</style>
you can use this way for define night and day theme for your app .
I hope you enjoyed .

Editing activity theme

I'm trying to edit theme like this:
<style name="MyAppTheme" parent="#android:style/Theme.DeviceDefault.Light.DarkActionBar">
<item name="android:background">#color/colorPrimary</item>
</style>
But this colours my whole activity. Instead I want only action bar to be coloured with my custom color. How to achieve this?
Edit:
I've managed to achieve desired result by using Theme Editor and creating custom theme with it.
Try this
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
and please read this for more info
Use AppCompat theme. Specify colorPrimaryatribute since the ActionBar background color depends on it.
<style name="MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
</style>
Also you can try it like this ,
Use a parent style and declare your whatever Widget style inside the parent one.Here use android:actionBarStyle
See the example below
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">ANY_HEX_COLOR_CODE</item>
</style>
</resources>
set MyTheme in your activity
Note : ActionBar will not work for your target environment which is at API level 10

How to Switch theme for an activity

So here is the situation:
I have DogActivity and FavoritesActivity. DogActivity is just a ListView. When you click on a Dog in the list, it takes you to FavoritesActivity.
I want to have a number of themes ready to go. They don’t need to be dynamically generated. They can already exist in XML form.
Depending on which dog the user selects from the list, I want to have the FavoritesActivity shown in one of my pre-existing themes.
I hear talks about ContextWrapper, but I am not sure how to apply it. Any thoughts on how I may accomplish this?
Details:
Here is the usual single theme:
for v21/styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorControlHighlight">#color/colorAccentLight</item>
<item name="android:colorControlNormal">#color/colorAccent</item>
<item name="android:itemTextAppearance">#style/AppTheme.itemTextStyle</item>
<item name="popupMenuStyle">#style/PopupMenu.MyAppTheme</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:colorControlHighlight">#color/colorAccentLight</item>
</style>
<style name="AppTheme.itemTextStyle" parent="#android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">#color/colorPrimary</item>
</style>
</resources>
for styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Want I want to do:
Essentially I just want to change the colorPrimary, colorPrimaryDark and colorAccent on the fly and have all the styles and themes and XML layouts that use them to change. So if I can change those colors before I launch FavoritesActivity then that would solve my problems.
You can just send the dog type as an Intent extra, and then use the setTheme() method to set the appropriate Theme.
For this example, suppose you just have two Themes:
<style name="AppThemeOne" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppThemeTwo" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimaryCustom</item>
<item name="colorPrimaryDark">#color/colorPrimaryDarkCustom</item>
<item name="colorAccent">#color/colorAccentCustom</item>
</style>
Then, in DogActivity, set an Intent Extra to the Dog type the user selected from the ListView:
Intent intent = new Intent(DogActivity.this, FavoritesActivity.class);
intent.putExtra("dog_type", "terrier");
startActivity(intent);
Then, in FavoritesActivity, load the correct Theme:
#Override
protected void onCreate(Bundle savedInstanceState) {
String dogType = getIntent().getStringExtra("dog_type");
if (dogType.equals("terrier")) {
setTheme(R.style.AppThemeOne);
} else {
setTheme(R.style.AppThemeTwo);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_layout);
//.....
}
I've accomplish it quite simply on my latest project, you just have to set the values on the theme via Java. Like the following code:
public class FavoritesActivity extends AppCompatActivity { // it can be Activity too
#Override
public void onCreate(Bundle savedInstanceState) {
if( ... check condition to change theme ) {
// this will replace every value from FavoritesActivity theme by the
// the values on `other_style` theme.
getTheme().applyStyle(R.style.other_style, true);
}
// call super AFTER applying the theme
super.onCreate(savedInstanceState);
.. carry on your normal stuff
}
that's very useful because you can very easily replace just a few values and keep the rest as the original, or change everything from the original. It all depends what argument you pass to the applyTheme method.
Also is great you don't have to mock with ContextThemeWrapper. The values are there on the theme and that's it.
https://developer.android.com/reference/android/content/res/Resources.Theme.html#applyStyle(int, boolean)
You can use this.recreate() method for this.
Based on https://stackoverflow.com/a/29722976/7547681 answer.

PreferenceFragmentCompat setting the theme

Beginner coder here.
I am using PreferenceFragmentCompat to make a preference fragment that I can use with getSupportFragmentManager().
https://plus.google.com/+AndroidDevelopers/posts/9kZ3SsXdT2T
I am following the directions there, except I'm not sure how to set the preferenceTheme. My app crashes when I click on the settings menu with this exception is thrown
IllegalStateException: Must specify preferenceTheme in theme.
Where do I set the theme?
In values/styles.xml you have to add:
<item name="preferenceTheme">#style/PreferenceThemeOverlay.v14.Material</item>
use this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
</style>

Categories

Resources