Reputation: 11
I have an android app with 2 themes. And the user is able to switch themes from a settings activity elsewhere in the app. The default theme is AppThemeBlue
and when the app starts up all is well. However after user changes theme from settings activity to the AppThemeGreen
no change is made.
Here are my 2 themes in the style.xml
<resources>
<style name="AppThemeBlue" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/BlueColorPrimary</item>
<item name="colorPrimaryDark">@color/BlueColorPrimaryDark</item>
<item name="colorAccent">@color/BlueColorPrimaryDark</item>
</style>`
<style name="AppThemeGreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/GreenColorPrimary</item>
<item name="colorPrimaryDark">@color/GreenColorPrimaryDark</item>
<item name="colorAccent">@color/GreenColorPrimaryDark</item>
</style>
</resources>
my main_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppThemeBlue.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:titleTextColor="?attr/colorPrimary"
app:popupTheme="@style/AppThemeBlue.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:id="@+id/content_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary"
android:orientation="vertical" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:background="?attr/colorPrimaryDark"
android:src="@android:drawable/ic_input_add" />
</LinearLayout>
and my code in the ActivityMain
public class ActivityMain extends AppCompatActivity {
protected static FloatingActionButton mFloatingActionButton;
protected static Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mMainContentLayout = (LinearLayout) findViewById(R.id.content_bg);
mNavigationView = (NavigationView) findViewById(R.id.nav_view);
mFloatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
setTheme();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setTheme();
}
public void setTheme() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ActivityMain.this);
String color = sp.getString(getResources().getString(R.string.themes_key), null);
if (color != null) {
switch (color) {
case ConstentValues.COLORBLUE: {
setStausBar(R.color.BlueColorPrimaryDarkStatus);
setTheme(R.style.AppThemeBlue);
break;
}
case ConstentValues.COLORGREEN: {
setStausBar(R.color.GreenColorPrimaryDarkStatus);
setTheme(R.style.AppThemeGreen);
break;
}
}
} else {
setStausBar(R.color.BlueColorPrimaryDarkStatus);
setTheme(R.style.AppThemeBlue);
}
}
private void setStausBar(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = ActivityMain.this.getWindow();
window.setStatusBarColor(ActivityMain.this.getResources().getColor(color));
}
}
}
Is it becase I have ?attr/mycolor
in my main_layout.xml
? If so, please explain what is the problem with the ?attr because in the style.xml
both themes have the same item names.
Any help will be appreciated. Thanks.
Upvotes: 1
Views: 805
Reputation: 890
If your activity theme set only once then call the setTheme() method before super.onCreate() and setContentView().
If you want to change the theme at runtime, ie. through a button click, then you need to restart the activity.
Read this article for more information about changing themes at runtime.
Upvotes: 1