Alert Dialog Box Theme Issue - android

I am getting an error when trying to set theme for alertDialogbox. I am betting white box behind the alertdialogbox. Any idea how i can get rid of it? Here is my code for styles.xml.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowSoftInputMode">stateUnchanged|adjustPan</item>
<item name="android:alertDialogTheme">#style/alertTheme</item>
</style>
<style name="alertTheme" parent="#android:style/Theme.Holo.Light.Dialog">
</style>

In your values res folder, your alertDialogTheme should have the following property:
<!-- In API <21, this gives a funky background. Set to translucent -->
<item name="android:windowBackground">#color/translucent</item>
where #color/translucent is defined in colors.xml as #00000000, which gives opacity of 0.
In your values-v21 res folder, your alertDialogTheme should have the following property:
<!-- In API 21+, this is the dialog background color -->
<item name="android:windowBackground">#color/someColor</item>
This does nothing to alertDialogs in any API as far as I can tell:
<!--<item name="android:colorBackground">...</item>-->
I've only tested this on the Light AppCompat theme.

You do not provide any code, but in any way you should use AppCompat.
Your dialog style should inherit from AppCompat.Dialog
<style name="AlertDialog" parent="Theme.AppCompat.Light.Dialog"/>
and you should only use the AppCompat AlertDialog. Additionally you then pass in your style to the constructor.
new android.support.v7.app.AlertDialog.Builder(context, R.style.AlertDialog).show();
Without AppCompat
If you chose to try without AppCompat, be sure to test on multiple devices. You will need to provide different themes for phones before and after API 21 and handling the android:windowBackground yourself by either setting or hiding it.

Try adding to your alertTheme
<item name="android:background">#null</item>

Here goes a little bit improved answer of samGbos:
values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:alertDialogTheme">#style/SplashScreenDialog</item>
<item name="alertDialogTheme">#style/SplashScreenDialog</item>
</style>
...
<style name="SplashScreenDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
values-v21/styles.xml
...
<style name="SplashScreenDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
</style>

Related

Material components not using color accent declared in theme

My main app theme parent is MaterialComponents theme. I changed the colors, but material components (MaterialButton, InputLayout, EditText) are not using my accent color (they are using some blue color, my declared Accent color is Teal)
What is the issue? How is the best way to deal with theming Material Components?
My main theme:
<style name="AppTheme" parent="Theme.MaterialComponents">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Edit:
Changing theme to Theme.MaterialComponents.Bridge is not solving that problem.
Maybe you should try to use colorSecondary instead of colorAccent:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Original AppCompat attributes. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorSecondary">#color/colorAccent</item>
</style>
This works for me
The issue in my case was a different styles.xml declared for the specific API. I made a mistake in it, and when testing, that other values-v21/styles.xml was applied before styles.xml.
So long story short, check if the mistake is not present in other variants. I declared that variant wrong.
For those, trying to use a theme that inherits from Theme.MaterialComponents.* please also keep in mind not to use a regular Button class but com.google.android.material.button.MaterialButton instead.
Instead of:
<Button
(...)
style="#style/Widget.MaterialComponents.Button" />
Make sure to use:
<com.google.android.material.button.MaterialButton
(...)
style="#style/Widget.MaterialComponents.Button" />
Been there. Nothing to be ashamed of. :)
Inherit your app theme from Theme.AppCompat like:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
</style>
Note that dark or light theme is upon you but you need to extend Theme.AppCompat.
Try changing your styles.xml like this :
<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>
</style>
If this works, let me know
Edit : Try following this link for setting up material components https://medium.com/#pedromassango/android-material-design-components-setup-google-i-o18-part-1-8894f315b5e

Is this the correct way to have different values/styles for different versions

