How to Show and Hide Actionbar with Appcompat V.7

How To Show and hide ActionBar with AppCompat v.7

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().

Android: how to hide ActionBar on certain activities

Apply the following in your Theme for the Activity in AndroidManifest.xml:

<activity android:name=".Activity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

android:windowNoTitle will not hide actionbar with appcompat-v7 21.0.0

@Chk0nDanger your answer is true but you should use below code :

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

without parent element, everything will be white color (textviews,buttons,chekcboxs, etc)!

in your manifest.xml file :

    <activity android:name=".MyClass"
android:theme="@style/Theme.AppCompat.NoActionBar"
/>

updated 2015 29 July

make sure android:windowNoTitle should be replaced by windowNoTitle when Upgraded to AppCompat v22.1.0

Android Hide actionbar and Toolbar

Just replace this:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">

with this:

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

and if you want to hide the toolbar after the setSupportActionbar(Toolbar toolBar) method use getSupportActionbar().hide();

Hope it helps!!!

ActionBarCompat: Hide ActionBar before activity is created (bug?)

I got stuck with the same problem and, it seems to me, found a reason of this strange behavior. I looked through source of support library and got this:

Appcompat checks a value of mHasActionBar variable before creating new action bar in ActionBarActivityDelegate

final ActionBar getSupportActionBar() {
// The Action Bar should be lazily created as mHasActionBar or mOverlayActionBar
// could change after onCreate
if (mHasActionBar || mOverlayActionBar) {
if (mActionBar == null) {
mActionBar = createSupportActionBar();
...

And we can change its value by calling supportRequestWindowFeature(int featureId) which is delegated by ActionBarActivity to a ActionBarActivityDelegate.

There are base delegate class ActionBarDelegateBase and its descendants ActionBarDelegateHC, ActionBarActivityDelegateICS, ActionBarActivityJB, one of which is chosen according to a version of running android. And method supportRequestWindowFeature is actually works fine almost in all of them, but it's overridden in ActionBarActivityDelegateICS like that

@Override
public boolean supportRequestWindowFeature(int featureId) {
return mActivity.requestWindowFeature(featureId);
}

So it has no effect on the variable mHasActionBar, that's why getSupportActionBar() returns null.

We almost there. I came to two different solutions.

First way

  1. import source project of appcompat from git

  2. change overridden method in ActionBarActivityDelegateICS.java to something like this

    @Override
    public boolean supportRequestWindowFeature(int featureId) {
    boolean result = mActivity.requestWindowFeature(featureId);
    if (result) {
    switch (featureId) {
    case WindowCompat.FEATURE_ACTION_BAR:
    mHasActionBar = true;
    case WindowCompat.FEATURE_ACTION_BAR_OVERLAY:
    mOverlayActionBar = true;
    }
    }
    return result;
    }
  3. place this line in activity's onCreate method before getSupportActionBar()

    supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);

Second way

  1. import project of appcompat from android SDK (which is with empty src directory)

  2. add this method to your activity

    private void requestFeature() {
    try {
    Field fieldImpl = ActionBarActivity.class.getDeclaredField("mImpl");
    fieldImpl.setAccessible(true);
    Object impl = fieldImpl.get(this);

    Class<?> cls = Class.forName("android.support.v7.app.ActionBarActivityDelegate");

    Field fieldHasActionBar = cls.getDeclaredField("mHasActionBar");
    fieldHasActionBar.setAccessible(true);
    fieldHasActionBar.setBoolean(impl, true);

    } catch (NoSuchFieldException e) {
    Log.e(LOG_TAG, e.getLocalizedMessage(), e);
    } catch (IllegalAccessException e) {
    Log.e(LOG_TAG, e.getLocalizedMessage(), e);
    } catch (IllegalArgumentException e) {
    Log.e(LOG_TAG, e.getLocalizedMessage(), e);
    } catch (ClassNotFoundException e) {
    Log.e(LOG_TAG, e.getLocalizedMessage(), e);
    }
    }
  3. call requestFeature() in onCreate method of your activity like this

    if (Build.VERSION.SDK_INT >= 11) {
    requestFeature();
    }
    supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);

I used the second way. That's all.

transparent Actionbar with AppCompat-v7 21

<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>

<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ACTION BAR STYLES -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
<item name="android:background">@drawable/actionbar_background</item>
<item name="android:windowActionBarOverlay">true</item>

<!-- Support library compatibility -->
<item name="background">@drawable/actionbar_background</item>
<item name="windowActionBarOverlay">true</item>
</style>

Toolbar and Contextual ActionBar with AppCompat-v7

Update:

Solution: use the windowActionModeOverlay property. Set this in your theme:

<item name="windowActionModeOverlay">true</item>

and the actionmode will be shown over the action bar instead of pushing it down. (If you're not using the latest AppCompat then you need to add the "android:" prefix to the property). It basically lets AppCompat know that you have a toolbar located in the top of the screen and that it should draw the ActionMode on top of it.



Old answer/workaround:

I ran into the same problem. No matter what theme I set, it always pushes down the Toolbar I set as ActionBar. I tried with and without the support library, but it didn't matter.

Unfortunately I was not able to fix it so I have built a workaround instead. In my ActionModeCallback's onCreateActionMode I hide the action bar:

actionBarToolbar.setVisibility(View.GONE);

and in onDestroyActionModeI show it again:

actionBarToolbar.setVisibility(View.VISIBLE);

The hiding/showing happens so quickly it is not noticeable on my test devices. There is of course a downside: although the enter-animation still works, the exit-animation of the contextual action bar gets lost because the Toolbar immediately pops over it. But until we come across a better solution I guess we are stuck with this.


(My Activity is actually extending a custom BaseActivity class which has a method called getActionBarToolbar(), taken from the Google I/O 2014 app source code, so I can easily get fetch the Toolbar:

BaseActivity activity = (BaseActivity) getActivity();
activity.getActionBarToolbar().setVisibility(View.GONE);

Too bad the I/O app does not use the contextual action bar.)

Unable to hide deafault action bar [ANDROID]

<activity android:name=".MainActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" />

use this code in your android menifest , surely it will work



Related Topics



Leave a reply



Submit