149

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);

        c.setContentView(R.layout.main);

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

Here is where I call it in onCreate():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupUI();
}

private void setupUI(){
     setContentView(R.layout.main);
     UIHelper.setupTitleBar(this);
}

But I get the error:

requestFeature() must be called before adding content
Jonik
  • 80,077
  • 70
  • 264
  • 372
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • Answered here: http://stackoverflow.com/questions/16939814/android-util-androidruntimeexception-requestfeature-must-be-called-before-add – karma Jan 12 '15 at 08:09

9 Answers9

362

Well, just do what the error message tells you.

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

Note:

As said in comments, for both ActionBarSherlock and AppCompat library, it's necessary to call requestFeature() before super.onCreate()

Sankar V
  • 4,794
  • 3
  • 38
  • 56
Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
  • 63
    damn, this is a classy answer. – A Person Mar 20 '13 at 09:58
  • 60
    For ActionBarSherlock, it's also necessary to call `requestFeature()` before `super.onCreate()`. Reference: https://github.com/JakeWharton/ActionBarSherlock/issues/310#issuecomment-7350074 – Saran Sep 07 '13 at 19:43
  • 1
    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
  • 36
    Its the same case in AppCompat as @Saran said. You must call your `requestFeature` before `super.onCreate()` – Jaison Brooks Oct 27 '14 at 19:57
  • 1
    with ActionBarSherlock it worked for me fine. But with AppCompat it crashes. And when I put requestFeature before super.onCreate() it does not crashes but it does not have any effect either... – Andranik Dec 05 '14 at 17:56
  • can you help in my case this is not worked for me http://stackoverflow.com/questions/27404146/when-orientation-changed-i-got-requestfeature-must-be-called-before-adding-con – Sasha Dec 10 '14 at 15:06
  • 8
    This answer doesn't help much...but what do I know – Ojonugwa Jude Ochalifu May 26 '15 at 22:13
  • Its better to call before super.onCreate() in most of the cases. I faced in case of AdTapsy – Narendra Singh Jul 14 '15 at 14:20
  • 1
    This is so weird, I am not even calling this function and it keep having this error! – Neon Warge Aug 31 '16 at 05:27
  • error message should be more specific - ' requestWindowFeature()' must be called before adding content – Shirish Herwade Jan 11 '17 at 14:22
  • In my case `setSystemUiVisibility()` is same like `setContentView()` method. It must be placed after `requestWindowFeature()`. – Muhammed Aydogan Oct 12 '20 at 12:43
24

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) {
       super(context);

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

       setUpAdditionalStuff(); // do more view cleanup
       setView(content);           
   }

   private void setUpAdditionalStuff() {
       // ...
   }

   // Call ((CustomDialog) dialog).prepare() in the onPrepareDialog() method  
   public void prepare() {
       setTitle(R.string.custom_title);
       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.
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
Cookster
  • 1,463
  • 14
  • 21
  • 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
  • 2
    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 05:20
15

I was extending a DialogFragment and the above answer didnot work. I had to use getDialog() to achieve remove the title:

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
6

For SDK version 23 and above, the same RuntimeException is thrown if you are using AppCompatActivity to extend your activity. It will not happen if your activity derives directly from Activity.

This is a known issue on google as mentioned in https://code.google.com/p/android/issues/detail?id=186440

The work around provided for this is to use supportRequestWindowFeature() method instead of using requestFeature().

Please upvote if it solves your problem.

Keshav Bansal
  • 407
  • 4
  • 6
2

In my case I showed DialogFragment in Activity. In this dialog fragment I wrote as in DialogFragment remove black border:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(STYLE_NO_FRAME, 0)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    super.onCreateDialog(savedInstanceState)

    val dialog = Dialog(context!!, R.style.ErrorDialogTheme)
    val inflater = LayoutInflater.from(context)
    val view = inflater.inflate(R.layout.fragment_error_dialog, null, false)
    dialog.setTitle(null)
    dialog.setCancelable(true)
    dialog.setContentView(view)
    return dialog
}

Either remove setStyle(STYLE_NO_FRAME, 0) in onCreate() or change/remove onCreateDialog. Because dialog settings will change after the dialog has been created.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
1

Change the Compile SDK version,Target SDK version to Build Tools version to 24.0.0 in build.gradle if u face issue in request Feature

Jaichander
  • 812
  • 2
  • 15
  • 24
1

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?

EboMike
  • 76,846
  • 14
  • 164
  • 167
0

I had this issue with Dialogs based on an extended DialogFragment which worked fine on devices running API 26 but failed with API 23. The above strategies didn't work but I resolved the issue by removing the onCreateView method (which had been added by a more recent Android Studio template) from the DialogFragment and creating the dialog in onCreateDialog.

BillC
  • 145
  • 2
  • 8
0

I had the same problem using AppCompatDialogFragment, my workaround was:

override fun onAttachFragment(childFragment: Fragment) {       
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE)
    super.onAttachFragment(childFragment)
}
Nelson
  • 233
  • 1
  • 4
  • 6