0

I want to show fragment in activity without no title bar. i also have added request window without title bar before inflating the views. this is not happening and it is showing exception as

requestFeature() must be called before adding content
    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:318)
    at com.wishkart.wishkart.dialogFragment.HomeProductDialogFragment.onCreateView(HomeProductDialogFragment.java:30)

My fragment code is given as:

 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    getActivity().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getActivity().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    View view = inflater.inflate(R.layout.custom_product_popup, container, false);
    return view;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Anser Abbas
  • 59
  • 3
  • 11

1 Answers1

0

The problem is that views have already been added when SetContentView is called which happens before onCreateView in the fragment. The solution is to ensure FEATURE_NO_TITLE is requested before the call to setContentView.

Inside the enclosing Activity:

@Override
protected void onCreate(
final Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);

  // Hide the Title bar of this activity screen
  getWindow().requestFeature(Window.FEATURE_NO_TITLE);

  setContentView(R.layout.main);
  ...
}

You could also hide it inside the fragment like this:

 @Override
  public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
  }

Or getActivity().getActionBar() if the support library is not being used.

Note: Call show() to restore the ActionBar.

But I would advise doing it from the Activity if you are going to do it in code.

But the most maintainable framework friendly way to hide the bar is to use a style rather than doing it in code:

Hiding the ActionBar with a theme

Elletlar
  • 3,136
  • 7
  • 32
  • 38
  • In my activity i am showing many fragment but I want to hide title bar for one fragment and other will remain same. – Anser Abbas Jul 14 '18 at 12:34
  • Okay. The original post was asking about how to get rid of requestFeature error which I think I've answered...But see the other solution that I just added, hide() and show() can be called on the ActionBar from within the fragment. – Elletlar Jul 14 '18 at 12:41