Android: remove left margin from actionbar's custom layout - android

I am using a custom actionbar view, and as you can see in the screenshot below, there is a blank gray space in the actionbar. I want to remove it.
What have I done:
res/values-v11/styles.xml
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
<item name="actionBarStyle">#style/ActionBarStyle</item>
</style>
res/values/my_custom_actionbar.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:height">60dp</item>
</style>
</resources>
Manifest
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/AppName"
android:theme="#style/AppBaseTheme" >
<!-- activities... etc -->
</application>
MainActivity
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
ActionBar actionbar = getSupportActionBar();
actionbar.setDefaultDisplayHomeAsUpEnabled(false);
actionbar.setDisplayHomeAsUpEnabled(false);
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayUseLogoEnabled(false);
actionbar.setHomeButtonEnabled(false);
// Add the custom layout
View view = LayoutInflater.from(this).inflate(R.layout.actionbar, null, false);
actionbar.setCustomView(view);
}
I have found a recent post, that is pointing out that there is an issue with the latest release. I have also updated ADT and SDK to Android 5.
Android ActionBar's custom view not filling parent
I don't know what should I do.
Edit (partial solution):
Not working on Android <= API 10.
Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width
What have I changed:
Use the latest sdk version:
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
Add a toolbarStyle:
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
<item name="actionBarStyle">#style/ActionBarStyle</item>
<item name="android:toolbarStyle">#style/ToolbarStyle</item>
<item name="toolbarStyle">#style/ToolbarStyle</item>
</style>
<style name="ToolbarStyle" parent="#style/Widget.AppCompat.Toolbar">
<item name="contentInsetStart">0dp</item>
<item name="android:contentInsetStart">0dp</item>
</style>

If you are adding the Toolbar via XML, you can simply add XML attributes to remove content insets.
<android.support.v7.widget.Toolbar
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />

try this:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
View customView = getLayoutInflater().inflate(R.layout.main_action_bar, null);
actionBar.setCustomView(customView);
Toolbar parent =(Toolbar) customView.getParent();
parent.setPadding(0,0,0,0);//for tab otherwise give space in tab
parent.setContentInsetsAbsolute(0,0);

The left inset is caused by Toolbar's contentInsetStart which by default is 16dp.
Change this to align to the keyline.
Update for support library v24.0.0:
To match the Material Design spec there's an additional attribute contentInsetStartWithNavigation which by default is 16dp. Change this if you also have a navigation icon.
It turned out that this is part of a new Material Design Specification introduced in version 24 of Design library.
https://material.google.com/patterns/navigation.html
However, it is possible to remove the extra space by adding the following property to Toolbar widget.
app:contentInsetStartWithNavigation="0dp"
Before :
After :

I found an other resolution (reference appcompat-v7 ) that change the toolbarStyle ,following code:
<item name="toolbarStyle">#style/Widget.Toolbar</item>
<style name="Widget.Toolbar" parent="#style/Widget.AppCompat.Toolbar">
<item name="contentInsetStart">0dp</item>
</style>

Just add app:contentInsetStart="0dp" to the XML attribute of the toolbar.

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:paddingLeft="0dp">
This should be good enough.

Just Modify your styles.xml
<!-- ActionBar styles -->
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
</style>

Only add app:contentInsetStart="0dp" to the toolbar remove that left space.
So your Toolbar definition look like this
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:contentInsetStart="0dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
and it look like this

Instead of adding a toolbar in the layout, you can set your custom view as shown below.
Toolbar parent = (Toolbar) customView.getParent();
parent.setContentInsetsAbsolute(0,0);

If you check the documentation of toolbar, by default it has style defined and there is an item contentInsetStart.
<item name="contentInsetStart">16dp</item>
If you want to override this padding then it can be done in two ways.
1.Override the style and add your own style with contentInsetStart and contentInsetEnd values
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
</style>
2.In XML you can directly define the contentInsetStart and contentInsetEnd values
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:layout_height="?attr/actionBarSize">
<!--Add your views here-->
</androidx.appcompat.widget.Toolbar>

