Design Support TabLayout Style Not Applied - android

I'm working with android.support.design.widget.TabLayout and I'm having a REAL hardtime trying to make the applied theme to work. Here's the XML I'm trying to use:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/viewPagerLayout_tabView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/TabLayout.Theme"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewPagerLayout_viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
Here's the style that is being applied:
<style name="TabLayout.Theme" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/rowHeaderColor</item>
<item name="tabIndicatorHeight">5dp</item>
<item name="tabTextAppearance">#style/TextAppearance.Jacksonville.Tab</item>
<item name="tabSelectedTextColor">#000</item>
</style>
<style name="TextAppearance.Jacksonville.Tab" parent="TextAppearance.Design.Tab">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#4C4D4F</item>
<item name="textAllCaps">true</item>
</style>
When applied to the theme, the TextColor doesn't appear as the one I selected. I thought it was a problem with my sight so I changed it to Selected: Black and not selected: white but the same colors appeared. Is there something I would be needing to besides that in order for the theme to be applied? The problem is only with the text colors. The textAllCaps, textSize and other attributes are being applied with no major problems.
Thanks!

Related

toolbar width without menu button

I've created application with menu and toolbar.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
android:id="#+id/app_bar_menu_coord_layout_included"
layout="#layout/app_bar_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_menu"
app:menu="#menu/activity_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
The toolbar (the app_bar_menu_coord_layout_included above):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/app_bar_menu_coord_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:tag="app_bar_menu_coord_layout"
tools:context="com.magnifi.pennantrace.MenuActivity">
<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="#drawable/mybutton"
app:popupTheme="#style/AppTheme.PopupOverlay">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--first-->
<TextView
android:id="#+id/toolbar_div_rank"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="4th Place"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#id/guidelineToolbarV1"
app:layout_constraintEnd_toStartOf="#+id/guidelineToolbarH1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
...
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/content_frame"/>
</LinearLayout>
Now I wanted to adjust text sizes of toolbar elements depending on screen size. What I do is take toolbar width as 100% and set the rest of textview's text sizes to fit the desired rectangle (for example 6% of width and 100% of height).
But it seems like its not working properly.
I've tested on screen size 320 height and 480 width:
As you see on the picture, first of all the width of it is 480, and as I noticed the text adjustment doesn't work, take a look on the last text 37M, it should have fitted into one line. What I think is that the menu button (the red rectangle in image) is taking some place. What would be the best way to identify actual width of toolbar without the menu button?
EDIT 1:
I've tried adding to the app_bar_menu_coord_layout_included this:
android:layout_marginLeft="-64dp"
And here is the result of correct text size adjustment. But I don't want to get rid of the menu button. Instead I want to get correct size of toolbar without menu button.
Android gave us components to make ease implementing components. Not for giving hours to customise it. Why don't you make an custom toolbar instead of wasting hours in managing default toolbar.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
style="#style/styleToolbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_back"
style="#style/styleToolbarIcon"
android:src="#drawable/ic_back" />
<TextView
android:id="#+id/headerTitle"
style="#style/styleToolbarText"
android:text="#string/app_name" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
some styles to be put in res>values>styles.xml.
<style name="styleToolbar" parent="Widget.AppCompat.Toolbar">
<item name="android:layout_width">match_parent</item>
<item name="android:background">#color/colorWhite</item>
<item name="android:layout_height">?attr/actionBarSize</item>
<item name="colorControlNormal">#color/colorWhite</item>
<item name="android:textColorPrimary">#color/colorWhite</item>
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">#style/MyToolbarTheme</item>
<item name="contentInsetEnd">0dp</item>
<item name="contentInsetLeft">0dp</item>
<item name="contentInsetRight">0dp</item>
<item name="contentInsetStart">0dp</item>
<item name="contentInsetStartWithNavigation">0dp</item>
</style>
<style name="MyToolbarTheme">
<!-- Used to tint the back arrow, menu and spinner arrow -->
<item name="colorControlNormal">#FFF</item>
</style>
<style name="styleToolbarIcon">
<item name="android:layout_width">?attr/actionBarSize</item>
<item name="android:layout_height">match_parent</item>
<item name="android:contentDescription">#string/app_name</item>
<item name="android:padding">#dimen/toolbar_icon_padding</item>
<item name="android:gravity">center</item>
</style>
<style name="styleToolbarText">
<item name="android:layout_width">0dp</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">match_parent</item>
<item name="android:gravity">left|center_vertical</item>
<item name="android:singleLine">true</item>
<item name="android:textColor">#252525</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:scrollHorizontally">true</item>
</style>
You should customize the toolbar layout as per your need. So you will have all controls on its size.

