Apply a theme to an activity in Android?
You can apply a theme to any activity by including android:theme
inside <activity>
inside manifest file.
For example:
<activity android:theme="@android:style/Theme.Dialog">
<activity android:theme="@style/CustomTheme">
And if you want to set theme programatically then use setTheme()
before calling setContentView()
and super.onCreate()
method inside onCreate()
method.
Apply custom theme in Android Activity
As you probably use the AppBaseTheme for your application theme, you would also need a AppCompat theme for the overrriden activities:
Your MyTheme needs to have the parent Theme.AppCompat
or Theme.AppCompat.Light
, not the Holo theme as currenly.
Android: How to I apply a theme to all the activities of application after selecting one from preferences?
Okay so I've been playing around and found a workaround to it. I added a onBackPressed() in the SettingsActivity to bring up the MainActivity again and it seemed to work, applying the selected theme once exiting the settings activity.
@Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(SettingsActivity.this, MainActivity.class));
}
As for applying themes to all activities i just added
SharedPreferences getData = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String themeValues = getData.getString("theme_preference", "1");
if (themeValues.equals("1")) {
setTheme(R.style.Theme_Light);
}
if (themeValues.equals("2")) {
setTheme(R.style.Theme_Dark);
}
if (themeValues.equals("3")) {
setTheme(R.style.Theme_Red);
}
to every onCreate of every activity.
AndroidManifest - Set theme for activity
To Hide the Action Bar add the below code in Values/Styles
<style name="CustomActivityThemeNoTitle" parent="@android:style/Theme.Light">
<item name="android:windowNoTitle">true</item>
</style>
Then in your AndroidManifest.xml file add the below code in the required activity:
<activity android:name=".Main2Activity"
android:theme="@style/CustomActivityThemeNoTitle"></activity>
Alternatively, you can call this.requestWindowFeature(Window.FEATURE_NO_TITLE);
in the onCreate method for your activity. For example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_signup);
...
}
How to apply a theme / style in android without restarting the activity?
It seems to be that the best solution is to use recreate()
; since there is no other way to refresh a whole layout for an activity:
For everytime the user presses on a options from the list of themes
the key/value for the pressed one is saved via OnPreferenceChangeListener
in a SharedPreference
and recreate();
is then called.
themecolorList.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
recreate();
return true;
}
});
In my PreferenceActivity
s onCreate();
I call a custom made method setTheme();
which is called after recreate();
is called. The setTheme();
just looks up what is saved in SharedPreference
from the OnPreferenceChangeListener
and set the theme to corresponding value
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme();
super.onCreate(savedInstanceState);
}
Change Activity's theme programmatically
As docs say you have to call setTheme
before any view output. It seems that super.onCreate()
takes part in view
processing.
So, to switch between themes dynamically you simply need to call setTheme
before super.onCreate
like this:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
Related Topics
Difference Between /Res and /Assets Directories
Gradle - Library Duplicates in Dependencies
Why My App Is Not Showing Up on Tablets in Google Play
How to Set Dialog to Show in Full Screen
How to Strip or Escape HTML Tags in Android
How to Find Android Source Code Online
How to Add a Fragment Inside a Viewpager Using Nested Fragment (Android 4.2)
Android Drawable Images from Url
How to Launch Termux Through Another Android App
Loading Existing .HTML File with Android Webview
How to Display Album Art Using Mediastore.Audio.Albums.Album_Art
Creating an Android Trial Application That Expires After a Fixed Time Period