Using AppCompatAcitivty you can use just by
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
View logo = getLayoutInflater().inflate(R.layout.custom_toolbar, null);
mToolbar.addView(logo, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mToolbar.setContentInsetsAbsolute(0,0);

You need to add this line app2:contentInsetStart="0dp" in your toolbar
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app2="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app2:contentInsetStart="0dp"/>

this work for me
toolbar.setPadding(0,0,0,0);
toolbar.setContentInsetsAbsolute(0,0);

Setting "contentInset..." attributes to 0 in the Toolbar didn't work for me. Nilesh Senta's solution to update the style worked!
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="actionBarStyle">#style/Actionbar</item>
<item name="android:titleTextStyle">#style/ActionbarTitle</item>
</style>
<style name="Actionbar" parent="Widget.AppCompat.ActionBar">
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
</style>
java (onCreate)
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT
);
View view = LayoutInflater.from(this).inflate(R.layout.actionbar_main, null);
actionBar.setCustomView(view, layoutParams);

In xml add these two attribute for Toolbar tag:
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
and inside code add these lines:
// remove extra padding of toolbar
toolbar.setContentInsetsAbsolute(0, 0);
toolbar.setPadding(0, 0, 0, 0);
Tested on mobile and tablet devices both.

Kotlin
supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM;
supportActionBar?.setCustomView(R.layout.actionbar);
val parent = supportActionBar?.customView?.parent as Toolbar
parent?.setPadding(0, 0, 0, 0)//for tab otherwise give space in tab
parent?.setContentInsetsAbsolute(0, 0)

I did not find a solution for my issue (first picture) anywhere, but at last I end up with a simplest solution after a few hours of digging. Please note that I tried with a lot of xml attributes like app:setInsetLeft="0dp", etc.. but none of them helped in this case.
Picture 1
the following code solved this issue as in the Picture 2
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//NOTE THAT: THE PART SOLVED THE PROBLEM.
android.support.design.widget.AppBarLayout abl = (AppBarLayout)
findViewById(R.id.app_bar_main_app_bar_layout);
abl.setPadding(0,0,0,0);
}
Picture 2

Create toolbar like this:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/menuToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="#color/white"
android:contentInsetLeft="10dp"
android:contentInsetRight="10dp"
android:contentInsetStart="10dp"
android:minHeight="?attr/actionBarSize"
android:padding="0dp"
app:contentInsetLeft="10dp"
app:contentInsetRight="10dp"
app:contentInsetStart="10dp"></android.support.v7.widget.Toolbar>
please follow this link for more - Android Tips

only adding android:padding="0dp" work for me
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="60dp"
android:padding="0dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">

ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayShowCustomEnabled(true);
setDisplayShowTitleEnabled(true);
View customView = getLayoutInflater().inflate(R.layout.activity_main,null); //here activity_main.xml is the GUI design file.
ab.setCustomView(customView);
Toolbar parent =(Toolbar) customView.getParent(); //use V7 Toolbar import
parent.setContentInsetsAbsolute(0, 0);
setPadding(5,0,0,0);
ab.setIcon(R.mipmap.ic_launcher);

You can just use relative layout inside toolbar view group in your xml file and adjust the positions of widgets as you require them for your use case.No need to create custom layout & inflate it and attach to toolbar. Once done in your java code use setContentInsetsAbsolute(0,0) with your toolbar object before setting it as support action bar in your layout.

It would be better to add a background item into the style of the app actionbar to consistent with the background color of the customized actionbar:
<item name="android:background">#color/actionbar_bgcolor</item>
After android 6.0, the actionbar even has a margin-right space and cannot be set. Add a right margin adjusting in the activity like this: (the view is the customized actionbar or a right button in it)
int rightMargin = Build.VERSION.SDK_INT>=Build.VERSION_CODES.M ? 0 : 8; // may the same with actionbar leftMargin in px
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(p.leftMargin, p.topMargin, rightMargin, p.bottomMargin);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
p.setMarginEnd(rightMargin);
}
view.setLayoutParams(p);
The actionbar machenism is just supported after Android 3.0+ app. If we use a toolbar (of support lib v7) instead, we should layout it in each xml of each activity, and take care of the issue of overlapping with system status bar in the Android 5.0 or later device.

Related

How to set an icon/logo next to the title in the ActionBar?

