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
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.
In my case I showed
DialogFragment
inActivity
. In this dialog fragment I wrote as in DialogFragment remove black border:Either remove
setStyle(STYLE_NO_FRAME, 0)
inonCreate()
or chande/removeonCreateDialog
. Because dialog settings have changed after the dialog has been created.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:
* Some Additional notes:
I was extending a DialogFragment and the above answer didnot work. I had to use getDialog() to achieve remove the title:
Doesn't the error exactly tell you what's wrong? You're calling
requestWindowFeature
andsetFeatureInt
after you're callingsetContentView
.By the way, why are you calling
setContentView
twice?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