android dynamically change style at runtime
Changing the style after creating the view is not supported .. so what you can do is:
- create a new android xml file of type values
- add new theme
- add your elements to that theme and their values and save the file
Now, when you are dynamically creating the new view you call the constructor that will allow to define a defStyle. Then, you point to the style ID you have just created by pointing to R."the XML file name"."your style ID"
myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
android change style dynamically
I am seeing contradicting answers to this question. This says yes, but this says no. I looked at the android docs for the View and can not find any setStyle method.
change style attribute at runtime
You may want to have a look at the same question: android dynamically change style at runtime
As it seems to me, the matter is that generic style attributes can force screen re-layouting, so there is no API to change styles at runtime - you need to create screen anew (the same approach is dictated for landscape/portrait screen orientation changes, where Activity is re-created and large brains of Android system architects invented savedState in onCreate() to easy (complicate?) that process, and then invented Fragments to easy that even more))
So, changing styles generally means changing layout. But you can change Theme attributes at runtime (per the link above - I haven't tried myself), and it will work since it doesn't need re-layouting.
As a workaround, since you will need to recreate views for new layout, you can simplify code by applying different Theme, see the link.
Change AppTheme dynamically
I solved it by changing styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/indigoColorPrimary</item>
<item name="colorPrimaryDark">@color/indigoColorPrimaryDark</item>
<item name="colorAccent">@color/indigoColorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.FullScreen">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="AppTheme.MainActivity">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
<style name="AppTheme.SearchActivity">
<item name="android:statusBarColor">@android:color/darker_gray</item>
<item name="android:windowAnimationStyle">@null</item>
</style>
<!-- Colors -->
<style name="Indigo">
<item name="colorPrimary">@color/indigoColorPrimary</item>
<item name="colorPrimaryDark">@color/indigoColorPrimaryDark</item>
<item name="colorAccent">@color/indigoColorAccent</item>
</style>
<style name="Blue">
<item name="colorPrimary">@color/blueColorPrimary</item>
<item name="colorPrimaryDark">@color/blueColorPrimaryDark</item>
<item name="colorAccent">@color/blueColorAccent</item>
</style>
<style name="Red">
<item name="colorPrimary">@color/redColorPrimary</item>
<item name="colorPrimaryDark">@color/redColorPrimaryDark</item>
<item name="colorAccent">@color/redColorAccent</item>
</style>
</resources>
And in my BaseActivity i use this code:
getTheme().applyStyle(R.style.Blue, true);
How to change current Theme at runtime in Android
I would like to see the method too, where you set once for all your activities. But as far I know you have to set in each activity before showing any views.
For reference check this:
http://www.anddev.org/applying_a_theme_to_your_application-t817.html
Edit (copied from that forum):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Call setTheme before creation of any(!) View.
setTheme(android.R.style.Theme_Dark);
// ...
setContentView(R.layout.main);
}
Edit
If you call setTheme
after super.onCreate(savedInstanceState);
your activity recreated but if you call setTheme
before super.onCreate(savedInstanceState);
your theme will set and activity
does not recreate anymore
protected void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Dark);
super.onCreate(savedInstanceState);
// ...
setContentView(R.layout.main);
}
Related Topics
Placing/Overlapping(Z-Index) a View Above Another View in Android
How to Enable/Disable Bluetooth Programmatically in Android
Difference Between .Getpath() VS Cursor in Getting the Real Path of a File from Uri in Android
Execute Asynctask Several Times
Remove All Unused Resources from an Android Project
Add and Remove Views in Android Dynamically
Meaning of Top, Ascent, Baseline, Descent, Bottom, and Leading in Android's Fontmetrics
Deleting Row in SQLite in Android
How to Enable Google Play App Signing
Android HTML Imagegetter as Asynctask
How to Maintain Fragment State When Added to the Back Stack
How to Use Library That Used Android Support with Androidx Projects
How to Draw an Overlay on a Surfaceview Used by Camera on Android
How to Make Toolbar Transparent
Deprecated Gradle Features Were Used in This Build, Making It Incompatible with Gradle 5.0
Listview Reusing Views When ... I Don't Want It To