I am learning how to implement Dark Theme to application.
After reading documents from this, below are the requirements that I understand
make sure "Style" parent = <style name="AppTheme" parent="Theme.AppCompat.DayNight">
create a new colors for Dark Theme in side colors.xml file
if using "Switch" toggle, use AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); and AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); inside onCheckedChanged listener
Based on testing, calling AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); will trigger recreate() to recreate the activity, result it will change the theme as expected, also the BLACK blinking thingy will appear.
After reading through this, it stated that I need to declare setTheme method before setContentView(R.layout.activity_main); so it will solve the BLACK thingy issue.
The Android Dev document does not mentioned anything about setTheme is required in OnCreate method after AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); is used
If using only AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); able to switch desire theme, what's the point of using setTheme again in onCreate method?
Is the BLACK blinking thing a bug? or is there a proper way to solve it?
~~~~~~~~~~~~~~~~~~~~~~~~~~~ UPDATE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Maybe my questions is confusing as well. To make it short, just wanted to know that why "setTheme" is needed to declared in `onCreate` when `AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)` already restarted the acitivy?
I'm so confused and spent more than a week to solve this, guidance or tips are much appreciated
The AppCompactDelegate some times recreates the activity that causes black blinking
Check This It Changes theme without black blinking
Create two themes or use default dark theme xml like this :
<!-- Dark theme -->
<style name="DarkTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/darkColorPrimary</item>
<item name="colorPrimaryDark">#color/darkColorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="fontFamily">#font/roboto</item>
<item name="listBackground">#color/darkListBackground</item>
<item name="cardBackground">#color/darkCardBackground</item>
<item name="textColor">#color/darkCardTitle</item>
<item name="actionBarBackgroundColor">#color/darkColorPrimary</item>
<item name="actionBarTextColor">#color/darkCardTitle</item>
<item name="dialogTextColor">#color/darkDialogTitle</item>
<item name="dialogDateColor">#color/dialogDateColor</item>
</style>
<!-- Light theme -->
<style name="LightTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/lightColorPrimary</item>
<item name="colorPrimaryDark">#color/lightColorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="fontFamily">#font/roboto</item>
<item name="listBackground">#color/lightListBackground</item>
<item name="cardBackground">#color/lightCardBackground</item>
<item name="textColor">#color/lightCardTitle</item>
<item name="actionBarBackgroundColor">#color/lightColorPrimary</item>
<item name="dialogTextColor">#color/lightDialogTitle</item>
<item name="dialogDateColor">#color/dialogDateColor</item>
</style>
Use Shared Prefrence to save themes type
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("NightMode", false);
editor.apply();
Change App theme on Activity on create method using shared prefrence
if (!darktheme) {
setTheme(R.style.DarkTheme);
} else {
setTheme(R.style.LightTheme);
}
For Brief Information get that project :
https://github.com/yeshasmp/ToDo/tree/v1.0
Related
In Android, is there any way to give an ActionMode the same appearance as the normal Toolbar? Currently my ActionMode looks different to the Toolbar in that the background is slightly brighter (at least using Theme.AppCompat.NoActionBar), the MenuItems are fully black/white (depending on the theme) instead of colorControlNormal (which I set them to) and most annoyingly, there is an underline under the ActionMode that I can't seem to get rid of. Instead, I'm trying to make the ActionMode indistinguishable from the Toolbar.
The Toolbar (also what the ActionMode is meant to look like):
What the ActionMode currently looks like:
Any help would be highly appreciated!
For changing the background color of ActionMode & Remove the bottom line. You can use actionModeStyle where you can set background to your desired color.
For changing the icon color you can set colorControlNormal inside the actionBarTheme.
So your theme would look like this:
<style name="Theme.AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!--add this-->
<item name="actionModeStyle">#style/ActionModeStyle</item>
<item name="actionBarTheme">#style/MyActionBarTheme</item>
</style>
<style name="MyActionBarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
//replace with your own desired color
<item name="colorControlNormal">#a00</item>
</style>
<style name="ActionModeStyle" parent="#style/Widget.AppCompat.ActionMode">
//replace with your own desired color
<item name="background">#color/colorPrimary</item>
<item name="titleTextStyle">#style/ActionModeTitleTextStyle</item>
</style>
<style name="ActionModeTitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionMode.Title">
//replace with your own desired color
<item name="android:textColor">#a00</item>
</style>
I have a Switch declared in my XML which is a part of an Activity using a specific theme:
<Switch
android:id="#+id/sw_TakeTime_SelectRange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchPadding="#dimen/dim_ButtonPadding"
android:showText="false"
android:text="#string/sw_SelectDateRange_Switch_Range"
android:textColor="#color/Mercedes_BLACK"
android:textSize="18sp"
android:theme="#style/SwitchCompatStyle"
android:switchMinWidth="20dp"
app:layout_constraintBottom_toBottomOf="#+id/bt_SelectDateRange_Confirm"
app:layout_constraintStart_toStartOf="#+id/cv_SelectDateRange"
app:layout_constraintTop_toTopOf="#+id/bt_SelectDateRange_Confirm"/>
but the switch animation is not shown...
This is the Preview:
This is the running application...
This is what I edited in SwitchCompatStyle
<style name="SwitchCompatStyle">
<item name="colorControlActivated">#color/Mercedes_RED</item>
</style>
I figured out, that the real problem is the Theme of my Activity, which is set to
<activity android:name=".activity_SelectDateRange"
android:theme="#style/Theme.Transparent">
This transparent theme includes:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name = "android:windowCloseOnTouchOutside">true</item>
</style>
If I use this in combination with the switch the animation is not working anymore.
I tried to delete setting by setting in the theme, to detect which one is the problem, but I can't figure it out. Maybe it is a combination of settings.
This indeed seems to be a bug. I have the same problem. If I click on the Switch itself it animates fine but if I change the switch state while the NavigationDrawer is open there is no animation.
See this for the Android Issue Tracker entry: SwitchCompat.setChecked does not animate inside RecyclerView
I have defined a relativeLayout with a recyclerView and an imageView. I want the imageView to draw behind the status bar. But for some reason
android:fitsSystemWindow = true
doesn't work.
How can I achieve it?
Set your full-screen theme like following
<style name="Theme.MyApp.FullScreen" parent="Theme.MyApp">
<item name="android:statusBarColor" tools:targetApi="lollipop">#android:color/transparent</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
<item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="lollipop">true</item>
</style>
Don't add android:fitsSystemWindows="true" in your layout file
Also, no need to add android:windowSoftInputMode="adjustPan" in Manifest file
To draw behind status bar, You have to set your status bar have transparent background.
So, you need to set this with adding below proprieties in your theme:
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
And in your .xml you have already set below property which is useful:
android:fitsSystemWindows="true"
I have been wrestling with trying to get my PreferenceFragment to have the same Material-based theme and styling (via AppCompat) as the rest of my application. The PreferenceFragment that I am using to manage all of my application settings is shown below:
As you can see from the screenshot above, I was able to customize the PreferenceFragment by using colorAccent, colorPrimary, and a few other attributes. My theme for the PreferenceFragment is as follows:
<style
name="settingsTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/blue_grey_500</item>
<item name="colorAccent">#color/blue_grey_500</item>
<item name="android:textColor">#color/black_text_alt</item>
<item name="android:textColorSecondary">#color/black_secondary_text</item>
</style>
However, despite my best efforts, I am unable to apply themes to the individual elements within my PreferenceFragment, such as ListPreference and EditTextPreference; all of the preferences still retain the standard AppCompat theme:
I found an 2 year old post that discusses this issue, albeit with no real solution: How to apply theme to <PreferenceScreen> elements of a <PreferenceCategory>
I am wondering if anyone has been able to successfully apply themes to the preferences since AppComat V21 has been released. If not, are there any viable workarounds that can be used to apply custom themes to individual preferences?
UPDATE: I made a library to battle the issue (uses AppCompat r22): https://github.com/consp1racy/android-support-preference
The ListPreference extends DialogPreference which uses this piece of code to create the dialog:
mBuilder = new AlertDialog.Builder(context)
.setTitle(mDialogTitle)
.setIcon(mDialogIcon)
.setPositiveButton(mPositiveButtonText, this)
.setNegativeButton(mNegativeButtonText, this);
As you can see the AlertDialog.Builder constructor is not supplied with the second optional int theme parameter. That means the dialog will be themed by whatever your activity's theme has in its android:alertDialogTheme attribute.
Now you have to create a custom theme for your dialog which derives from Theme.AppCompat.Dialog like so:
<style name="Theme.YourApp.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="colorAccent">#color/accent_yourapp</item>
<item name="colorPrimary">#color/primary_yourapp</item>
<item name="colorPrimaryDark">#color/primary_dark_yourapp</item>
</style>
Problem 1: The above solution will not work for RingtonePreference because it does not extend ListPreference but calls a ringtone chooser intent, so it's always themed according to system. Check out this answer:
RingtonePreference Theme So we can mark this as solved.
Problem 2: The AppCompat dialogs lack title. And so far I haven't found a way to fix this. True that I am not looking hard enough. Let's ignore the title absence as a minor issue.
Problem 3: The radio button drawables are not mutated so the graphics are inconsistent between passive and active (colored) state - all are colored (not just the one you!re pressing) or all are grey. Now this is really annoying
Problems 2 & 3 forced me to take another route - my dialog theme looks like this on API 14+
<style name="Theme.MyApp.Dialog.Alert" parent="android:Theme.DeviceDefault.Light.Dialog.MinWidth">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
</style>
and like this on API 21+
<style name="Theme.YourApp.Dialog.Alert" parent="android:Theme.DeviceDefault.Light.Dialog.MinWidth">
<item name="android:colorPrimary">#color/primary_yourapp</item>
<item name="android:colorPrimaryDark">#color/primary_dark_yourapp</item>
<item name="android:colorAccent">#color/accent_yourapp</item>
</style>
These values have been experimentally gained by crawling through the platform's source files and well tested.
The point is that the only reliable solution seems to be using the device default dialog theme. The only choice before Lollipop is a light or dark variant. On Lollipop this will work as intended and requested.
EDIT: Since appcompat-v7-r21.1.0 you can use AppCompatDialog which is material themed variant of native AlertDialog.
You can use provided AlertDialog.Builder (not to be confused with its native counterpart) for creating its instances.
DialogPreference uses android.app.AlertDialog.Builder directly so it would be impossible to switch the displayed dialog to the material design one for all API levels (which requires using android.support.v7.widget.AlertDialog.Builder).
I believe the only option for now is to extend and override dialog creation logic in DialogPreference or its subclasses (and use reflection to work around private accessors if required). Check out this reddit thread and an example AppCompatListPreference made by the author of that thread.
Taking cues from the answers already given I set in the manifest a custom theme for Settings activity because it is necessary to remove the border background around the Dialog that appear, using a transparent background this only for API <21:
AndroidManifest.xml
<application
android:name=".MyApplication"
...
android:theme="#style/AppTheme">
...
<activity
android:name=".ui.SettingsActivity"
...
android:theme="#style/AppTheme.Settings">
</activity>
</application>
values/style.xml
<!-- Application theme -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:alertDialogTheme">#style/AppTheme.AlertDialog</item>
</style>
<!-- Must be different because of transparent background -->
<style name="AppTheme.Settings" parent="AppTheme.Base">
<item name="android:alertDialogTheme">#style/AppTheme.SettingsAlertDialog</item>
</style>
<style name="AppTheme.AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowContentOverlay">#null</item>
<item name="colorAccent">#color/accent_light</item>
<item name="colorControlActivated">#color/accent_light</item>
<item name="colorControlHighlight">#color/primary_highlight</item>
</style>
<style name="AppTheme.SettingsAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="colorAccent">#color/accent_light</item>
<item name="colorControlActivated">#color/accent_light</item>
<item name="colorControlHighlight">#color/primary_highlight</item>
</style>
finally for values-v21/style.xml the normal dialog, if you don't put this in v21 dialogs will be transparent.
<style name="AppTheme.Settings" parent="AppTheme.Base">
<item name="android:alertDialogTheme">#style/AppTheme.AlertDialog</item>
</style>
Actually I've the problem to set the colors directly from the Theme attributes: ?android:colorAccent instead of my fixed color #color/accent_light.
You should check out this Gist about how different components use the theme color attributes. You might need to add the following attributes to your theme to get the effect you want:
<item name="colorControlNormal">#color/x_color</item>
<item name="colorControlActivated">#color/y_color</item>
<item name="colorControlHighlight">#color/z_color</item>
I work with android and I used android-styled-dialogs project for custom dialogs
I have custom theme
<style name="DialogStyleDark.Custom">
<item name="dialogBackground">#color/tt_gray_2</item>
<item name="titleTextColor">#color/tt_light_blue</item>
<item name="titleSeparatorColor">#color/sdl_button_separator_dark</item>
<item name="messageTextColor">#color/sdl_message_text_dark</item>
<item name="buttonTextColor">#color/sdl_button_text_dark</item>
<item name="buttonSeparatorColor">#color/sdl_button_separator_dark</item>
<item name="buttonBackgroundColorNormal">#color/sdl_button_normal_dark</item>
<item name="buttonBackgroundColorPressed">#color/tt_gray_1</item>
<item name="buttonBackgroundColorFocused">#color/tt_gray_1</item>
</style>
dialog seems like this
I want to it has shadow as android native dialogs
Can anybody help me?
Since I did much research in how to customize and recolor the default holo dialogs I can tell you that this is done with drawables. So just create some nice png files with shadows and use them as background.
Here you can find the orginal xml file which is used by alert holo themed dialogs.