I have tried:
In onCreate():
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_settings);
getSupportActionBar().setLogo(R.drawable.ic_settings);
In manifest:
android:logo="#drawable/ic_settings"
android:icon="#drawable/ic_settings"
And nothing is working. No errors or anything but the icons just don't appear.
you can try this:
getSupportActionBar().setDisplayUseLogoEnabled(true);
UPDATE other way:
<style name="MyActionBarLogo" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="background">#color/mainColor500</item>
<item name="logo">#drawable/actionbar_logo</item>
<item name="displayOptions">useLogo</item>
</style>
And in your main styles:
<item name="actionBarStyle">#style/MyActionBarLogo</item>
UPDATE 2 third way you can create a toolbar in activity xml file like that:
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
Set logo using method setLogo() and set toolbar as support actionBar like that:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

Android - Using custom ActionBar

I have a custom ActionBar layout which I want to use in almost all of my Activities.
The problem is the text doesn't appear.
This is how I use it in an Activity
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
//getSupportActionBar().setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.actionbar_theme);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("Signup");
But the title "Signup" doesn't appear on the ActionBar..
The theme of the ActionBar
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/actionbar_theme_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:textColor="#color/white" />
</RelativeLayout>
Does this mean that I can't just do setTitle("asdf")?
As of now you can use toolbar as ActionBar and that toolbar can have any custom layout you wish. Here is the reference link.
Declare this in activity xml
<android.support.v7.widget.Toolbar
android:id=”#+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/colorPrimary” />
Then in java code use
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
for this to work perfectly your Activity should extends AppCompatActivit
This is not how it works. If you set a custom layout to your action bar, you need to change the textview of it like if it was a normal view.
If you are using this layout several time, keep a reference of your action bar or of the viewgroup.
ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.your layout containing your text view,null);
mActionBar.setCustomView(actionBarLayout);
(TextView)findViewById(R.id.actionbar_theme_title).setText(...)

Styling Toolbar does not work using AppCompat

