requestFeature() must be called before adding content

The question:

I am trying to implement a custom titlebar:

Here is my Helper class:

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

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Well, just do what the error message tells you.

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


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

Method 2

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.

Method 3

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


Method 4

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

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

Please upvote if it solves your problem.

Method 5

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?) {
    setStyle(STYLE_NO_FRAME, 0)

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

    val dialog = Dialog(context!!,
    val inflater = LayoutInflater.from(context)
    val view = inflater.inflate(R.layout.fragment_error_dialog, null, false)
    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.

Method 6

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?

Method 7

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

Method 8

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.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment