java


Redraw/refresh layout after changing Activity's theme/style


I have two themes defined in my styles.xml:
<style name="AppTheme" parent="android:Theme.Holo"></style>
<style name="AppLightTheme" parent="android:Theme.Holo.Light">
<item name="android:background">#FFFFFF</item>
</style>
This is how I set the theme of an activity:
protected void changeTheme(boolean dark) {
if (dark) {
setTheme(R.style.AppTheme);
} else {
setTheme(R.style.AppLightTheme);
}
}
Now after I change the theme, only the background stays the same UNTIL I open a different layout and go back. I'm using a DrawerLayout so I basically switch between layouts.
How can I re-draw it or kind of refresh it?
This is all I've tried that didn't do anything:
ViewGroup vg = findViewById (R.id.mainLayout);
vg.invalidate();
.
Intent intent = getIntent(); //this one is obvious, had to include so you don't try this unnecesarry.. code
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
.
getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
.
getWindow().getDecorView().findViewById(android.R.id.content).refreshDrawableState();
.
getWindow().getDecorView().findViewById(android.R.id.content).requestLayout();
.
findViewById(android.R.id.content).invalidate();
.
myLayout.invalidate();
Any ideas?
You can call recreate() to recreate your activity and cause all Views to be recreated with the new theme.
protected void changeTheme(boolean dark) {
if (dark) {
setTheme(R.style.AppTheme);
} else {
setTheme(R.style.AppLightTheme);
}
recreate();
}
recreate() is ok if your app is only targeting SDK level 11 and above.
When need to restart an activity, you can use following code.
Bundle temp_bundle = new Bundle();
onSaveInstanceState(temp_bundle);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("bundle", temp_bundle);
startActivity(intent);
finish();
and in onCreate...
#Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent().hasExtra("bundle") && savedInstanceState==null){
savedInstanceState = getIntent().getExtras().getBundle("bundle");
}
//add code for theme
switch(theme)
{
case LIGHT:
setTheme(R.style.LightTheme);
theme = LIGHT;
break;
case BLACK:
setTheme(R.style.BlackTheme);
theme = BLACK;
break;
default:
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code
}
I've included code for switching theme. Here, 'theme' is a string variable.

Related Links

Placement of Math.toRadians for solving quadratic and cubic equations
Usage of static keyword while making a Custom ViewPager Adapter Class
Apache flink Wikipedia edit analytics with Scala
If statement is not working with my getText().toString() [duplicate]
java-how to scroll a jpanel?
SSLEngine but without delegated task
Get Date, Month and Year from Java Date without changing timezone [duplicate]
recursive method to how to count how many different numbers in array? [java]
Error while running ice on ubuntu ( java)
Chromecast - how to stretch a video
Recursion in tree traversal
EJB timer service run every two hours but once a day not
Unable to retrieve value sent from controller to view in spring mvc
Uploading a local file to a remote hdfs with Java API but connect to localhost
After creating feature(using feature project) in Eclipse, how to zip it with a folder(in filesystem) so that I can use that folder through the plugin?
Build option is not working in Talend Job

Categories

HOME
listview
path-finding
office365api
grep
cplex
sqlite3
azure-media-services
indesign
mouse
directx
basic
reveal.js
google-apps-marketplace
jplayer
alignment
size
ups
netflix
seaborn
undefined
firefox-webextensions
cx-freeze
bootstrap-tour
one-to-many
sparse-matrix
internet-explorer-9
vision
tasklet
tinymce-4
stormpath
bcd
bosh
webtest
twitch
particles.js
mapzen
taffy
nstextview
jxcore
zendesk-app
gcsfuse
django-scheduler
minimization
eventkit
root-framework
word-vba-mac
google-feed-api
rotativa
thrust
impresspages
angstrom-linux
actionbardrawertoggle
ios4
wapiti
netmq
energy
prerequisites
content-length
iis-arr
ideamart
operation
census
c3
qcodo
internet-connection
code-access-security
uitouch
poller
gnu-smalltalk
html4
robotics-studio
distutils
angularjs-controller
browser-detection
viewswitcher
reddot
buildr
removeclass
subscript
doh
servicehost
floating
blitz++
bespin
user-friendly
webkit.net

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App