Android Studio 2.1 preview 3
This is just a question, as I am confused as I have seen many alternatives in doing this.
I have created a new android project and my Activity extends AppCompatActivity
public class MainActivity extends AppCompatActivity {
I want to have the transparent statusbar on devices running 21 and over.
So in my values/styles I have the following
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
And in my values-21/styles I have the following
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Make the statusbar transparent -->
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
My Manifest I select the theme
android:theme="#style/AppTheme"
Just some questions
Is this the correct way, or is there any better way to do this?
Would values-21/styles inherit all the colors in values/styles so I would have to repeat this?
It's the right way. May I suggest you to organize your style better?
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="CommonTheme">
</style>
<style name="CommonTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
values-v21/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="CommonTheme">
<!-- All customization of the theme for this version -->
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
So you don't need to repeat the common values of the style for every api level.
I will try to answer it giving some references
Maintaining Compatibility
To avoid duplication of code, define your styles inside res/values/,
modify the styles in res/values-v21/ for the new APIs, and use style
inheritance, defining base styles in res/values/ and inheriting from
those in res/values-v21/
So you should try to avoid code duplication in your style.xml at different folders res/values/ and res/values-v21/ by using style inheritance.
Style Inheritence
If you want to inherit from styles that you've defined yourself, you
do not have to use the parent attribute. Instead, just prefix the name
of the style you want to inherit to the name of your new style,
separated by a period.
If you want to inherit a style that you've defined yourself you can skip adding a parent attribute and inherit from it using a dot or period notation.
With this, you can define a base theme BaseTheme in res/values/ with different colors and inherit from it as BaseTheme.StyledStatusBar without specifying a parent attribute.
<resources>
<style name="BaseTheme.StyledStatusBar"></style>
<!-- Base application theme. -->
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
In values-21/, add item android:windowTranslucentStatus to BaseTheme.StyledStatusBar
<resources>
<style name="BaseTheme.StyledStatusBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
In manifest, select the theme
android:theme="#style/BaseTheme.StyledStatusBar"
1) Is this the correct way, or is there any better way to do this?
Yes. That's the correct/recommended way of having different values for different API versions.
2) Would values21/styles inherit all the colors in values/styles so I would have to repeat this?
I'm not sure I fully follow this question. Both styles you showed will inherit from Theme.AppCompat.Light.NoActionBar so your colors should be declared again, but I'll present two better alternatives:
Alternative 1, a little bit better:
Use a BaseTheme that is common for both. To view the code for it, please check #mimmo-grottoli answer.
Alternative 2, much better:
If the only different on the two themes is the android:windowTranslucentStatus that was introduced in KitKat (API level 19), you can put it all in the same theme in values/styles, like the following:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Make the statusbar transparent -->
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
The Android Framework ignores XML parameters that it does not recognize. That means that on JellyBean or ICS, the device will ignore windowTranslucentStatus while correctly applying the colors and in KitKat and up, it will correctly apply windowTranslucentStatus.
That tricky is valid for all XML in Android (even layouts), the IDE might give you warnings about API level, but in XML they're always safe to use.
Different folders of values/styles are made to give a unique style when your app is running on a specific version of Android.
So yes you are right when saying that the newer version inherits from the older one. When adding items in your latest version of styles you keep the latest version up to date to the latest APIs.
To conclude, your way is the very commun way, it's an organized and a clean way to keep your app updated.
Is this the correct way, or is there any better way to do this?
Yes. That's the right way of having different values for different API versions.
Would values-21/styles inherit all the colors in values/styles so I would have to repeat this?
Yes
technically <item name="android:windowTranslucentStatus">true</item> won't give you fully transparent statusbar.
If you want it to by fully transparent, you can use <item name="android:statusBarColor">#android:color/transparent</item>

Can't understand how to style Android app for pre-Lollipop devices using Theme.AppCompat

