Take the 2-minute tour ×

I am trying to implement a custom titlebar:

Here is my Helper class:

import android.app.Activity;
import android.view.Window;

public class UIHelper {
    public static void setupTitleBar(Activity c) {
        final boolean customTitleSupported = c.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);


        if (customTitleSupported) {
            c.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);

Here is where I call it in onCreate():

public void onCreate(Bundle savedInstanceState) {

private void setupUI(){

But I get the error:

requestFeature() must be called before adding content
share|improve this question
add comment

3 Answers

up vote 60 down vote accepted

Well, just do what the error message tells you.

Don't call setContentView() before requestFeature().

share|improve this answer
damn, this is a classy answer. –  A Person Mar 20 '13 at 9:58
For ActionBarSherlock, it's also necessary to call requestFeature() before super.onCreate(). Reference: github.com/JakeWharton/ActionBarSherlock/issues/… –  Saran Sep 7 '13 at 19:43
What if you need to show one layout without a title bar and then show one with a title bar? You will have to use requestWindowFeature(Window.FEATURE_NO_TITLE) to hide it, then setContentView() for your first layout, then requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) to show the title bar again. That would be after setContentView() the second time. –  msbg Sep 29 '13 at 16:44
add comment

I know it's over a year old, but calling requestFeature() never solved my problem. In fact I don't call it at all.

It was an issue with inflating the view I suppose. Despite all my searching, I never found a suitable solution until I played around with the different methods of inflating a view.

AlertDialog.Builder is the easy solution but requires a lot of work if you use the onPrepareDialog() to update that view.

Another alternative is to leverage AsyncTask for dialogs.

A final solution that I used is below:

public class CustomDialog extends AlertDialog {

   private View content;

   public CustomDialog(Context context) {

       LayoutInflater li = LayoutInflater.from(context);
       content = li.inflate(R.layout.custom_view, null);

       setUpAdditionalStuff(); // do more view cleanup

   private void setUpAdditionalStuff();
       // ...

   // Call ((CustomDialog) dialog).prepare() in the onPrepareDialog() method  
   public void prepare() {
       setIcon( getIcon() );
       // ...

* Some Additional notes:

  1. Don't rely on hiding the title. There is often an empty space despite the title not being set.
  2. Don't try to build your own View with header footer and middle view. The header, as stated above, may not be entirely hidden despite requesting FEATURE_NO_TITLE.
  3. Don't heavily style your content view with color attributes or text size. Let the dialog handle that, other wise you risk putting black text on a dark blue dialog because the vendor inverted the colors.
share|improve this answer
Originally I posted setTitle() and setIcon() in the onCreate() method, but the edit moved it to the prepare() method which is called during the onPrepareDialog() method. –  Cookster Jun 28 '12 at 19:29
Thanks for sharing. I think the following line content = inflater.inflate(R.layout.custom_view, null); should probably be content = li.inflate(R.layout.custom_view, null);. So, inflater has to be replaced by li. –  aLearner Nov 17 '12 at 5:20
add comment

Doesn't the error exactly tell you what's wrong? You're calling requestWindowFeature and setFeatureInt after you're calling setContentView.

By the way, why are you calling setContentView twice?

share|improve this answer
add comment

Your Answer