I am trying as best I can to style the ActionBar (i.e. an android.support.v7.widget.Toolbar) in my app, but it just won't work. I am following the official documentation when trying to achieve this. What I am really trying to do is to make the title color white. It is currently displayed in black. My target API is 11.
What am I doing wrong?
res/values/themes.xml
<resources>
<!-- Base theme applied no matter what API -->
<style name="MMTheme.Base" parent="Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColor">#color/mm_dark_gray</item>
<item name="colorPrimary">#color/mm_brown</item>
<item name="colorPrimaryDark">#color/mm_dark_gray</item>
<item name="colorAccent">#color/mm_green</item>
<item name="android:actionBarStyle">#style/MMActionBar</item>
<item name="android:actionBarTabTextStyle">#style/MMActionBarTabText</item>
<item name="android:actionMenuTextColor">#color/mm_green</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MMActionBar</item>
<item name="actionBarTabTextStyle">#style/MMActionBarTabText</item>
<item name="actionMenuTextColor">#color/mm_green</item>
</style>
<style name="MMTheme" parent="MMTheme.Base"></style>
<!-- ActionBar style -->
<style name="MMActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#color/mm_green</item>
<item name="android:titleTextStyle">#style/MMActionBarTitleText</item>
<!-- Support library compatibility -->
<item name="titleTextStyle">#style/MMActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MMActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/mm_white</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
<!-- ActionBar tabs text -->
<style name="MMActionBarTabText" parent="#style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">#color/mm_green</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
</resources>
The inflated android.support.v7.widget.Toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mm_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"/>
In the Activity
// Setup main Toolbar
Toolbar toolbar = (Toolbar) mInflatedActivity.findViewById(R.id.mm_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
I have tried everything I can think of and searched a lot online, but I cannot make it work. What am I missing?
Current result is (nevermind the search icon):
Adding style="#style/MMActionBar" to the xml properties of the android.support.v7.widget.Toolbar results in this:
Changing main parent theme to Theme.AppCompat.Light.DarkActionBar does nothing.
Just use this in yout layout xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/red600"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Set theme in xml itself to ThemeOverlay.AppCompat.Dark.ActionBar which will make title text white.
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
And I guess this is the right way to theme Toolbar, since according to official Android Developer blog
Styling of Toolbar is done differently to the standard action bar, and
is set directly onto the view.
Here's a basic style you should be using when you're using a Toolbar
as your action bar:
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
The app:theme declaration will make sure that your text and items are
using solid colors (i.e 100% opacity white).
I have faced same problem. The following solution solved my problem.
For this you need to make changes at two places.
1.Modify your toolbar xml like this,
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/ColorPrimary"
android:elevation="2dp"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#+id/TV_tool_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My App Name"
android:textSize="18sp"
android:textColor="#FFFFFF"/>
</android.support.v7.widget.Toolbar>
Modify like this in your activity file.
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
Thats it. This will make your title color white. Cheers..!
Use this to change colors of the letters on the toolbar:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp"
>
</android.support.v7.widget.Toolbar>

Remove shadow below actionbar

I use actionbarsherlock. The piece of code below is responsible for changing it's background to a custom one.
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.ActionBar">
<item name="background">#drawable/actionbar_bg</item>
<item name="android:background">#drawable/actionbar_bg</item>
<...>
</style>
<style name="Theme.MyApp" parent="#style/Theme.Sherlock.Light">
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
<..>
</style>
And it works for actionbarsherlock (on versions below honeycomb). But in ICS I have a shadow below actionbar which I don't want. What is the style item to make it disappear?
What is the style item to make it disappear?
In order to remove the shadow add this to your app theme:
<style name="MyAppTheme" parent="android:Theme.Holo.Light">
<item name="android:windowContentOverlay">#null</item>
</style>
UPDATE:
As #Quinny898 stated, on Android 5.0 this has changed, you have to call setElevation(0) on your action bar. Note that if you're using the support library you must call it to that like so:
getSupportActionBar().setElevation(0);
For Android 5.0, if you want to set it directly into a style use:
<item name="android:elevation">0dp</item>
and for Support library compatibility use:
<item name="elevation">0dp</item>
Example of style for a AppCompat light theme:
<style name="Theme.MyApp.ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<!-- remove shadow below action bar -->
<!-- <item name="android:elevation">0dp</item> -->
<!-- Support library compatibility -->
<item name="elevation">0dp</item>
</style>
Then apply this custom ActionBar style to you app theme:
<style name="Theme.MyApp" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/Theme.MyApp.ActionBar</item>
</style>
For pre 5.0 Android, add this too to your app theme:
<!-- Remove shadow below action bar Android < 5.0 -->
<item name="android:windowContentOverlay">#null</item>
On Android 5.0 this has changed, you have to call setElevation(0) on your action bar. Note that if you're using the support library you must call it to that like so:
getSupportActionBar().setElevation(0);
It's unaffected by the windowContentOverlay style item, so no changes to styles are required
add app:elevation="0dp" in AppBarLayout for hiding shadow in appbar
If you are working with ActionBarSherlock
In your theme add this:
<style name="MyTheme" parent="Theme.Sherlock">
....
<item name="windowContentOverlay">#null</item>
<item name="android:windowContentOverlay">#null</item>
....
</style>
You must set app:elevation="0dp" in the android.support.design.widget.AppBarLayout and then it works.
<android.support.design.widget.AppBarLayout
app:elevation="0dp"... >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
app:popupTheme="#style/AppTheme.PopupOverlay" >
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
app:elevation="0dp"
but not
android:elevation="0dp"
worked for me on android L
I have this same problem, and I have successfully solved this problem. All you have to do is just change the elevation to 0 floating point value in that activity in which you want to remove the elevation.
If you want to change it in an activity called MyActivity.java so you have to get the ActionBar first.
First import the ActionBar class
import android.support.v7.app.ActionBar;
after importing you have to initialize a variable of action bar and set its elevation to 0.
private ActionBar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
.
.
toolbar=getSupportActionBar();
toolbar.setElevation(0);
.
.
}
Solution for Kotlin (Android 3.3, Kotlin 1.3.20)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar!!.elevation = 0f
}
For Xamarin Developers, please use : SupportActionBar.Elevation = 0; for AppCompatActivity or ActionBar.Elevation = 0; for non-compat Activities
Try This it helped me without changing theme . Put Your AppBarLayout inside any layout.Hope this will help you
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="false"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_height="?attr/actionBarSize"
android:background="#color/white">
<ImageView
android:src="#drawable/go_grocery_logo"
android:layout_width="108dp"
android:layout_height="32dp" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
Add this toolbar.getBackground().setAlpha(0); to inside OnCreate method. The add this:
android:elevation="0dp" android:background="#android:color/transparent
to your toolbar xml file.
For those working on fragments and it disappeared after setting toolbar.getBackground().setAlpha(0); or any disappearance, i think you have to bring your AppBarLayout to the last in the xml, so its Fragment then AppBarLayout then relativelayout/constraint/linear whichever u use.
Use:
outLineAmbientShadowColor="#null"

