Android Disable Dark Mode programmatically using java

<p>1.Before onCreate insert the line to set dark mode set to no.</p> <pre> @Override protected void onCreate(Bundle savedInstanceState) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }</pre> <p>2. Note if you are putting setDefaultNightMode after onCreate then onCreate will call multiple times.</p> <p>3. Another option using theme.xml</p> <p>In themes.xml change:</p> <pre> &lt;style name=&quot;Theme.MyApp&quot; parent=&quot;Theme.MaterialComponents.DayNight.DarkActionBar&quot;&gt;</pre> <p>to</p> <pre> &lt;style name=&quot;Theme.MyApp&quot; parent=&quot;Theme.MaterialComponents.Light.DarkActionBar&quot;&gt;</pre> <p>4. Or third option using</p> <ol> <li><em>Append</em>&nbsp;the following code inside your&nbsp;<code>&lt;style&gt;</code>&nbsp;tag in the&nbsp;<code>styles.xml</code>&nbsp;file:</li> </ol> <ul> <li><code>&lt;item name=&quot;android:forceDarkAllowed&quot;&gt;false&lt;/item&gt;</code></li> </ul> <ol> <li><strong>Note</strong>:&nbsp;<em>file path</em>:&nbsp;<code>android/app/src/main/res/values/styles.xml</code></li> <li>After step&nbsp;<em>1</em>&nbsp;you shoule have something like below:</li> </ol> <ul> <li><code>&lt;resources&gt; &lt;!-- Base application theme. --&gt; &lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt; ... &lt;item name=&quot;android:forceDarkAllowed&quot;&gt;false&lt;/item&gt; &lt;/style&gt; &lt;/resources&gt;</code></li> </ul> <ol> <li><strong>Maybe</strong>&nbsp;the above steps don&rsquo;t help you but don&rsquo;t worry, just like is said in the top of this post, this feature is released on&nbsp;<em>API level 29</em>, this means you should change the&nbsp;<code>compileSdkVersion</code>&nbsp;to&nbsp;<strong>29</strong>&nbsp;or higher.</li> </ol> <p><strong><a href="https://medium.com/@hasperong/android-disable-dark-mode-programmatically-using-java-1372530b9414">Website</a>&nbsp;</strong></p>