How do I style appcompat-v7 Toolbar like Theme.AppCompat.Light.DarkActionBar?
The recommended way to style the Toolbar for a Light.DarkActionBar
clone would be to use Theme.AppCompat.Light.DarkActionbar
as parent/app theme and add the following attributes to the style to hide the default ActionBar:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Then use the following as your Toolbar:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
For further modifications, you would create styles extending ThemeOverlay.AppCompat.Dark.ActionBar
and ThemeOverlay.AppCompat.Light
replacing the ones within AppBarLayout->android:theme
and Toolbar->app:popupTheme
. Also note that this will pick up your ?attr/colorPrimary
if you have set it in your main style so you might get a different background color.
You will find a good example of this is in the current project template with an Empty Activity
of Android Studio (1.4+).
appcompat v7 23, actionbars, transparency
I think you are doing it in the wrong way.I mean, the following Toolbar Hasn't any PrimaryColor or even Background:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
app:layout_scrollFlags="scroll|enterAlways"/>
<!--android:background="?attr/colorPrimaryDark"-->
<!--app:theme="@style/AppTheme.AppBarOverlay">-->
<!--android:layout_height="?attr/actionBarSize"-->
<!--app:popupTheme="@style/AppTheme.PopupOverlay"-->
So, this will works on (v21) versions only without any background or PrimaryColor
.and as you can see, Toolbar is going to be like your Activity Theme
which that has:
<item name="colorPrimary">@color/colorPrimary</item>
With:
Theme.AppCompat.Light.DarkActionBar
And, if you don't set any background color or PrimaryColor, because of:
Theme.AppCompat.Light.DarkActionBar
and your Toolbar
, it has to be like that:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
Otherwise, it should crashed(i guess, not sure) because you have a Theme with DarkActionBar
and one Toolbar with no background.
I think this is the problem!
Edit:
For completing the answer, since this code was simple anyway, i'll add it here:
Add this also in your AppBarLayout
:
android:theme
Unable to get AppCompat ActionBar/Toolbar to become an overlay
So, it turns out the issue was simultaneously totally simple, and yet not obvious (to me, at least). Basically the issue is that although the Toolbar is being set as the Actionbar, it is still at the end of the day a layout element declared in XML. So, the trick is of course to make sure it overlaps in the XML. Here's a sample from my layout, now:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/actionbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
All works as expected now. Hope this can help someone else if they have the same issue!
Styling Toolbar does not work using AppCompat
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).
Related Topics
<Select> Not Working in Phonegap App on Android 2.3.3
Widgets Don't Respond When Re-Added Through Code
A Correct Way to Convert Byte[] in Java to Unsigned Char* in C++, and Vice Versa
Android: Listview.Getscrolly() - Does It Work
Android Custom Url to Open App Like in iOS
App Crashes on Startup Due to Java.Lang.Illegalargumentexception: Column '_Id' Does Not Exist
Eclipse Can't Find Android.Support.V4.Widget.Swiperefreshlayout
Android.App.Application Cannot Be Instantiated Due to Nullpointerexception
Getting Net::Err_Unknown_Url_Scheme While Calling Telephone Number from HTML Page in Android
Android Stock Browser Not Respecting CSS Overflow
Finding Android Sdk on MAC and Adding to Path
Google Maps API V2 Class Not Found
Disable Android Browser's Input Overlays
Find the Physical Address of Exception Vector Table from Kernel Module
How to Solve Render Problem Path.Op() Not Supported
Using Android Studio with Vuforia
Is There a Faster Way to Decode HTML Characters to a String Than HTML.Fromhtml()