My brain is about to blow on Android's themes and styles. Somebody, help me, please!
I'm trying to create a theme for my app to provide as similar look for pre- and post-Lollipop devices as possible. So, I've inherited my theme from Theme.AppCompat.NoActionBar and my activities from AppCompatActivity ans set up colorPrimary, colorPrimaryDark and colorAccent. All that went well. The problems started when I tried to style background and text color. I've set android:textColorPrimary and android:textColorSecondary and got what I wanted on Lollipop device, but not on KitKat, so I've added textColorPrimary and textColorSecondary attributes to theme and after that Android Studio said that it can't build the app because these attributes can't be found. Same happened when I've tried to add colorBackground. I've tried to google it but wasn't able to found something useful about what attributes are provided by which theme and what should I use in my situation.
You must have to create two values folder one for lollipop values-v21 and simple values for pre-lollipop
See this style for pre-lollipop devices just paste it to values/styles.xml
Styles.xml
<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>
<style name="MyMaterialTheme.TransparentActivity">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
</resources>
see official documentation on android blog
If you want to style the app for different apis, you have to make a folder name values-v(sdk version) for e.g values-v21 for lollipop, values-v19 for kitkat,... but in this case I think you only need to create values-v21.
inside that folder, create an xml file style.xml and based on the api to use the appropriate attributes.

Android actionbar background around title

Android Studio 1.1.0. SDK 22 Installed&Compiled, SDK 17 Target (I suppose it's not relevant, because issue occurs also on preview in IDE).
I started playing with Holo theme action bar styling and ran into an issue, involving background color. This is my styles.xml:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:background">#ff0d3200</item>
<item name="android:actionBarStyle">#style/BarTheme</item>
</style>
<style name="BarTheme" parent="android:Widget.Holo.ActionBar">
<item name="android:background">#443322</item>
</style>
</resources>
And this is output. I want app title's background color to be the same as action bar's. As I've said, it's the same on the preview, so I don't think it's because my phone(SDK17) or emulator. Overflow button behaves the same way, there just isn't any in this case.
I tried setting android:titleTextStyle in BarTheme to this style:
<style name="TitleTheme" parent="android:TextAppearance.Holo">
<item name="android:background">#443322</item>
</style>
But it didn't change a thing. What do I do to overcome this?
try ,
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">value</item>
<item name="android:titleTextStyle">#style/TitleColor</item>
</style>
<style name="TitleColor" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">value</item>
</style>
Due to David's input I solved my problem myself. I had to set windowBackground, instead of background in AppTheme. I'm not sure what is the exact difference, but it works properly. Remember not to change attribute in your BarTheme, because it won't work with windowBackground. Therefore correct styles.xml file is:
<resources>
<color name="bcColor">#ff0d3200</color>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:windowBackground">#color/bcColor</item>
<item name="android:actionBarStyle">#style/BarTheme</item>
</style>
<style name="BarTheme" parent="android:Widget.Holo.ActionBar">
<item name="android:background">#443322</item>
</style>
</resources>
Note that color has to be defined as resource in this case and cannot be hardcoded. The clean way to do this is create colors.xml in values folder and define all colors there.

Android custom theme

Hello I'm making a custom theme for my app. I want to have a custom ActionBar and background for the rest of the app (below the ActionBar). I'm wondering how to do the background. Right now I have:
<style name="MyTheme" parent="android:Theme.Holo">
<item name="android:actionBarStyle">#style/dActionBar</item>
</style>
<style name="dActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#4C721D</item>
<item name="android:textColor">#FFFFFF</item>
</style>
Would I just have to do <item name-"android:background"></item> in MyTheme or would I have to create another style? After I create this do I need to put it in the res/styles folder? Also what are the API restrictions on this, if any?
Don't forget to apply your theme to your application in your AndroidManifest.xml.
<application android:theme="#style/CustomActivityTheme" ... />
You can find all the information about styling the Actionbar here.
To style your ActionBar with a nice Holo-look, try this website
Hope this helps!
Try to use below code in your Style.xml file
<resources>
<style name="Theme.Sample" parent="Theme.AppCompat">
<item name="android:colorBackground">#color/primary_material_dark</item>
<item name="actionBarTheme">#color/colorGray</item>
</style>
</resources>
Just need to use android:colorBackground as a name of item to define the background color like as below:
<item name="android:colorBackground">#color/primary_material_dark</item>
Hope you get your answer,Thanks.

Categories

Resources