Removing extra space in custom ActionBar
My problem is about using fully custom ActionBar views (as this is needed in order for my client to want the ActionBar to work). I removed the logo, title and everything else is possible. However, the custom view for the action bar will not span the entire screen.
I tried the following (as a quick overview):
- Dynamically removing aspects of ActionBar (snippet below)
- Complete removal of the options menu
- Specifying the style / theme to remove everything from the action bar
Here is a screenshot of my problem:
Here's the style: (as a side note, I originally kept the native ActionBar, but removed most of it)
<!-- Customized App Theme -->
<style name="AppTheme2" parent="Theme.AppCompat.Light">
<!-- Let the actioBbar overlay the activity, ie activity is full screen -->
<item name="android:windowActionBarOverlay">true</item>
<!-- Set up the action bar styles -->
<item name="android:actionBarStyle">@style/ActionBarEmpty</item>
<!-- Remove the shadow under the actionBar -->
<item name="android:windowContentOverlay">@null</item>
<!-- Set the drawer icon to show up instead of the "Up" caret icon
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
-->
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/ActionBarEmpty</item>
</style>
<!-- Final, empty Action Bar style | makes space for customized actionBar -->
<style name="ActionBarEmpty" parent="Widget.AppCompat.Base.ActionBar">
<!-- TODO: Add support versions -->
<item name="android:displayOptions">none</item>
<item name="android:background">@color/transparent</item>
<!-- Tested the below, does absolute nothing -->
<item name="android:layout_margin">0dp</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">0dp</item>
</style>
Here is the code in my Activity regarding setting up the ActionBar:
// Initialize and set up the ActionBar
mActionBar = getActionBar();
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
// Set the actionBar layout initially to the simple one
mActionBar.setCustomView(mViewActionBarSimple);
Here is the view shown in the screenshot for the action bar:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="@color/white">
<ImageButton
android:id="@+id/navButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/padding_actionbar_icon"
android:paddingBottom="@dimen/padding_actionbar_icon"
android:paddingRight="@dimen/padding_actionbar_icon"
android:paddingEnd="@dimen/padding_actionbar_icon"
android:layout_marginLeft="@dimen/margin_actionbar_icon_left"
android:layout_marginStart="@dimen/margin_actionbar_icon_left"
android:src="@drawable/ic_drawer_normal"
android:background="?android:attr/selectableItemBackground"
/>
<TextView
android:id="@+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_general"
android:layout_gravity="center"
android:textColor="@color/black"
android:textSize="@dimen/text_normal"
android:text="@string/garbage_fill_short"
/>
</LinearLayout>
In addition to trying to remove everything I could think of, I also tried to remove the options menu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// To remove the options menu - and make the custom actionBar take the full space -
// always return false
return false;
}
I am running this on a Samsung Galaxy S4 with Android KitKat.
Thanks for reading this. Any help would be helpful at this point.
Update: For anyone reading this, I don't know why it worked like this, but I ran into a problem after following the answer below. I surrounded the actionBar layout with a RelativeLayout [with LinearLayout and its content as children] and the width / background corrected themselves ... The answer is still amazing as it is very crisp and completely removes the actionBar even though I used to have everything still using this in some way.
source to share
Try below to remove the content of the action bar by creating a basic activity so you can link throughout your application and inflate the custom action bar from the layout
@SuppressLint("InlinedApi")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(false);
actionBar.setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
View homeIcon = findViewById(android.R.id.home);
// Hides the View (and so the icon)
if (homeIcon != null)
((View) homeIcon.getParent()).setVisibility(View.GONE);
overridePendingTransition(0, 0);
}
add custom view
@SuppressLint("InlinedApi")
@Override
public void setContentView(int layoutResID)
{
super.setContentView(R.layout.activity_base);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container);
viewGroup.removeAllViews();
viewGroup.addView(getLayoutInflater().inflate(layoutResID, null));
// you can find action_bar layouts view & add listner
}
and customize the XML containing the custom action bar in activity_base.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:orientation="vertical" >
<include layout="@layout/action_bar" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right" />
</LinearLayout>
If you want to use, then prolong this action.
source to share