I searched the Internet to find any good solution to this problem and did not find it. So I did the trick that did it.
First of all, we need to request the overlay function of the action bar. So in onCreate() your activity before setContntView() call: requestWindowFeature(com.actionbarsherlock.view.Window.FEATURE_ACTION_BAR_OVERLAY);
He will do everything, including the navigation box, outside the action bar. We donβt need this, so we need to set the top edge of our FrameLayout, which places our fragments in activity with the exact height of the action bar. In the activity layout file, we need to do the following:
<FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="?attr/actionBarSize" />
This will only result in a navigation box behind the action bar. Now we will hide the action bar when the navigation box is opened in half, and will show it when the box is almost closed. To do this, we need to do the following:
@Override public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); if(slideOffset > 0.5){ actionBar.setBackgroundDrawable(null); actionBar.hide(); } else { actionBar.show(); if(slideOffset < 0.1){ actionBar.setBackgroundDrawable(layerDrawable); } } }
As you can see, I also change the background image of the action bar to make it transparent before I start hiding it, and change it back using my own background when I show it.
My custom background is a layerListDrawable which is all transparent but has a separator at the bottom with some shadow.
And for this, I defined the following list layer in XML:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp"> <shape android:shape="rectangle"> <solid android:color="@android:color/transparent"/> </shape> </item> <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp"> <shape android:shape="rectangle"> <solid android:color="#afafaf"/> </shape> </item> <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp"> <shape android:shape="rectangle"> <gradient android:angle="270" android:startColor="#88afafaf" android:endColor="#33afafaf" /> </shape> </item> </layer-list>
And to get the background that I need from this XML, I do the following in this step:
final ActionBar actionBar = getSupportActionBar(); final LayerDrawable layerDrawable = (LayerDrawable) getResources() .getDrawable(R.drawable.shadow_divider); final TypedArray styledAttributes = getTheme().obtainStyledAttributes( new int[] { R.attr.actionBarSize }); int topOffset = (int) (styledAttributes.getDimension(0, 0)); styledAttributes.recycle(); layerDrawable.setLayerInset(1, 0, topOffset - 3, 0, 2); layerDrawable.setLayerInset(2, 0, topOffset - 2, 0, 0); actionBar.setBackgroundDrawable(layerDrawable);
where R.drawable.shadow_divider is the list of XML layers that I defined earlier.
It looks great! Hope this can help someone.
EDIT
I had a small error, which can sometimes be the cause of crushing. Here is the fixed code: `
<FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" **android:paddingTop="?attr/actionBarSize"** />`
It should be paddingTop not layout_marginTop !