How to Use Device Default Theme for App

How to use device default theme for app?

There are currently up to 3, sometimes 4 Themes available for Android devices (.Light variations and similar not included)

Theme

Theme

the default for the earliest versions of Android up to 2.3 Gingerbread(10), including some minor style changes in those versions



Theme.Holo

Theme.Holo

introduced with Android 3.0 Honeycomb (11)



Theme.Material

Theme.Material

new in Android 5.0 Lollipop (21)



Theme.DeviceDefault

(Could be anything)

introduced with 4.0 Ice Cream Sandwich (14), a theme that can be customized by the device manufacturer. It represents the native look of the device - i.e. Holo or Material on Nexus devices (& in Android Studio's design editor), maybe something custom on other devices. In case "something custom" isn't an option, those devices must come with the stock themes. Apps that want the stock theme have to specify it though.



What is the best approach to this?

No theme + targetSdkVersion >= 14

The simplest, but not necessarily best option is to define no theme at all. Android will then select the default for you. But Android does not want to surprise your app with themes you're not expecting so it falls back to the Theme you probably had designed your app for. It does so by looking at the android:targetSdkVersion within AndroidManifest.xml (which can nowadays be set via gradle).

  • Apps that target old platforms, which had only Theme (i.e. API levels 3-10), will get only Theme.
  • Apps targeting 11-13 get Theme.Holo.
  • 14 or above will get Theme.DeviceDefault.

Since this is just for backwards compatibility, you won't get Theme.Material on your old Gingerbread phone. Therefore no theme + target 14+ = device default.

Specifying different themes

Android's resource overlay system allows to specify styles based on device API level. For example different versions of a style in res/values-v11 and res/values-v21. This is also what any newly created apps via Android Studio will setup for you.

As an example, the most basic setup for a .Light themed app looks like this:

/res/values/styles.xml is applied to every device and serves as base

<resources>
<style name="AppTheme" parent="android:Theme.Light"/>
</resources>

/res/values-v11/styles.xml is loaded on all devices that have API level 11 and above (including those that are 21 and above). But just the newest version of "AppTheme" is actually used.

<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light"/>
</resources>

/res/values-v21/styles.xml

<resources>
<style name="AppTheme" parent="android:Theme.Material.Light"/>
</resources>

Note: alternatively specifying Theme.DeviceDefault in /res/values-v14/styles.xml should be enough for having a default look but that doesn't allow to fine tune the design. Doesn't hurt to add the v14 override. DeviceDefault and Holo could be different after all.


AndroidManifest.xml is the place where the custom theme is put to use. E.g. as application wide default:

...
<application
android:theme="@style/AppTheme"
...


Links to official documentation

Select a theme based on platform version - Official doc for defining different styles via resource configurations

Holo Everywhere - blog post that explains the default mechanism and DeviceDefault / Holo theme

Using the Material Theme - material documentation

Setting default theme

You need to define activity theme in Manifest. If you want to change theme for specific activity edit activity declaration like this:

<activity
android:name=".MainActivity"
android:theme="@style/AppTheme">
</activity>

where AppTheme is your theme defined in styles.xml

If you want to set default theme for all activities, add android:theme to Application tag

<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

where AppTheme is your theme from styles.xml.

These attributes will force preview to load your theme anytime.

Note: You should use Theme.AppCompat because of compatibility.

EDIT: I didn't read your question carefully. In manifest you can use android:theme="@android:style/Theme.Material" for material theme. Or better, for compatibility, android:theme="@style/Theme.AppCompat".

How to set light theme by default in Android?

You can do this by just copying light theme Properties into Night theme file.

Go to res>values>themes.

In themes folder, you will see two files themes.xml and themes.xml(night).

Step 1:

  • Copy All themes.xml colors and paste them into themes.xml(night).

Step 2:

  • Replace the Base application theme in both files.

Replace this

<style name="<app name here>" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

to this

<style name="Theme.CurrentLocation" parent="Theme.MaterialComponents.Light">

How do you change android app theme colors with default phone settings?

Please checkout this code lab Add Light/Dark Theme

Update the dark version of your theme

Open themes.xml (night) (app > res > values > themes > themes.xml (night))

Note: This themes.xml file is different from the previous themes.xml file.

This file contains the dark theme version of the theme.

The resources in this file will be used when Dark theme on the device is on.

When you're done, your themes.xml (night) file should look like this:

    <resources xmlns:tools="http://schemas.android.com/tools">
<!-- Application theme for dark theme. -->
<style name="Theme.TipTime" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/green_light</item>
<item name="colorPrimaryVariant">@color/green</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/blue_light</item>
<item name="colorSecondaryVariant">@color/blue_light</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>

your theme structure should look like this

How to change app default theme to a different app theme?

Actually you should define your styles in res/values/styles.xml. I guess now you've got the following configuration:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light"/>
<style name="AppTheme" parent="AppBaseTheme"/>

so if you want to use Theme.Black then change AppBaseTheme parent to android:Theme.Black or you could change app style directly in manifest file like this - android:theme="@android:style/Theme.Black". You must be lacking android namespace before style tag.

You can read more about styles and themes here.

How to change default theme of application programatically?

My usual advice on this is to use a Transparent, full-screen theme in your manifest.

When you launch your activity, switch to your custom theme.

Combined with that, I always suggest an alpha-animation to fade across from the application theme to the activity theme. This prevents jarring to the user when the custom theme appears.


manifest theme defined as:

android:theme="@android:style/Theme.Translucent.NoTitleBar"

base activity onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// set your custom theme here before setting layout
super.setTheme(android.R.style.Theme_Holo_Light_DarkActionBar);

setContentView(R.layout.activity_main);

overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}

basic fade in:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />

basic fade out (not really needed, but for completeness):

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />

For further related discussion about this, check out my answer on this related question:

  • How to set the theme for the application, to avoid wrong color transitions?


Related Topics



Leave a reply



Submit