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

Can you have two print statements in the default part of a switch
Java while loop error , non syntax
Dragon book compiler input [duplicate]
USACO Training Pages Your Ride Is Here ArrayList IndexOutOfBounds Error
Java - Check 2D Array for 3-6 of the same value
Android Java: Add ImageView to TableRow without changing row size
which is the efficient way to store a tree structure data in file and read that using java code
How to ensure user selected image doesn't change after restart Android?
Passing class object to method that expects super class object
Why doesn't my service (using Apache Daemon ) work in the Services tool?
Maven: How to keep a dependency version in sync within two separated maven projects
How do I get a list of the foreign keys in a derby database
Text not showing (beginner)
Insertion sort: count swaps and comparisons
Trying to find memory leak in my java swing code
Is there any way to create JSON object with keys as upper case?

Categories

HOME
log4j
jdo
hive
mockito
google-api-php-client
alfresco
android-4.4-kitkat
razor
google-project-tango
azure-storage-tables
slurm
python-unittest
spring-kafka
midi
imacros
ups
invantive-sql
apache-metamodel
tapestry
selectedindexchanged
facebook-access-token
bootstrap-material-design
neo4j-spatial
saas
kendo-datasource
oracle-fusion-middleware
noraui
grails-3.1
pim
usbserial
phpfreechat
geopositioning
wallpaper
html5-fullscreen
android-ble
bitbucket-pipelines
abstract-class
optix
flashair
node-sass
sencha-touch-2.3
angular-resource
upstart
rails-routing
jlink
return-value
lowpass-filter
composite-key
snmptrapd
typed-lambda-calculus
gridpane
bonobo
mikroc
google-web-starter-kit
merge-conflict-resolution
python-stackless
yaws
ipconfig
wapiti
energy
freedesktop.org
unity-networking
teamcity-8.0
cannon.js
riak-cs
event-bubbling
dundas
facebook-graph-api-v2.4
file-copying
javafx-webengine
flask-cors
mesa
intentservice
quartz-graphics
batterylevel
pyinotify
telerik-scheduler
fluent-interface
brewmp
backcolor
sustainable-pace
zend-decorators
mediarss
zune
grid-system
dbisam
lzh

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App