title: How do I force apply a dark theme on Android?
body: <p>According to the <a href="https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#force_dark" rel="nofollow noreferrer">docs</a>, I only need to set <code>android:forceDarkAllowed=true</code> in my activity manifest and inherit theme from <code>parent=&quot;Theme.MaterialComponents.DayNight&quot;</code>. I tried that, but it didn't work.</p>
  <p>Here's my manifest file:</p>
  <pre><code>&lt;application
      android:allowBackup=&quot;true&quot;
      android:icon=&quot;@mipmap/ic_launcher&quot;
      android:label=&quot;@string/app_name&quot;
      android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
      android:supportsRtl=&quot;true&quot;
      android:theme=&quot;@style/AppTheme&quot;&gt;
      &lt;activity
          android:name=&quot;.MainActivity&quot;
          android:forceDarkAllowed=&quot;true&quot;&gt;
          &lt;intent-filter&gt;
              &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
              &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  <p>And here's my <code>styles.xml</code> styles:</p>
  <pre><code>&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.Light&quot;&gt;
      ...
  &lt;/style&gt;
  
  &lt;style name=&quot;AppTheme.Dark&quot; parent=&quot;Theme.MaterialComponents.DayNight&quot;&gt;
      ...
  &lt;/style&gt;
  </code></pre>
  <p>I tried to get the theme name of the activity by using this code below:</p>
  <pre><code>resources.getResourceName(
      packageManager.getPackageInfo(packageName, PackageManager.GET_META_DATA)
          .applicationInfo.theme
  )
  </code></pre>
  <p>It shows me <code>com.example.name:style/AppTheme</code> and not <code>AppTheme.Dark</code>. How can I make it so that when I run the application, the <code>MainActivity</code> automatically sets itself to use <code>AppTheme.Dark</code> (i.e. dark mode) using <code>android:forceDarkAllowed</code>?</p>