Android - Custom style on Action Bar

is it possible to customize the background (and title) of my action bar in a specific activity? What I have done and it's not working:
<style name="AppTheme.BenchTheme"> <!-- style of my activity -->
<item name="android:windowBackground">#android:color/holo_blue_light</item>
<item name="android:actionBarStyle">#style/BenchActionBar</item>
</style>
and
<style name="BenchActionBar">
<item name="android:background">#color/colorBench</item>
</style>
I want to do it in my style.xml. Is it possible? I know I can do it via code (using something like getSupportActionBar().setBackgroundDrawable(myColor);)...
Thanks
First make your Theme extend from Theme.AppCompat.Light.NoActionBar:
<style name="AppTheme.BenchTheme" parent="Theme.AppCompat.Light.NoActionBar">
Then in your layout you can already add the toolbar as you want for example:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:background="#color/kelly_green"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
style="#style/MyToolbarElevation"
>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/txt_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/_2sdp"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="#dimen/_13sdp"
/>
</android.support.v7.widget.Toolbar>

Set transparent toolbar while using navigation drawer [duplicate]

It is self Q&A post
I have transparent ActionBar which overlays layout. After migration to the latest support library I have been forced to get rid off the ActionBar in favor of the Toolbar. The old ways to make it transparent and overlay that layout doesn't work anymore.
<style name="CustomActionBarTheme" parent="#android:style/Theme.AppCompat">
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:actionBarStyle">#style/TransparentActionBar</item>
</style>
<style name="TransparentActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#android:color/transparent</item>
</style>
Create your toolbar.xml file with background of AppBarLayout is #null
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:id="#+id/general_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Login"
android:textSize="20sp"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
and here is result:
All you need to is to define theme which hide action bar, define action bar style with transparent background and set this style to the toolbar widget. Please note that toolbar should be drawen as last view (over all view tree)
<style name="Theme.Custom" parent="#android:style/Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="CustomActionBar" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar should be above content-->
<include layout="#layout/toolbar" />
</RelativeLayout>
Toolbar layout:
<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"
app:theme="#style/CustomActionBar"/>
Checking Google's example Source Code I found out how to make the toolbar completely transparent. It was simpler than I thought. We just have to create a simple Shape drawable like this.
The name of the drawable is toolbar_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:type="linear" />
</shape>
And then in the fragment or activity.. Add the toolbar like this.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/toolbar_bg"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
And here we will have a fully transparent toolbar.
Don't add the <android.support.design.widget.AppBarLayout > if you do, this won't work.
Note: If you need the AppBarLayout, set the elevation to 0 so it doesn't draw its shadow.
The Toolbar works like a View, so the answer it's very simple.
toolbar.getBackground().setAlpha(0);
Only this worked for me (AndroidX support library):
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:theme="#style/AppTheme.AppBarOverlay"
android:translationZ="0.1dp"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#null"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
This code removes background in all necessary views and also removes shadow from AppBarLayout (which was a problem)
Answer was found here: remove shadow below AppBarLayout widget android
Add the following line in style.xml
<style name="Base.Theme.AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme" parent="Base.Theme.AppTheme">
</style>
Now add the following line in style-v21,
<style name="AppTheme" parent="Base.Theme.AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
and set the theme as
android:theme="#style/AppTheme"
It will make the status bar transparent.
Just add android:background="#android:color/transparent" like below in your appbar layout
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>`
Just use the following xml code:
Code for the layout:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/def_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/toolbarTransparent"
android:layout_alignParentTop="true" />
And add the following code in the colors.xml:
<color name="toolbarTransparent">#00FFFFFF</color>
This will give you the desired output.
Perhaps you need to take a look at this post transparent Actionbar with AppCompat-v7 21
Points that the post suggest are
Just ensure that you use RelativeLayout to lay Toolbar and the body.
Put the Toolbar in the last.
Put transparent color for android:background attribute of the Toolbar
This should solve the problem without too much hassle.
The simplest way to put a Toolbar transparent is to define a opacity in #colors section, define a TransparentTheme in #styles section and then put these defines in your toolbar.
#colors.xml
<color name="actionbar_opacity">#33000000</color>
#styles.xml
<style name="TransparentToolbar" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
#activity_main.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:background="#color/actionbar_opacity"
app:theme="#style/TransparentToolbar"
android:layout_height="?attr/actionBarSize"/>
That's the result:
Screenshot transparent toolbar
I implemented translucent Toolbar by creating two Theme.AppCompat.Light.NoActionBar themes and setting colorPrimary attribute to transparent color.
1) Create one theme for opaque Toolbar:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Toolbar background color -->
<item name="colorPrimary">#ff212cff</item>
</style>
Second theme for transparent/overlay Toolbar:
<style name="AppTheme.Overlay" parent="AppTheme">
<item name="colorPrimary">#color/transparent</item>
</style>
2) In your activity layout, put Toolbar behind content so it can be displayed in front of it:
<RelativeLayout
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:orientation="vertical">
<fragment
android:id="#+id/container"
android:name="com.test.CustomFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</RelativeLayout>
3)
Apply the transparent theme to your acivity in AndroidManifest.xml
<activity
android:name="com.test.TransparentActivity"
android:parentActivityName="com.test.HomeActivity"
android:theme="#style/AppTheme.Overlay" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.test.HomeActivity" />
</activity>
I know am late for the party. I've created a simple class to manage the Toolbar transparency.
import android.annotation.SuppressLint;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.Toolbar;
public class TransparentToolbarManager {
private Toolbar mToolbar;
private ColorDrawable colorDrawable;
public static final int MAX_ALPHA = 255, MIN_ALPHA = 0;
public TransparentToolbarManager(Toolbar mToolbar) {
this.mToolbar = mToolbar;
this.colorDrawable = new ColorDrawable(mToolbar.getContext().getResources().getColor(R.color.colorPrimary));
}
public TransparentToolbarManager(Toolbar mToolbar, ColorDrawable colorDrawable) {
this.mToolbar = mToolbar;
this.colorDrawable = colorDrawable;
}
//Fading toolbar
public void manageFadingToolbar(int scrollDistance) {
if (mToolbar != null && colorDrawable != null) {
//FadeinAndOut according to the horizontal scrollValue
if (scrollDistance <= MAX_ALPHA && scrollDistance >= MIN_ALPHA) {
setToolbarAlpha(scrollDistance);
} else if (scrollDistance > MAX_ALPHA) {
setToolbarAlpha(MAX_ALPHA);
}
}
}
#SuppressLint("NewApi")
public void setToolbarAlpha(int i) {
colorDrawable.setAlpha(i);
if (CommonHelper.isSupport(16)) {
mToolbar.setBackground(colorDrawable);
} else {
mToolbar.setBackgroundDrawable(colorDrawable);
}
}
}
and the CommonHelper.isSupport()
public static boolean isSupport(int apiLevel) {
return Build.VERSION.SDK_INT >= apiLevel;
}
Just Add android:background="#10000000" in your AppBarLayout tag It works
https://stackoverflow.com/a/37672153/2914140 helped me.
I made this layout for an activity:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent" <- Add transparent color in AppBarLayout.
android:theme="#style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:theme="#style/ToolbarTheme"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:theme="#style/ToolbarTheme"
/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
<- Remove app:layout_behavior=...
/>
</android.support.design.widget.CoordinatorLayout>
If this doesn't work, in onCreate() of the activity write (where toolbar is #+id/toolbar):
toolbar.background.alpha = 0
If you want to set a semi-transparent color (like #30ff00ff), then set toolbar.setBackgroundColor(color). Or even set a background color of AppBarLayout.
In my case styles of AppBarLayout and Toolbar didn't play role.
try below code
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.Transparent"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
style.xml
<style name="AppTheme.Transparent" parent="ThemeOverlay.AppCompat.Light">
<item name="colorPrimary">#android:color/transparent</item>
<item name="colorControlActivated">#color/colorWhite</item>
<item name="colorControlNormal">#color/colorWhite</item>
</style>
for Support Toolbar v7 android.support.v7.widget.Toolbar:
code
toolbar.setBackground(null);
// or
toolbar.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
xml (android:background="#null" or android:background="#android:color/transparent")
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#null"
android:minHeight="?attr/actionBarSize"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:ellipsize="start"
android:singleLine="true"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
</android.support.v7.widget.Toolbar>
if Title is invisible, set textColor
Add below code to styles.xml file
<style name="LayoutPageTheme" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
<item name="android:actionBarStyle">#style/TransparentActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="TransparentActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#android:color/transparent</item>
</style>
<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" />
Don't forget to write: android:background="#android:color/transparent"
Go to res package and open color.xml.
Set color primary to #00000000.

SwitchCompat color on Lollipop

I have a SwitchCompat in the action bar (v7.Toolbar) of my activity. I want it be colorStructure when off and colorAccent when on. It works fine on pre-Lollipop, but on Lollipop the activated switch is drawn in a color which I did not define anywhere. It looks like "material_deep_teal_200". Thats my only style definitions:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Default" 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:windowNoTitle">true</item>-->
<item name="windowActionBar">false</item>
<item name="colorSwitchThumbNormal">#color/colorStructure</item>
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="colorControlActivated">#color/colorAccent</item>
<item name="colorControlHighlight">#color/colorAccent</item>
<!--<item name="selectBarStyle">#style/DefaultSelectBar</item>-->
</style>
</resources>
And that's the begin of the activity xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:theme="#style/Widget.AppCompat.Toolbar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:background="#color/colorPrimary"
android:paddingRight="#dimen/abc_dropdownitem_text_padding_right">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="#dimen/abc_action_bar_icon_vertical_padding_material">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/helpButton"
android:src="#drawable/ic_help_large"
android:background="#00000000"
android:layout_gravity="center"/>
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/master_switch"
android:clickable="true"
android:checked="true"
android:gravity="center"
android:layout_gravity="right"
/>
</FrameLayout>
</android.support.v7.widget.Toolbar>
So what do I need to change to make the switch have the same custom colors on KitKat and Lollipop?
Thanks
Actually the code as presented above works fine - also on Lollipop. I must have done a mistake during validation after cleaning up the problematic code.
Originally the problem was when I also had a configuration in values-v21. I assume that there was a typo or something like that which got lost when I was simplifying all into the default xml file.
So in short, the code as presented works, and I assume that having configurations in values-v21 would not cause a problem either.

How to make Toolbar transparent?

It is self Q&A post
I have transparent ActionBar which overlays layout. After migration to the latest support library I have been forced to get rid off the ActionBar in favor of the Toolbar. The old ways to make it transparent and overlay that layout doesn't work anymore.
<style name="CustomActionBarTheme" parent="#android:style/Theme.AppCompat">
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:actionBarStyle">#style/TransparentActionBar</item>
</style>
<style name="TransparentActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#android:color/transparent</item>
</style>
Create your toolbar.xml file with background of AppBarLayout is #null
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:id="#+id/general_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Login"
android:textSize="20sp"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
and here is result:
All you need to is to define theme which hide action bar, define action bar style with transparent background and set this style to the toolbar widget. Please note that toolbar should be drawen as last view (over all view tree)
<style name="Theme.Custom" parent="#android:style/Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="CustomActionBar" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar should be above content-->
<include layout="#layout/toolbar" />
</RelativeLayout>
Toolbar layout:
<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"
app:theme="#style/CustomActionBar"/>
Checking Google's example Source Code I found out how to make the toolbar completely transparent. It was simpler than I thought. We just have to create a simple Shape drawable like this.
The name of the drawable is toolbar_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:type="linear" />
</shape>
And then in the fragment or activity.. Add the toolbar like this.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/toolbar_bg"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
And here we will have a fully transparent toolbar.
Don't add the <android.support.design.widget.AppBarLayout > if you do, this won't work.
Note: If you need the AppBarLayout, set the elevation to 0 so it doesn't draw its shadow.
The Toolbar works like a View, so the answer it's very simple.
toolbar.getBackground().setAlpha(0);
Only this worked for me (AndroidX support library):
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:theme="#style/AppTheme.AppBarOverlay"
android:translationZ="0.1dp"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#null"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
This code removes background in all necessary views and also removes shadow from AppBarLayout (which was a problem)
Answer was found here: remove shadow below AppBarLayout widget android
Add the following line in style.xml
<style name="Base.Theme.AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme" parent="Base.Theme.AppTheme">
</style>
Now add the following line in style-v21,
<style name="AppTheme" parent="Base.Theme.AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
and set the theme as
android:theme="#style/AppTheme"
It will make the status bar transparent.
Just add android:background="#android:color/transparent" like below in your appbar layout
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>`
Just use the following xml code:
Code for the layout:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/def_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/toolbarTransparent"
android:layout_alignParentTop="true" />
And add the following code in the colors.xml:
<color name="toolbarTransparent">#00FFFFFF</color>
This will give you the desired output.
Perhaps you need to take a look at this post transparent Actionbar with AppCompat-v7 21
Points that the post suggest are
Just ensure that you use RelativeLayout to lay Toolbar and the body.
Put the Toolbar in the last.
Put transparent color for android:background attribute of the Toolbar
This should solve the problem without too much hassle.
The simplest way to put a Toolbar transparent is to define a opacity in #colors section, define a TransparentTheme in #styles section and then put these defines in your toolbar.
#colors.xml
<color name="actionbar_opacity">#33000000</color>
#styles.xml
<style name="TransparentToolbar" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
#activity_main.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:background="#color/actionbar_opacity"
app:theme="#style/TransparentToolbar"
android:layout_height="?attr/actionBarSize"/>
That's the result:
Screenshot transparent toolbar
I implemented translucent Toolbar by creating two Theme.AppCompat.Light.NoActionBar themes and setting colorPrimary attribute to transparent color.
1) Create one theme for opaque Toolbar:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Toolbar background color -->
<item name="colorPrimary">#ff212cff</item>
</style>
Second theme for transparent/overlay Toolbar:
<style name="AppTheme.Overlay" parent="AppTheme">
<item name="colorPrimary">#color/transparent</item>
</style>
2) In your activity layout, put Toolbar behind content so it can be displayed in front of it:
<RelativeLayout
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:orientation="vertical">
<fragment
android:id="#+id/container"
android:name="com.test.CustomFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</RelativeLayout>
3)
Apply the transparent theme to your acivity in AndroidManifest.xml
<activity
android:name="com.test.TransparentActivity"
android:parentActivityName="com.test.HomeActivity"
android:theme="#style/AppTheme.Overlay" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.test.HomeActivity" />
</activity>
I know am late for the party. I've created a simple class to manage the Toolbar transparency.
import android.annotation.SuppressLint;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.Toolbar;
public class TransparentToolbarManager {
private Toolbar mToolbar;
private ColorDrawable colorDrawable;
public static final int MAX_ALPHA = 255, MIN_ALPHA = 0;
public TransparentToolbarManager(Toolbar mToolbar) {
this.mToolbar = mToolbar;
this.colorDrawable = new ColorDrawable(mToolbar.getContext().getResources().getColor(R.color.colorPrimary));
}
public TransparentToolbarManager(Toolbar mToolbar, ColorDrawable colorDrawable) {
this.mToolbar = mToolbar;
this.colorDrawable = colorDrawable;
}
//Fading toolbar
public void manageFadingToolbar(int scrollDistance) {
if (mToolbar != null && colorDrawable != null) {
//FadeinAndOut according to the horizontal scrollValue
if (scrollDistance <= MAX_ALPHA && scrollDistance >= MIN_ALPHA) {
setToolbarAlpha(scrollDistance);
} else if (scrollDistance > MAX_ALPHA) {
setToolbarAlpha(MAX_ALPHA);
}
}
}
#SuppressLint("NewApi")
public void setToolbarAlpha(int i) {
colorDrawable.setAlpha(i);
if (CommonHelper.isSupport(16)) {
mToolbar.setBackground(colorDrawable);
} else {
mToolbar.setBackgroundDrawable(colorDrawable);
}
}
}
and the CommonHelper.isSupport()
public static boolean isSupport(int apiLevel) {
return Build.VERSION.SDK_INT >= apiLevel;
}
Just Add android:background="#10000000" in your AppBarLayout tag It works
https://stackoverflow.com/a/37672153/2914140 helped me.
I made this layout for an activity:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent" <- Add transparent color in AppBarLayout.
android:theme="#style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:theme="#style/ToolbarTheme"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:theme="#style/ToolbarTheme"
/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
<- Remove app:layout_behavior=...
/>
</android.support.design.widget.CoordinatorLayout>
If this doesn't work, in onCreate() of the activity write (where toolbar is #+id/toolbar):
toolbar.background.alpha = 0
If you want to set a semi-transparent color (like #30ff00ff), then set toolbar.setBackgroundColor(color). Or even set a background color of AppBarLayout.
In my case styles of AppBarLayout and Toolbar didn't play role.
try below code
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.Transparent"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
style.xml
<style name="AppTheme.Transparent" parent="ThemeOverlay.AppCompat.Light">
<item name="colorPrimary">#android:color/transparent</item>
<item name="colorControlActivated">#color/colorWhite</item>
<item name="colorControlNormal">#color/colorWhite</item>
</style>
for Support Toolbar v7 android.support.v7.widget.Toolbar:
code
toolbar.setBackground(null);
// or
toolbar.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
xml (android:background="#null" or android:background="#android:color/transparent")
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#null"
android:minHeight="?attr/actionBarSize"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:ellipsize="start"
android:singleLine="true"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
</android.support.v7.widget.Toolbar>
if Title is invisible, set textColor
Add below code to styles.xml file
<style name="LayoutPageTheme" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
<item name="android:actionBarStyle">#style/TransparentActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="TransparentActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#android:color/transparent</item>
</style>
<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" />
Don't forget to write: android:background="#android:color/transparent"
Go to res package and open color.xml.
Set color primary to #00000000.

Categories

Resources