0

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.

User7723337
  • 11,857
  • 27
  • 101
  • 182

1 Answers1

1

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);
Ranjeet Chouhan
  • 686
  • 5
  • 13
  • I get an error `Caused by: java.lang.IllegalStateException This Activity already has an action bar supplied by the window decor.` for the line `setSupportActionBar(toolbar);` As my activity is using default toolbar. – User7723337 Aug 13 '20 at 08:21
  • Then remove default one use AppCompatActivity and in style.xml use NoActionBar try this https://stackoverflow.com/questions/26515058/this-activity-already-has-an-action-bar-supplied-by-the-window-decor – Ranjeet Chouhan Aug 13 '20 at 08:27
  • If I remove it from the style.xml then my activity is blank I can't see anything on the screen. I tried with `mActionBar = getSupportActionBar();` in the `onCreate` and then calling hide/show `mActionBar.hide();` on some condition. It is working perfectly but activity with `NavigationView` are not responding to hide/show. But activity without `NavigationView` hide/show title bar correctly. – User7723337 Aug 13 '20 at 08:42
  • Because NavigationView has toolbar context so, it is overriding behaviour. Don't remove from style.xml just change ActionBar to NoActionBar. – Ranjeet Chouhan Aug 13 '20 at 08:46
  • Got it working now issue was the height of the bar. But I am not able to get the left burger icon on the toolbar but I see three dots menu on the right. How can I have the left menu icon. – User7723337 Aug 13 '20 at 09:57
  • getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_list); – Ranjeet Chouhan Aug 13 '20 at 10:02
  • Thanks, I can see the menu icon. But when I click on that nothing happens. How do I get the onclick on that and display the NavigationView ? Also is there a way to hide the right three dots menu. – User7723337 Aug 13 '20 at 10:11