Johan Sánchez
Johan Sánchez

Reputation: 165

Remove ActionBar in fragment

I have some troubles trying to remove a toolbar in a specific fragment, i have this code in onCreateView fragment method:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_login,container,false);
    mValidator = new Validator(this);
    mValidator.setValidationListener(this);
    mFbHelper = new 
    mGHelper = new GoogleHelper(this, null, this);

    ((DrawerUtil) getActivity()).setDrawerLocked(true);
    ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
    initViews(view);
    return view;
}

I can hide the ActionBar, but the idea is to remove the ActionBar to use properly a logo, im using this code: ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(state); but it doesnt work.enter image description here Any ideas?

Upvotes: 4

Views: 8453

Answers (7)

Yogesh Nikam Patil
Yogesh Nikam Patil

Reputation: 1280

For a specific fragment you can use :

override fun onStart() {
        super.onStart()
        (activity as AppCompatActivity).supportActionBar!!.hide()
    }

    override fun onStop() {
        super.onStop()
        (activity as AppCompatActivity).supportActionBar!!.show()
    }


    override fun onResume() {
        super.onResume()
        (activity as AppCompatActivity).supportActionBar!!.hide()
    }

Upvotes: 5

LiA
LiA

Reputation: 386

for kotlin, using

import android.support.v7.app.AppCompatActivity

Try

 override fun onStart() {
        super.onStart()
        //hide action bar from splash screen
        (activity as AppCompatActivity).supportActionBar!!.hide()
 }

in the specific fragment(s, and

            supportActionBar!!.hide()

in the Activity/Activities.

Note: do not forget to replace "hide()" by "show()" when you want the ActionBar to be visible again.

Upvotes: 1

Nabende
Nabende

Reputation: 61

It may be a little late, but this may save the next OP some minutes of searching. This is what worked for me @Johan Sanchez . Assuming that you have declared the theme to NoActionBar in your manifest but a blank space remains, its probably your XML files..

In my main_activity.xml file is a fragment placeholder that will be replaced by the respective fragment xml files... inside it is a layout_margintop attribute. Remove it

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="simiyu.com.smallheaven.MainActivity">
<FrameLayout
    android:id="@+id/flContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"/>

</android.support.design.widget.CoordinatorLayout>

The result should then be as

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="simiyu.com.smallheaven.MainActivity">
<FrameLayout
    android:id="@+id/flContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"/>

</android.support.design.widget.CoordinatorLayout>
enter code here

Upvotes: 0

jigar savaliya
jigar savaliya

Reputation: 504

you can remove it from xml file,check your activity code where fragment is loading and make changes there if this fragment is load than toolbar.setvisibility to gone

Upvotes: 0

Rajesh Gosemath
Rajesh Gosemath

Reputation: 1872

For your login activity use

<style name="BaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
   <item name="windowNoTitle">true</item>
   <item name="windowActionBar">false</item>
</style>

this will remove the default ActionBar provided by android.So attach your fragment to this activity and there will be no action bar in your fragment too.

For other activities if you need a ActionBar you can use the same theme and add Toolbar as actionBar to youer activity.

If you do not want ActionBar use theme

parent="@style/Theme.AppCompat.Light.NoActionBar"

If you want ActionBar use theme

parent="@style/Theme.AppCompat.Light.DarkActionBar" or

parent="@style/Theme.AppCompat.Dark.LightActionBar"

If you use light or dark ActionBar theme you will get a default ActionBar.Again if you want to hide this u can do this

getActivity().getSupportActionBar().hide();    // in Fragment

getSupportActionBar().hide();                  // in Activity 

Upvotes: 1

Anil
Anil

Reputation: 1615

Try like this

private HomeActivity csActivity;

and

csActivity      = (HomeActivity)getActivity();
csActivity.getSupportActionBar().hide();

Upvotes: 4

Vishal Vaishnav
Vishal Vaishnav

Reputation: 3422

An activity in which your load your fragment ,try below code in java file;

 ActionBar actionBar = getSupportActionBar();
 actionBar.hide();

and remove below line from Menifest .xml

 android:theme="@style/AppTheme.NoActionBar"

Upvotes: 0

Related Questions