Note that there are some explanatory texts on larger screens.

plurals
  1. POTheme change doesn't work on <4.0 as it should
    primarykey
    data
    text
    <p>I have some difficulties with setting up a "theme switcher" programmatically. I would like to switch themes from app (between White (Theme.Light.NoTitleBar) and Dark (Theme.Black.NoTitleBar)) and what I do is: I set a SharedPreference:</p> <pre><code>final String PREFS_NAME = "MyPrefsFile"; final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); final SharedPreferences.Editor editor = settings.edit(); </code></pre> <p>and than I have a two buttons to switch themes (second one is almost identical)</p> <pre><code>Button ThemeWhite = (Button) findViewById(R.id.ThemeWhite); ThemeWhite.setOnClickListener(new OnClickListener() { public void onClick(View v) { editor.putBoolean("Theme", false); editor.commit(); System.exit(2); } }); </code></pre> <p>and in begging of each activity I check SharedPreference</p> <pre><code>boolean theme = settings.getBoolean("Theme", false); if(theme){ this.setTheme(R.style.Theme_NoBarBlack); } else{ this.setTheme(R.style.Theme_NoBar); } setContentView(R.layout.aplikacja); </code></pre> <p>I define themes in file <em>styles.xml</em> in folder values:</p> <pre><code>&lt;resources&gt; &lt;style name="Theme.NoBar" parent="@android:style/Theme.Light.NoTitleBar" /&gt; &lt;style name="Theme.NoBarBlack" parent="@android:style/Theme.NoTitleBar" /&gt; </code></pre> <p></p> <p>in values-v11:</p> <pre><code>&lt;resources&gt; &lt;style name="Theme.NoBar" parent="@android:style/Theme.Holo.Light.NoActionBar" /&gt; &lt;style name="Theme.NoBarBlack" parent="@android:style/Theme.Holo.NoActionBar" /&gt; </code></pre> <p></p> <p>in values-v14:</p> <pre><code>&lt;resources&gt; &lt;style name="Theme.NoBar" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar" /&gt; &lt;style name="Theme.NoBarBlack" parent="@android:style/Theme.DeviceDefault.NoActionBar" /&gt; </code></pre> <p></p> <p>manifest file:</p> <pre><code> &lt;application ... android:theme="@style/Theme.NoBar" &gt; </code></pre> <p>Everything is working excellent on android >4.0 <strong>but when I use 2.2 it doesn't change theme</strong> - just font is getting white as it should be but there is no dark background.</p> <p>I tried checking if it at least works and changed Theme.NoBarBlack in values (for android &lt;3.0) and its value the same as Theme.NoBar and then when I pressed button font wasn't changed -as it should do.</p> <p>EDIT (<strong>PROBLEM FOUND</strong>): So after trying it turn out that on Android 2.2 Manifest file set Background and rest, and programmatically changing theme affect only text color. any idea why that happens?</p> <p><strong>ANSWER (as I don't have 10 reputation):</strong> On android &lt;3.0 it matters if </p> <pre><code>super.onCreate(savedInstanceState); </code></pre> <p>Is before setting up theme or not.</p> <p>So it should be:</p> <pre><code>public void onCreate(Bundle savedInstanceState) { setTheme(R.style.Theme_NoBar); super.onCreate(savedInstanceState); setContentView(R.layout.lista); } </code></pre> <p>Sorry guys for making problem out of nothing but as it was working on >3.0 I was confused. Spend half of day on it but working. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload