Android Dynamically Change Style at Runtime

android dynamically change style at runtime

Changing the style after creating the view is not supported .. so what you can do is:

  1. create a new android xml file of type values
  2. add new theme
  3. 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



Leave a reply



Submit