1
votes

How to change theme inside android app?I have activity with settings where I can select Light or Dark theme.

Settings Activity enter image description here Can I use something like this:

public class OTGEnabler extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otgenabler);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        String theme = sharedPrefs.getString("Choose Theme",null);
        if(theme != null && theme.equals("Light"))
        {
            setTheme(R.style.AppTheme);
        }
        else
        {
            setTheme(R.style.AppTheme_AppBarOverlay);
        }
    }
}

Here is error log:

02-04 07:16:40.023 23924-23924/com.example.pavle.usbotgenabler E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pavle.usbotgenabler, PID: 23924 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pavle.usbotgenabler/com.example.pavle.usbotgenabler.otg_enaber.OTGEnabler}: 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:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 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.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:203) at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:133) at com.example.pavle.usbotgenabler.otg_enaber.OTGEnabler.onCreate(OTGEnabler.java:80) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2
I tried to run app again and it starts but when I select light or dark from list preference menu nothing happens !!!pavlenis1906

2 Answers

0
votes

you can use setTheme(..) inside onCreate before calling setContentView(...)and super.oncreate() and it should work, Not after them!


Extra help

Add an edit text in your view

Theme One

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorAccent">@color/colorAccent</item>
</style>

Theme Two

<style name="AppThemeTwo" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorAccent">@color/blue</item>
</style>

If you add these two you will notice difference in your EditText like cursor color cursor pointer color etc..

0
votes

In androidManifest.xml file change theme of settings activity to new theme which you want for settings activity.

this new theme must be defined in values->style.xml

If not present add to settings activity in androidManifest.xml file following xml tag android:theme