Premier
Premier

Reputation: 4228

Dynamically change activity theme

My application has a generic main activity. Main activity reads its configuration from arrays.xml like follow

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="home_buttons_text">
    <item>@string/Title_1</item>
  </string-array>
  <array name="home_buttons_icon">
    <item>@drawable/google</item>
  </array>
  <string-array name="home_buttons_url">
    <item>http://www.google.com</item>
  </string-array>
  <array name="themes">
    <item>@style/my_theme</item>
  </array>
</resources>

In this way to add a new icon, i just put new lines here.

So in "themes" I've theme's name. How i can load and apply theme referred by name?

For icons I've used

TypedArray mItemsIcons = resources.obtainTypedArray(R.array.home_buttons_icon);
TypedArray mThemes = resources.obtainTypedArray(R.array.themes);
.
.
.
mItemsIcons.getDrawable(position);
.
.
.

But I've no idea on how to get theme. I've tested

int theme = mThemes.getResourceId(position);
setTheme(theme);

but it doesn't work.

Check here for details: http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray

Upvotes: 4

Views: 3784

Answers (1)

Ahmed Salem
Ahmed Salem

Reputation: 3969

you can use setTheme(..) before calling setContentView(...)and super.oncreate() and it should work fine

Upvotes: 4

Related Questions