Currently my android has black background and white foreground(text color), I want to change it to some other theme, is there any market available that I can download theme xml files that can be used in my app?
You don't have to download the light theme in order to use it. It comes with the system. You just need to indicate in your manifest that you'd like your activity to be shown with the light theme. Here is an example:
<activity android:name="NoteEditor"
android:theme="#android:style/Theme.Light"
>
Related
According to the docs, I only need to set android:forceDarkAllowed=true in my activity manifest and inherit theme from parent="Theme.MaterialComponents.DayNight". I tried that, but it didn't work.
Here's my manifest file:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:forceDarkAllowed="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And here's my styles.xml styles:
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
</style>
<style name="AppTheme.Dark" parent="Theme.MaterialComponents.DayNight">
...
</style>
I tried to get the theme name of the activity by using this code below:
resources.getResourceName(
packageManager.getPackageInfo(packageName, PackageManager.GET_META_DATA)
.applicationInfo.theme
)
It shows me com.example.name:style/AppTheme and not AppTheme.Dark. How can I make it so that when I run the application, the MainActivity automatically sets itself to use AppTheme.Dark (i.e. dark mode) using android:forceDarkAllowed?
That is not how dark mode works on Android 10.
First, Android does not magically append .Dark to your theme at any point, regardless of whether or not the user enabled dark mode. You have android:theme="#style/AppTheme". So, Android will use AppTheme all the time, and your AppTheme.Dark will not be used.
Second, the idea behind DayNight is that you use that as you base theme all the time, and Android will switch between normal and dark modes based on user request. So, if you switch AppTheme to extend DayNight, and get rid of AppTheme.Dark, you will be matching what the documentation calls for.
android:forceDarkAllowed is for cases where you are not in position to use a DayNight theme, and you want Android to try to automatically convert your UI to a dark theme. If you wanted to keep AppTheme extending Light instead of DayNight, this might work (I personally have not tried it with a MaterialComponents theme). But, even then, it will only be when the user has enabled dark mode (e.g., tapped on the notification shade tile).
For example, in this sample app, I use DayNight for my theme:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
I have different definitions for those colors in res/values/ and res/values-night/. I do not use forceDarkAllowed. And, when the user toggles night mode, my UI goes from:
to:
How can I make it so that when I run the application, the MainActivity automatically sets itself to use AppTheme.Dark (i.e. dark mode) using android:forceDarkAllowed?
You don't.
If you want a dark theme all the time, use a regular theme (not Light, not DayNight).
If your app uses a dark theme (such as Theme.Material), Force Dark will NOT be applied. Similarly, if your app's theme inherits from a DayNight theme, Force Dark will NOT be applied, due to the automatic theme switching.
Dark theme is available in Android 10 (API level 29) and higher. Make sure that your device's API level is 29 + that your target SDK in the gradle is 29.
There are two ways to apply dark theme:
DayNight - custom-able theme - you control how dark/light theme will look like
Force dark - you control the light theme, and if the user chooses dark mode the system will convert your views to dark theme at runtime.
There are settings to allow dynamically setting day or night regardless of the current system setting.
Each of the options map directly to one of the AppCompat.DayNight
modes:
Light - MODE_NIGHT_NO
Dark - MODE_NIGHT_YES
System default - MODE_NIGHT_FOLLOW_SYSTEM
To switch the theme, call AppCompatDelegate.setDefaultNightMode().
https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#change-themes
This would apply to setting a DayNight theme. If you are trying to take advantage of the Force Dark in Android 10, it applies to the .Light sub-themes. It will not be applied to any of the DayNight or inherently dark themes.
Apps must opt-in to Force Dark by setting
android:forceDarkAllowed="true" in the activity's theme. This
attribute is set on all of the system and AndroidX provided light
themes, such as Theme.Material.Light.
https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#force-dark
Below that, the documentation also specifies how to exclude certain items, either in resources or code.
Force Dark can be controlled on specific views with the
android:forceDarkAllowed layout attribute or with
setForceDarkAllowed().
Beyond that, there are ways to detect the current system light / dark setting and respond to it using the options above, but that is for another post.
It's not a great solution for a dark theme and I apologize for any overlap with the accepted answer, but I do not agree with claiming something isn't possible because it isn't preferred.
I'm wondering why, every time I create a new Android project, Eclipse opens a main activity with a white background, when in previous versions, it was black.
Is there any way for me to change that back to black as a default color for future activities?.
They changed the default theme that eclipse uses in your resources to the light theme. Change your resource files (delete the stuff eclipse added that you don't use) and it will go back to black.
So if you want to reskin your whole application to black, you can use following line in your manifest file.
Write to Application tag attribute theme.
<Application
android:theme="#android:style/Theme.Black" //old targeted application
OR
android:theme="#android:style/Theme.Holo" //for new devices
... >
Now all activities in this application have black theme.
You can "override" this by using theme attribute in Activity tag.
<Activity
android:theme="some theme"
...>
When an app does not set the theme itself, it will call the system default theme.
Now the android default theme is black background and white text. I want to set the default theme due to my wish.
Who knows where can set the default theme in android project?
The android manifest.xml has the attribut android:theme="resource or theme". This attribut sets it to holo dark or light or your own declared theme. To use your own layout you msut declare and build your own layout in the res/values folder.
Here's the documentation about it: http://developer.android.com/guide/topics/manifest/application-element.html
In menifest.xml file you can set default theme like
<application
android:theme="#android:style/Theme.Black"
/>
I have tried both Theme.DeviceDefault and Theme.Black in my styles.xml, and both make my 2.1 emulator become black correctly, but on the 4.1 emulator it stays white...am I doing something wrong?
<resources>
<style name="AppTheme" parent="android:Theme.Black" />
</resources>
PS: I presume the drop-down for "Theme" at the top of the Graphical Layout is just showing me what it would look like, it's not some kind of setting that's going somewhere (for the app)?
The Android system always chooses the most special values folder that fits best to your device.
As example if you have a folder values and another folder values-v14 the Android system takes resources from the latter if the device is v14 or newer.
Please refer to the Android documentation for more information.
1.create new folder under res -> values-v13
2.create file style there and add this style :
<style name="AppTheme" parent="android:style/Theme.Holo.Light">
in your manifast use this style for your application like this
<application
android:name=".MainApp"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
that way on ICS version you use the Holo.light theme and pre ICS use the default you already created
Setting it via styles doesn't actually work, but if you set it explicitly for application it works.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black" >
By default 4.0 and 4.1 give us white background but you can change it to black I face the same problem some days ago, if your project theme is set to be black then probably you change the Title while creating project to "Main" by default its "MainActivity" but if you change it to "Main" it will give you black backgrount/
I don't know my answer is relevant to your question or not but i think you talking about this if i am not wrong.
I'm quite new to android dev and would like to know, how can I choose the current theme of the phone. I'm developing on the Galaxy Tab and the current theme is a nice white one (if I go into the settings menu for example). But the default theme in the sdk seems to be Android.Black.
I would prefer not to hardcode any choice and let the app use whichever one is chosen on the phone/tab...
I'm not sure exactly what your question is aimed at, so I'll answer in two parts:
How do I let the app choose the current theme on Android?
The theme is set automatically (by default) to be whatever system theme the current device/user has selected. If no style or nested theme attributes are defined, then your app will be set to reflect the system style. For instance, if the default font color for the theme is #AFAFAF, the default font color for your app will also be #AFAFAF, unless otherwise specified by a style or theme resource.
If I don't like the current theme, how do I set my own theme?
You mentioned that you liked the nice white theme on the Galaxy tab. You are correct in that different versions of android use different system themes including fonts and backgrounds.
To get the current theme (to decide if it's the one you want), simply use the getApplication().getTheme(). If it's the theme you want, you don't have to do anything. If you want to apply logic and programatically set the theme, use the getApplication().setTheme([theme]) method to change it.
You can also specify both application-wide themes and themes for individual activities and you AndroidManifest.xml file as follows:
//for themes defined in res/values/style.xml
<application android:theme="#style/myApplicationTheme">
<activity android:theme="#style/myApplicationTheme">
//for system themes
<application android:theme="#android:style/Theme.Light">
<activity android:theme="#android:style/Theme.Light">
And, as always, to individual view elements.
If you're looking to implement a specific system theme, be sure to check out the complete theme specifications. These will list all system themes as well as all defined style elements for each of the themes so you can use the desired theme or make your own variant.
If you're particularly fond of the light system theme, it can be set for your application using
<application android:theme="#android:style/Theme.Light">