I want to set the navigation icon for my toolbar ,and get into the problem that the icon isn't center inside by default it looks like:
and the code is:
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_48dp);
and the theme.xml is:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowTranslucentStatus">true</item>
<item name="android:navigationBarColor">#color/colorPrimaryDark</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
and the layout xml is:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"
/>
I think I have tried everything I know but I just can't figure it out.I used to set the navigation icon for toolbar then it will be center inside and now.Any help will save my day!
Eidted: of course I tried different size of icons
and I konw I could add a custom layout in toolbar or customize a layout like (which is a Linearlayout I write about)
But I just don't why I can't set the navigation icon like before,it will automatically be center inside....I think maybe I should change the theme or define something like navigationButttonStyle in the attributes....
I fix this by inherit the right theme Widget.AppCompat.Toolbar rather than ThemeOverlay.AppCompat.Dark.ActionBar to define my own toolbar style.
And use
<item name="toolbarStyle">#style/MyToolBar</item>
to set the toolbarStyle
I think you are using a bit big image. Try figure it out using this link
OK now I finally find out where the problem is: which has nothing to do with the icon size
You should place the image resource in the to the corresponding folder like (drawable-xxhdpi , drawable-xhdpi).If you don't do so,which means if you just put it in the drawable folder , it won't be auto resized to be center inside in your toolbar;
Related
I have been trying to achieve this in a project with minSDK 16 :
Actionbar and statusbar in White color with dark text and icons.
Like this:
I have been trying to figure this out for ages now. Google searches lead to a lot of answers that dont make any sense and/or dont support older api's. colorControlNormal, titleTextColor, Appcompat-this, Appcompat-that... its a nightmare!
I am also not able to find any docs on what items to put in my styles xml, and none of the combinations of style properties I tried seem to have any effect whatsoever.
So can someone please give me a proper and well explained walkthrough as to how I can acheive this effect?
Thanks.
you can try with that
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">#color/white</item>
<item name="android:windowLightStatusBar">true</item>
</style>
but API min is 23.
Also, don't forget to use different styles for different versions of APIs so far I haven't found an app that works as you want, so most probably isn't possible.
Add these to your Base Application Theme in styles.xml
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
And add following attribute to your parent layout:
android:fitsSystemWindows=”true”
You can customize your toolbar with custom view as follows:
Create your view inside toolbar and use in you code as you want
XML:
<android.support.v7.widget.Toolbar>
</android.support.v7.widget.Toolbar>
Java:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
This will help you
To change the default option menu icon color(3 vertical dote), i have tried adding
<item name="android:textColorSecondary">#color/white</item>
But it doesn't make any change. I think it is not working coz my AppTheme parent is Theme.AppCompat.Light.NoActionBar. So can anybody help me on this?
The easiest way of doing it is by adding
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
or
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Dark</item>
to your primary Theme in order to change it to light or dark.
EDIT: In order to add any color for the overflow icon you would have to override the actionOverflowButtonStyle like so:
<style name="Theme.MyAppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="actionOverflowButtonStyle">#style/OverFlow</item>
</style>
<style name="OverFlow" parent="#style/Widget.AppCompat.Light.ActionButton.Overflow">
<item name="android:src">#drawable/ic_overflow_icon</item>
</style>
You can easily create your own overflow icon from websites like Android Asset Studio
2019 - Easiest way to do is write the below code to your xml in toolbar code or where you want.
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
please give up vote if its work.
You can Either use your own drawable or get the default overFlowIcon and change its tint value like below:
bd.toolbar.overflowIcon?.setTint(Color.WHITE)
To change the drawable all together, you just have to create your own vector and set it like below:
bd.toolbar.overflowIcon = ContextCompat.getDrawable(this,R.drawable.ic_baseline_menu_24)
If you are using Toolbar, just add the following code in your Activity:
toolbar.setOverflowIcon(getDrawable(R.drawable.ic_more_vert_black_24dp));
By using this code, you can directly replace the "three dots" icon with any other icon of your choice. It worked for me.
Im having problems with the new way to use toolbar. In landscape mode, toolbar title is centered vertically but the navigation drawer not. How can I solve this?
I am using the toolbar like in this post of Chris Banes:
AppCompat v21 — Material Design for Pre-Lollipop Devices!
This is the code for the drawer icon in style.xml:
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
And this is an image of the problem:
Landscape image
EDIT: Overflow button is not centered vertically too...
This is my toolbar theme and everything is centered correctly:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
style="#style/Widget.MyApp.Toolbar">
</android.support.v7.widget.Toolbar>
The example says to use android:minHeight="?actionBarSize" and android:_layout_height="wrap_content" but it only produced the error you described.
Add this in your toolbar style:
<item name="maxButtonHeight">?attr/actionBarSize</item>
This allows you to use wrap_content height and still have a centered icon!
I'm using appcompat v7 with the hamburger to arrow toggle.
However, I would like to add some padding on the left of the toggle.
This is my current situation :
http://nl.tinypic.com/r/2a0f712/8
This is what I would like to have :
http://nl.tinypic.com/r/v3q176/8
I've tried this :
findViewById(android.R.id.home).setPadding(25, 0, 15, 0);
But that didn't work. However, this did add padding on the right of the toggle.
I've also tried to add padding in the styles but that didn't work either.
AFAIK, the action is hard-coded in layout resources. U cant change the padding. Google did this to maintain the consistency across the app guess.
However u can define ur own icon in a drawable with ur own attributes (padding and all) and use it as an indicator by adding this line to styles.xml
<item name="android:homeAsUpIndicator">#drawable/xyz</item>
And the image u have shown about what u want is actually a toolbar and not an action bar. In toolbar, the icon is by default placed there.
To know more about toolbar and how to implement them, here is a link : http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html
I'm going to assume you are familiar with styling to some extent and recommend you go that route. FYI I have not confirmed the code below as working but I think it would be close to what you want.
For res/values-14/style.xml, something like this:
<!-- style for Action Bar -->
<style name="MyPaddedActionBar" parent="android:style/Widget.Holo.Light.ActionBarView">
<item name="android:paddingLeft">32dp</item>
<item name="android:paddingRight">32dp</item>
</style>
And then for everything 14 and below, you would use AppCompat (res/values-14/style.xml):
<style name="MyPaddedActionBar" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:paddingLeft">32dp</item>
<item name="android:paddingRight">32dp</item>
</style>
I'm using the Toolbar (instead of ActionBar) via AppCompat. I'd like to replace the Toolbar's title (the app/actity name) with an icon, but I don't see how.
My icon is just text using a non-standard font, so I guess it might be allowed in Material Design.
Use the following steps in your activity:
Declare the Toolbar object:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool1);
Set the support actionbar:
setSupportActionBar(toolbar);
Remove title:
getSupportActionBar().setDisplayShowTitleEnabled(false);
Add your logo in drawable folder in:
res/drawable.
Add logo using this code:
toolbar.setLogo(R.drawable.ic_launcher);
At styles.xml first make a style for action bar with no title and with the logo like that
<style name="ActionBar.NoTitle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="displayOptions">useLogo|showHome</item>
<item name="logo">#drawable/ic_logo</item>
</style>
Then create custome theme for your activity with new actionbar style
<!-- Create a style for MainActivity called AppTheme.Main that uses our custom ActionBar style -->
<style name="AppTheme.Main" parent="#style/AppTheme">
<item name="actionBarStyle">#style/ActionBar.NoTitle</item>
</style>
Then go to manifest file under the specified activity set theme to AppTheme.Main
android:theme="#style/AppTheme.Main"
I wish that help everyone
Here is the answer to a very similar question:
Android Lollipop, add popup menu from title in toolbar
It comes down to using the Toolbar as a ViewGroup (LinearLayout is a ViewGroup too, for instance). The layout designer doesn't currently support that, but you can add a child node in the XML.
Then you need to call this to remove the standard title text:
getSupportActionBar().setDisplayShowTitleEnabled(false);