Padding between ActionBar's home icon and title

Does anybody know how to set padding between the ActionBar's home icon and the title?
EDIT: make sure you set this drawable as LOGO, not as your app icon like some of the commenters did.
Just make an XML drawable and put it in the resource folder "drawable" (without any density or other configuration).
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/my_logo"
android:right="10dp"/>
</layer-list>
The last step is to set this new drawable as logo in your manifest (or on the Actionbar object in your activity)
Good luck!
I adapted Cliffus answer and assigned the logo-drawable in my actionbar style definition, for instance like this in res/style.xml:
<item name="android:actionBarStyle">#style/MyActionBar</item>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#3f51b5</item>
<item name="android:titleTextStyle">#style/ActionBar.TitleText</item>
<item name="android:textColor">#fff</item>
<item name="android:textSize">18sp</item>
<item name="android:logo">#drawable/actionbar_space_between_icon_and_title</item>
</style>
The drawable looks like Cliffus' one (here with the default app launcher icon) in res/drawable/actionbar_space_between_icon_and_title.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/ic_launcher"
android:right="20dp"/>
</layer-list>
In the android_manifest.xml you can still set a different app icon (launcher icon on 'desktop'. Any different logo definition here are visible in activities without an action bar.
I also faced a similar issue, in my case I had to set titles dynamically on each activity depending on the content.
So this worked for me.
actionBar.setTitle(" " + yourActivityTitle);
If all you want is the spacing, this is the easiest solution I could think of.
This is how I was able to set the padding between the home icon and the title.
ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(left, top, right, bottom);
I couldn't find a way to customize this via the ActionBar xml styles though. That is, the following XML doesn't work:
<style name="ActionBar" parent="android:style/Widget.Holo.Light.ActionBar">
<item name="android:titleTextStyle">#style/ActionBarTitle</item>
<item name="android:icon">#drawable/ic_action_home</item>
</style>
<style name="ActionBarTitle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textSize">18sp</item>
<item name="android:paddingLeft">12dp</item> <!-- Can't get this padding to work :( -->
</style>
However, if you are looking to achieve this through xml, these two links might help you find a solution:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml
(This is the actual layout used to display the home icon in an action bar)
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/action_bar_home.xml
This is a common question in Material Design as you may want to line your toolbars title with the content in the fragment below. To do this, you can override the default padding of "12dp" by using the attribute contentInsetStart="72dp" in your toolbar.xml layout as shown below
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/app_theme_color"
app:contentInsetStart="72dp"/>
Then in your activity, call
Toolbar toolbar = (Toolbar) activity.findViewById(R.id.toolbar);
toolbar.setTitle("Title Goes Here");
And you end up with this:
If you are using toolbar from AppCompat (android.support.v7.widget.Toolbar, >= revision 24, June 2016), the padding between the icon and the title can be changed with the following value :
app:contentInsetStartWithNavigation="0dp"
To improve my answer, you can use it on your toolbar directly inside your activity or you can create a new layout file for your toolbar. In this case, you just have to import your toolbar's #id using the include property on each needed views.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
</android.support.v7.widget.Toolbar>
You can then import your layout on your activity.xml
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
For me only the following combination worked, tested from API 18 to 24
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
where in "app" is : xmlns:app="http://schemas.android.com/apk/res-auto"
for example.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/SE_Life_Green"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
>
.......
.......
.......
</android.support.v7.widget.Toolbar>
Using titleMarginStart works for me. Xamarin example:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleMarginStart="24dp"/>
Set the logo like so:
mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);
SetSupportActionBar(mToolbar);
SupportActionBar.SetLogo(Resource.Drawable.titleicon32x32);
SupportActionBar.SetDisplayShowHomeEnabled(true);
SupportActionBar.SetDisplayUseLogoEnabled(true);
SupportActionBar.Title = "App title";
I used \t before the title and worked for me.
Im using a custom image instead of the default title text to the right of my apps logo. This is set up programatically like
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setCustomView(R.layout.include_ab_txt_logo);
actionBar.setDisplayShowCustomEnabled(true);
The issues with the above answers for me are #Cliffus's suggestion does not work for me due to the issues others have outlined in the comments and while #dushyanth programatic padding setting may have worked in the past I would think that the fact that the spacing is now set using android:layout_marginEnd="8dip" since API 17 manually setting the padding should have no effect. See the link he posted to git to verify its current state.
A simple solution for me is to set a negative margin on my custom view in the actionBar, like so android:layout_marginLeft="-14dp". A quick test shows it works for me on 2.3.3 and 4.3 using ActionBarCompat
Hope this helps someone!
I solved this problem by using custom Navigation layout
Using it you can customize anything in title on action bar:
build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:21.0.+'
...
}
AndroidManifest.xml
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name=".MainActivity"
android:theme="#style/ThemeName">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
styles.xml
<style name="ThemeName" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/ActionBar</item>
<item name="android:actionBarStyle" tools:ignore="NewApi">#style/ActionBar</item>
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
<item name="displayOptions">showCustom</item>
<item name="android:displayOptions" tools:ignore="NewApi">showCustom</item>
<item name="customNavigationLayout">#layout/action_bar</item>
<item name="android:customNavigationLayout" tools:ignore="NewApi">#layout/action_bar</item>
<item name="background">#color/android:white</item>
<item name="android:background">#color/android:white</item>
action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_launcher"
android:drawablePadding="10dp"
android:textSize="20sp"
android:textColor="#android:color/black"
android:text="#string/app_name"/>
I had a similar issue but with spacing between the up and the custom app icon/logo in the action bar. Dushyanth's solution of setting padding programatically worked for me (setting padding on app/logo icon). I tried to find either android.R.id.home or R.id.abs__home (ActionBarSherlock only, as this ensures backwards compatibility), and it seems to work across 2.3-4.3 devices I've tested on.
For my case, it was with Toolbar i resolved it like this:
ic_toolbar_drawble.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/ic_toolbar"
android:right="-16dp"
android:left="-16dp"/>
</layer-list>
In my Fragment, i check the api :
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
toolbar.setLogo(R.drawable.ic_toolbar);
else
toolbar.setLogo(R.drawable.ic_toolbar_draweble);
Good luck!
You can change drawableSize of your DrawerArrow like this:
<style name="MyTheme" parent="android:Theme.WithActionBar">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="barLength">24dp</item>
<item name="arrowShaftLength">24dp</item>
<item name="arrowHeadLength">10dp</item>
<item name="drawableSize">42dp</item> //this is your answer
</style>
It isn't correct answer, because you can't choose padding side and DrawerArrow icon scaling when change drawableSize (drawableSize = width = height). But you can margin from left. To margin from right do
findViewById(android.R.id.home).setPadding(10, 0, 5, 0);
I used this method setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) and it works!
int padding = getResources().getDimensionPixelSize(R.dimen.toolbar_content_insets);
mToolbar.setContentInsetsAbsolute(padding, 0);
When you are using a custom Toolbar, you can use
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.activity_title);
setSupportActionBar(toolbar);
getSupportActionBar().setLogo(R.drawable.logo);
and in your toolbar layout simply set app:titleMarginStart="16dp"
Note that you have to set the icon as a logo, don't use getSupportActionBar().setIcon(R.drawable.logo)
instead use:
getSupportActionBar().setLogo(R.drawable.logo)
I used AppBarLayout and custom ImageButton do to so.
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:background="#android:color/transparent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:src="#drawable/selector_back_button"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:id="#+id/back_button"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
My Java code:
findViewById(R.id.appbar).bringToFront();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
getSupportActionBar().setDisplayShowTitleEnabled(false);
You can achieve same by method as well:
Drawable d = new InsetDrawable(getDrawable(R.drawable.nav_icon),0,0,10,0);
mToolbar.setLogo(d);
In your XML, set the app:titleMargin in your Toolbar view as following:
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:titleMarginStart="16dp"/>
Or in your code:
toolbar.setTitleMargin(16,16,16,16); // start, top, end, bottom
Just include:
app:titleMargin="10dp"
Within your xml file, like:
<androidx.appcompat.widget.Toolbar
android:id="#+id/tbrMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleMargin="10dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
this work for me to add padding to the title and for ActionBar icon i have set that programmatically.
getActionBar().setTitle(Html.fromHtml("<font color='#fffff'> Boat App </font>"));
<string name="app_name">" "Brick Industry</string>
Just add " " for your app name
It will add space between icon and title

Categories

Resources