setTheme not working for android < 2.3

556 views
Skip to first unread message

Esben Gaarsmand

unread,
Oct 4, 2012, 7:11:15 AM10/4/12
to actionba...@googlegroups.com
getSupportActionbar

In my manifest I use following theme:

<activity
            android:name="x.x.MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Sherlock.NoActionBar">

In my onCreate I have following:

        setTheme(R.style.Theme_Sherlock);
        this.getSupportActionBar().setTitle(R.string.title_activity_main);// <-- this will crash on android 2.3 and below, will work fine on ice cream sandwich, the supportactionbar is null as the theme haven't been updated.
        
        // Start regular onCreate()
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

This would be nice if it worked because it would be possible to be free of the pre action bar before the activity is started.

SimonVT

unread,
Oct 6, 2012, 6:55:36 AM10/6/12
to actionba...@googlegroups.com
Not likely this can, or will, be fixed, doing stuff before super.onCreate() is not advised, and there are certain limitations (especially on older platforms).

Jake Wharton

unread,
Oct 6, 2012, 10:31:54 PM10/6/12
to actionba...@googlegroups.com
You are setting the theme on the manifest and then using setTheme to change it?!? You should set the theme in the manifest and that's it.

---
Jake Wharton
http://about.me/jakewharton

kenny goers

unread,
Oct 8, 2012, 9:51:58 AM10/8/12
to actionba...@googlegroups.com

Is that the case? I've been using setTheme on activities since 1.6 without an issue, I offer my users a light and dark option for theming and this is by far the easiest way and the only way setTheme really works is if called before the super.onCreate method, the only other thing I'd do before a call to the super class is setting the transition animation.

Is the setTheme call being depreciated? I hadn't read that.

That said, if there is an easier way I'd love to hear it.

Kenny.

Jake Wharton

unread,
Oct 8, 2012, 10:55:58 AM10/8/12
to actionba...@googlegroups.com

It's not deprecated and yes you should call it before super.onCreate if you are providing theme-switching functionality only. Otherwise you should always declare it in the manifest.

Reply all
Reply to author
Forward
0 new messages