change actionbar style in android - android

I want to override appcompat action bar style, I do this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:background">#android:color/black</item>
but this doesn't apply to the element,
this is an XML element:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
Your help will be appreciated, Thank you

use local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
for more details refer https://dzone.com/articles/getting-started-with-android-app-and-material-desi

Set Activity theme to this style
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
In your xml your toolbar should be created in AppBarLayout
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MyTheme"
app:elevation="0dp"
app:popupTheme="#style/AppTheme.PopupOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/MyTheme"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
MyTheme should have parent ThemeOverlay.AppCompat.Dark.ActionBar
<style name="MyTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
//customize toolbar here`</style>
`
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

Related

Change color and size of the title in Toolbar- Android

I am trying to change the title color and size of the toolbar but nothing happens. I search everywhere and nothing seems to work. I am using minsdk 19 api. Here is my code:
**activity.main.xml**
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
>
<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/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
**style.xml**
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryDarkColor</item>
<!--<item name="colorAccent">#color/primaryLightColor</item>-->
<item name="android:textColor">#color/primaryTextColor</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="actionBarStyle">#style/MyTitleTextStyle</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="MyTitleTextStyle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textColor">#color/primaryColor</item>
</style>
</resources>
It's well explained here. Add this to your Toolbar:
app:titleTextAppearance="#style/Toolbar.TitleText"
And create a style for it:
<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textColor">#FFFFFF</item>
</style>
You can also try this. This will allow you to control textsize
Define ActionBarTextAppearance in style.xml
<style name="ActionBarTextAppearance" parent="#android:style/TextAppearance.Medium">
<item name="android:textSize">30sp</item>
<item name="android:textColor">#ec1212</item>
<item name="android:textStyle">bold</item>
</style>
Then use in main.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:titleTextAppearance="#style/ActionBarTextAppearance"
/>
The easy way to achive tour goal is to have Textview as title inside your Toolbar and then give your choice size and color
<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/AppTheme.PopupOverlay"
>
<Textview
android:layout_width="wrap_parent"
android:layout_height="wrap_parent"/>
</android.support.v7.widget.Toolbar>

Android : Toolbar Subtitle Gravity Right Not Working

I want to create actionbar subtitle but its gravity should be right. It should end at title end. Please see following image
My Style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
</style>
<style name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">#style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
</style>
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">#color/black_overlay</item>
</style>
<style name="ToolbarSubtitleAppearance" parent="#style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
<item name="android:textSize">10sp</item>
<item name="android:layout_gravity">right</item>
<item name="android:layout_width">match_parent</item>
</style>
</resources>
Toolbar in Layout File :
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<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/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
Can i achieve this via doing small changes because i had included in many layout file.
make your toolbar like this so your can custimize as you want
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title "/>
<TextView
android:id="#+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sub title "/>
</LinearLayout>
</android.support.v7.widget.Toolbar>

Eliminate strange grey trapezoid in transparent toolbar

I could not find the source of some kind of mask laying over my transparent toolbar:
Does anybody know how to remove this?
When doing orientation change to landscape it's even more ugly.
Some code:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000" <-- makes no difference
android:theme="#style/AppTheme.AppBarOverlay.Transparent">
<include layout="#layout/toolbar_transparent"/>
</android.support.design.widget.AppBarLayout>
toolbar_transparent:
<?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/my_toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:elevation="4dp"
style="?attr/actionBarStyle"
android:background="#204CAF50"
android:theme="#style/AppTheme.NoActionBar.ColoredToolbar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
themes.xml (I admit it is a mess - much like try-and-error)
<style name="AppThemeBase" parent="Theme.AppCompat">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:activatedBackgroundIndicator">#drawable/activated_background</item>
</style>
<style name="AppTheme" parent="AppThemeBase"/>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="android:activatedBackgroundIndicator">#drawable/activated_background</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.AppBarOverlay.Transparent" parent="ThemeOverlay.AppCompat.ActionBar" >
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorPrimary">#00000000</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppTheme.ActionBar" parent="AppTheme.NoActionBar">
<item name="windowActionBar">true</item>
<item name="windowNoTitle">false</item>
</style>
<style name="AppTheme.NoActionBar.ColoredToolbar" parent="AppTheme.NoActionBar">
<item name="android:textColorSecondary">#color/colorPrimaryDark</item>
</style>
This comes from the android.support.design.widget.AppBarLayout wrapper.
When I remove it and just leave the Toolbar, the trapezoids are gone.
<!--<android.support.design.widget.AppBarLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:theme="#style/AppTheme.AppBarOverlay.Transparent">-->
<include layout="#layout/toolbar_transparent"/>
<!--</android.support.design.widget.AppBarLayout>-->
Works at least for Lollipop and Marshmallow. Kitkat and below not tested yet.
BTW I'm using com.android.support:design:24.2.1.
This works for me:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay.Transparent">
<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:theme="#style/AppTheme.AppBarOverlay.Transparent"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
in styles
<style name="AppTheme.AppBarOverlay.Transparent" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>

Tablayout overlapping toolbar

I'm trying to use Tablayout with Toolbar but it's giving me like the image below, and when try to remove <item name="android:fitsSystemWindows">true</item> from v21\styles.xml the Toolbar being overlapped by the android system status bar
v21\styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="MyMaterialTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:typeface">serif</item>
<item name="android:fitsSystemWindows">true</item>
</style>
<style name="ToolBarStyle" parent="AppTheme">
<item name="colorControlNormal">#android:color/white</item>
</style>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="MyMaterialTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:typeface">serif</item>
</style>
<style name="ToolBarStyle" parent="AppTheme">
<item name="colorControlNormal">#android:color/white</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="PopupTheme" parent="Theme.AppCompat">
<item name="android:background">#color/popColorBg</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:drawSelectorOnTop">true</item>
</style>
activity.xml
<android.support.design.widget.CoordinatorLayout 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="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/tools"
>
<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:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.melnykov.fab.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="#drawable/ic_add_white_24dp"
fab:fab_colorNormal="#color/colorPrimaryFab"
fab:fab_colorPressed="#color/colorPrimaryDarkFab"
fab:fab_colorRipple="#color/colorPrimaryDarkFab" />
And according to this Answer if I'll do this and remove android:fitsSystemWindows="true" from v21\styles.xml my layout will be like this:

Background color of Toolbar in Lollipop is normal but in Kitkat is white

In Lollipop, The background color of toolbar was shown normally, but in KitKat, it was shown as white color.
I don't know why.
activity_main.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<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/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<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>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay"
parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
styles.xml-v21
<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
Try putting your AppBarLayout at the end of you code. Apparently your included layout is overlapping the ToolBar. That happened to me and I fixed it doing that, it just came to my mind. Like this:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<include layout="#layout/content_main"/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<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/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Create a base theme in styles.xml with primary colors and other colors colors then set it as parent in styles.xml-v21 for your style name="AppTheme.NoActionBar".
Below is my example :
in styles.xml
<style name="AppBase" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary_color</item>
<item name="colorPrimaryDark">#color/primary_color_dark</item>
<item name="colorAccent">#color/primary_yellow_bright</item>
<item name="colorControlNormal">#color/primary_yellow</item>
<item name="colorControlHighlight">#color/primary_color_light_100</item>
<item name="colorControlActivated">#color/primary_yellow_bright</item>
<item name="colorButtonNormal">#color/primary_yellow_bright</item>
<item name="android:windowBackground">#color/primary_white</item>
<item name="android:textColorPrimary">#color/primary_color</item>
<item name="android:textColorSecondary">#color/primary_color_light</item>
<item name="android:textColor">#color/primary_white</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
</style>
<style name="AppTheme" parent="AppBase" />
in styles.xml-v21
<style name="AppTheme" parent="AppBase">
<item name="android:navigationBarColor">#color/primary_light_gray</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
in AndroidManifest.xml
android:theme="#style/AppTheme"
should work.

Categories

Resources