androidandroid-fragmentsandroid-titlebar

Android Hide or Show Title Bar from the Activity and Fragment


I have a Activity as below:

// My Activity Holding Fragment
public class MyActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activity_main);
   }
}

// My Fragment 
public class MyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.my_fragment, container, false);
        return view;
    }
}

I wan to have the Title Bar for my Activity.
I want to Hide or Show Title Bar from Activity and Frgment on some conditions.

I tried below solutions, but as my activity does not derive from ActionBarActivity it does not work.

// getSupportActionBar returns null
getSupportActionBar().hide();
getSupportActionBar().show();

// getActionBar returns null
getActionBar().hide();
getActionBar().show();

// findViewById returns null
findViewById(android.R.id.title).setVisibility(View.GONE);
findViewById(android.R.id.title).setVisibility(View.VISIBLE);

// Are deprecated 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

None of the solutions did work for hiding Title Bar from Activity or Fragment.

Basically I want to hide the Activity title bar for the Activity.


Solution

  • First Change your App theme from res/value/styles.xml to this

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    //leave everything as it is
    </style>
    

    After that Add toolbar in your Activity's XML Layout

     <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:background="@color/colorAccent"
        app:titleTextColor="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        //add your toolbar design here
    </toolbar>
    

    Now finally set up your toolbar in Activity Class

    public class MyActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activity_main);
    
       Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
       
       //need to set toolbar before calling getSupportActionBar
       setSupportActionBar(toolbar);
    
       //Now show or hide according to your need
       getSupportActionBar().hide();
       getSupportActionBar().show();
       
      
      }
    }
    

    Same would be applicable for fragment but for fragment you need to call view first

    Toolbar toolbar = getView().findViewById(R.id.toolbar);