0

I want change app theme from code but it doesn't work .

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

    setTheme(R.style.NightMode);

    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar)

Errors

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.w7orld.animex, PID: 23421 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.w7orld.animex/com.w7orld.animex.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. at android.support.v7.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:345) at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130) at com.w7orld.animex.MainActivity.onCreate(MainActivity.java:70) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Ahmed Rashid
  • 29
  • 2
  • 11

3 Answers3

0

We set the Night-mode like this:

public class App extends Application {
 public static final String TAG = "App";

private boolean isNightModeEnabled = false;

 @Override
   public void onCreate() {
   super.onCreate();

  // We load the Night Mode state here
   SharedPreferences mPrefs =  PreferenceManager.getDefaultSharedPreferences(this);
   this.isNightModeEnabled = mPrefs.getBoolean(“NIGHT_MODE”, false);
 }

public boolean isNightModeEnabled() {
   return isNightModeEnabled;
 }

public void setIsNightModeEnabled(boolean isNightModeEnabled) {
   this.isNightModeEnabled = isNightModeEnabled;
  }
}

As this instance will be launched before everything else, you will be able to call isNightModeEnabled() whenever you want and as such, in any Activity once your app is opened.

public final class FeedActivity extends AppCompatActivity {
 private final static String TAG = “FeedActivity”;

@Override
 protected void onCreate(Bundle savedInstanceState) {
    if (MyApplication.getInstance().isNightModeEnabled()) {
       setTheme(R.style.FeedActivityThemeDark);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_feed);
 }
}

How to work with night-mode in Android is posted here

Here is some example pictures showing the difference between cycling though light and night mode:

Light and dark mode

Haroun Hajem
  • 5,223
  • 3
  • 26
  • 39
0

I found way to do that. In onCreate i put this

setTheme(Designs.getThemeNoActionBar(this));
    setContentView(R.layout.activity_main);

get the theme from shared preferences and return theme without actionbar.

  public static int getThemeNoActionBar(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("Designs", Context.MODE_PRIVATE);
        int theme = sharedPreferences.getInt("theme", R.style.AppTheme);
        if (theme == nightModeTheme)
            return R.style.NightMode_NoActionBar;
        else if (theme == theme1)
            return R.style.Theme1_NoActionBar;
        else 
             return R.style.AppTheme_NoActionBar;
    }

In style res

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="NightMode.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="Theme1.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
Ahmed Rashid
  • 29
  • 2
  • 11
-1

The first problem that's visible is the placement of these lines:

setTheme(R.style.NightMode);

setContentView(R.layout.activity_main);

You must always place the setContentView(layout) call immediately after the super.onCreate(savedInstanceState);. Because this is the code that's generating the layout and its views.

  • "You must always place the setContentView(layout) call immediately after the super.onCreate(savedInstanceState);" - no, you don't. If I want to add several fields that aren't related to the layout between onCreate and setContentView, that won't magically make the app crash. – Zoe Mar 26 '19 at 22:31
  • Strictly speaking, you can have a `setContentView`less activity without any problem. Pointless, yes, but still allowed and possible – Zoe Mar 26 '19 at 22:33