Amrmsmb
Amrmsmb

Reputation: 1

How to change the Theme of an activity after it is created?

After creating an activity, how can I change its theme to a new one, for example, Theme.Holo.

Is it possible?

Upvotes: 0

Views: 122

Answers (2)

Bryan Herbst
Bryan Herbst

Reputation: 67189

You cannot change the theme of an Activity after it is created. You can change the theme before any Views are added, but not after.

As per the docs for setTheme(int):

Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)).

If you wish to change the theme before any Views are added, you can do so in onCreate() like so:

public void onCreate(Bundle savedInstanceState) {
    setTheme(myThemeResourceId);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

Upvotes: 2

Guillermo Merino
Guillermo Merino

Reputation: 3257

You can do:

@Override
protected void onCreate(final Bundle bundle) {
    activity.setTheme(yourThemeID);
    super.onCreate(bundle);
}

Upvotes: 1

Related Questions