1

In my Android application I'm trying to implement changing Theme. I managed to change colors of everything I wanted and theming in runtime works well (I'm using SharedPreferences to store chosen color).

However, when I turn on app from scratch, at first the default Theme is visible (ActionBar color, to be precise) and only after a second or two, when app is loading, color is changed to the one taken from SharedPreferences.

So how can I change the default Theme? Or is there any way to change color visible while loading?

UPDATE: I'm applying theme in onCreate and it's not enough.

michalbrz
  • 3,354
  • 1
  • 30
  • 41
  • possible duplicate of [How to set the theme for the application, to avoid wrong color transitions?](http://stackoverflow.com/questions/23184333/how-to-set-the-theme-for-the-application-to-avoid-wrong-color-transitions) – Richard Le Mesurier Jun 13 '14 at 10:00

4 Answers4

2

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:

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
0

When app loads for the first time onCreate()...method is executed first. So check the condition here and apply theme.Also do the same OnResume()..also.

    @Override
protected void onCreate(Bundle savedInstanceState) {

    try {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    LinearLayout ml1 = (LinearLayout) findViewById(R.id.ml1);

    pref = new MySharedPreference();
    color = MySharedPreference.getColorCode(getApplicationContext());
    switch (color) {
    case 1:
        ml1.setBackgroundResource(R.drawable.woodenback1);
        break;
    case 2:
        ml1.setBackgroundResource(R.drawable.woodenback2);
        break;
    case 3:
        ml1.setBackgroundResource(R.drawable.woodenback3);
        break;
    case 4:
        ml1.setBackgroundResource(R.drawable.blueback);
        break;
    case 5:
        ml1.setBackgroundResource(R.drawable.leavesback);
        break;
    }

    } catch (Exception e) {
    }   
karthik
  • 71
  • 6
  • I'm currently applying theme in onCreate and it doesn't work. I updated the question. – michalbrz May 17 '14 at 11:16
  • I don't think code will help, but: in onCreate (just at the beginning of it - I tried many places and none of them work) I'm changing ActionBar color like this: `actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(getCurrentTheme(activity))));` where 'getCurrentTheme' gets color from SharedPreferences – michalbrz May 17 '14 at 11:56
0

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

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

Community
  • 1
  • 1
kablu
  • 629
  • 1
  • 7
  • 26
  • 1
    I want to change default theme in runtime and I cannot edit manifest in runtime. I'm not looking for changing theme to some specific set of values - it should be flexible. – michalbrz May 17 '14 at 11:53
0

You can do:

String m_theme = "yourThemeName";
final int themeID = activity.getResources().getIdentifier(m_theme,
                    "style", activity.getPackageName());
activity.setTheme(themeID);
Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
  • It does work for changing theme (style, to be exact) at the runtime, but it doesn't change the default style. So still, when app runs I see default colors (from theme set in manifest) and after a second, colors from the theme I set in code. – michalbrz May 25 '14 at 10:18