59

I have a simple app that displays text.

The app starts with a main screen with a few options (ex. an info button that leads to info about the app, a browse button that allows the user to see all the individual pieces of text that can be displayed). The main button leads to another screen where text is displayed. By swiping left and right he can see different passages of text. This is the main purpose of the app.

Currently I have an ActionBar implemented. My MainActivity.Java extends AppCompatActivity. Everything in the app is in this activity.

Now I want to make it so the ActionBar appears only in "Display" mode, not in the start up screen, or "info" / "browse" mode.

Is it possible to have an ActionBar in one part of the app, and no ActionBar in another part of the app? (And keep it all in the same activity?)

I've been trying to accomplish this without avail. If this is possible, what should I try next?

So far, I've attempted the following:

1) Create this theme

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

And apply it to MainActivity ...

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.NoActionBar" > 

.... After doing this, the ActionBar was still there. (This comes from this S.O. post (android:windowNoTitle will not hide actionbar with appcompat-v7 21.0.0)

2) Another attempt was to add this to onCreate.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();

    setContentView(R.layout.activity_main);

This came from studying this S.O. post: (How to hide action bar before activity is created, and then show it again?)

Any suggestions?

0

9 Answers 9

95

In the activities where you want to have no action bar use a theme derived from Theme.AppCompat.NoActionBar or Theme.AppCompat.Light.NoActionBar. This activity will never be able to show the action bar unless you supply your own via setSupportActionBar(Toolbar).

In the activities where you want to have the action bar use a theme derived from Theme.AppCompat, Theme.AppCompat.Light or Theme.AppCompat.Light.DarkActionBar. This will allow you to dynamically hide or show the action bar in such activity. You will not be able to supply your own action bar using these themes.

When working with appcompat-v7 action bar you need to obtain it by calling getSupportActionBar() instead of getActionBar().

0
59

You can hide and show the ActionBar programatically.

To hide

getSupportActionBar().hide();

To Show

getSupportActionBar().show();

It can also be done from Fragments

To hide

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

To Show

getActivity().getSupportActionBar().show();

Edit:

As noted by @RenniePet,

You need to call ((AppCompatActivity)getActivity()).getSupportActionBar(), if you're using Toolbar as the ActionBar.

2
  • 3
    For use in a fragment I had to do this: "((AppCompatActivity)getActivity()).getSupportActionBar().show();"
    – RenniePet
    Commented Sep 23, 2016 at 1:30
  • hide() will throw a null exception.
    – Alston
    Commented Sep 4, 2019 at 12:12
12

Extend you activity as AppCompatActivity and then use action bar as:-

getSupportActionBar().hide();  // for hiding
getSupportActionBar().show();  // for showing
2
  • 2
    As a note, ActionBarActivity is now deprecated. AppCompatActivity should be used instead Commented Nov 26, 2015 at 10:13
  • 3
    The hide call has no effect in my app. Commented Sep 23, 2016 at 13:40
5
<style name="Theme.AppCompat.NoActionBar" parent="Theme.AppCompat.Light">
    <item name="android:windowActionBar">false</item>  
    <item name="android:windowNoTitle">true</item>
</style>

remove android in item windowActionbar.just like follow:

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

I hope it can help you.

1
  • 1
    need to remove android from android:windowNoTitle too.
    – Aamir Abro
    Commented Jan 5, 2016 at 17:02
3

AppCompatActivity has its own embedded toolbar. You dont need to use extra toolbar definition in your main xml.

just call getSupportActionBar().show()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().show();
}

public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        TextView text1=(TextView)findViewById(R.id.textView);
        text1.setText("I changed the text!");

    }

    return super.onOptionsItemSelected(item);
}
3

In onCreate method, after setContentView, use this simply to show or hide for each class.

getSupportActionBar().hide();
getSupportActionBar().show();

If you want to set title,

getSupportActionBar().setTitle("This is ToolBar");
0

I hope this can be solution. It worked on my system:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
1
  • This forces use of obsolete theme and does not cover appcompat. Commented May 17, 2015 at 8:16
0

Dismiss ToolBar for all activities with AppTheme:
styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- .NoActionBar -->
0

If you are trying to show/hide the ActionBar of an INTENT with AppCompatActivity and you get the following issues:

  • pointer null exception ...
  • thread issue ...

Follow these steps to access to the same relevant thread into the intent:

// 1) Calling the intent in the "main activity"
Intent intent = new Intent(context, YourClass.class);
startActivity(intent);

// 2) Get the context in your "Intent activity"
public class YourClass extends AppCompatActivity {
YourClass mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    mContext = this;
}

// 3) Hide/Show Action bar from a method
public void yourMethod(YourClass mContext){

    mContext.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mContext.getSupportActionBar().hide();
        }
    });

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.