user3685293
user3685293

Reputation: 121

android hide title in fragment

I have started working in fragment. I am trying to hide titleBar in fragment, but i have this Log-cat Error

android fragment requestfeature must be called before adding content

this is a source

public class SendItemsFragment extends Fragment {

Button b1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);


    View rootView = inflater.inflate(R.layout.send_items, container, false);



    return rootView;
    }

}

Upvotes: 10

Views: 13123

Answers (6)

Yasser AKBBACH
Yasser AKBBACH

Reputation: 1038

There's a better workaround:

(activity as MainActivity).supportActionBar?.title = ""

Make sure to change it before setting the content:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        (activity as MainActivity).supportActionBar?.title = ""
        binding = LoginFragmentBinding.inflate(inflater, container, false)
        return binding.root
    }

Upvotes: 1

nanaboison
nanaboison

Reputation: 101

This is what worked for me:

Hide action bar

((AppCompatActivity) getActivity()).getSupportActionBar().hide();

Upvotes: 9

Tuan
Tuan

Reputation: 84

In Manifest file, change label declare in activity is empty is my solution.

Upvotes: 0

TomV
TomV

Reputation: 1232

I had the same issue. The other answers in this post of setting

requestWindowFeature(Window.FEATURE_NO_TITLE); in the Activity before loading the fragment didn't work. What worked was this: In the AndroidManifest.xml application tag I had this:

android:theme="@style/Theme.AppCompat.DayNight"

and the issue was resolved by including a NoActionBar suffix:

android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"

Upvotes: 0

user3428540
user3428540

Reputation:

Just change your code as

requestWindowFeature(Window.FEATURE_NO_TITLE);

in onCreate function instead of,

getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);

public class SendItemsFragment extends Fragment {
   private Activity activity;
   public SendItemsFragment (Activity act) {
       this.activity = act;
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
         View rootView = inflater.inflate(R.layout.send_items, container, false);
         return rootView;
   }
}

Upvotes: 1

Gaskoin
Gaskoin

Reputation: 2485

You cannot call this method after setting contentView in Activity (LogCat answers your question already). You can do nothing about that. You must change your Fragment to Activity, or design your application in a different way. Eventually you can only hide title from Actionbar

getActivity().getActionBar().hide()

Upvotes: 1

Related Questions