Android Toolbar doesn't hide a title - android

I can't hide a title in my Toolbar that I set as ActionBar.
Here my activity style:
<style name="Theme.Paper" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
</style>
Layout:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<!-- some data -->
</data>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.Paper.AppBarOverlay">
<include
android:id="#+id/toolbar_layout"
layout="#layout/toolbar_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
</layout>
And Toolbar layout:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/Theme.Paper.Toolbar.PopupOverlay"
app:theme="#style/Theme.Paper.Spinner.PopupOverlay">
<Spinner
android:id="#+id/toolbar_spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:popupTheme="#style/Theme.Paper.Spinner.PopupOverlay"/>
</android.support.v7.widget.Toolbar>
</layout>
Here a code is in the onCreate method:
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_editor);
//Set ToolBar
setSupportActionBar(mBinding.toolbarLayout.toolbar);
//Up navigation
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false); // Try to hide a title
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(VectorDrawable
.getDrawable(this, R.drawable.ic_close_white_24dp));
}
But it doesn't work, a title still shows.
Where can a problem be?
ADDED
This is my debug screen:
As you can see my ActionBar is not null.

As you are using toolbar you can use:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");

Related

Android ToolBar can change back button color but not title color

I want to change my Toolbar title color to blue. It stays white. I was able to set the back button but not the title color - am I missing something?
I've tried to change it at 3 different places:
Activity
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
toolbar.setTitleTextColor(0xff00ff99);
setSupportActionBar(toolbar);
activity.xml
<android.support.v7.widget.Toolbar
android:id="#+id/detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="#style/ToolBarStyle"
android:background="#color/colorPrimaryDark"
android:titleTextColor="#android:color/holo_red_dark"
app:subtitleTextColor="#color/colorPrimaryDark"
app:layout_collapseMode="pin"
app:popupTheme="#style/ToolBarStyle" />
v21 styles.xml
<!-- ToolBar -->
<style name="ToolBarStyle" parent="Theme.AppCompat">
<item name="titleTextColor">#color/colorPrimaryDark</item>
<item name="android:titleTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorPrimary">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/colorPrimaryDark</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColor">#color/colorPrimaryDark</item>
</style>
styles.xml
<style name="ToolBarStyle" parent="Theme.AppCompat">
<item name="titleTextColor">#color/colorPrimaryDark</item>
<item name="android:titleTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorPrimary">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/colorPrimaryDark</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColor">#color/colorPrimaryDark</item>
</style>
What am I missing?
It stays white no matter what. I've even went through the 150 references to #ffffff in the whole project and nowhere is the toolbar referenced there.
targetSdk 26, running on Android 7.1.1
Create custom Toolbar like this:
toolbar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout 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="wrap_content"
android:fitsSystemWindows="false"
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:background="#android:color/backgroundColor"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/Base.Theme.AppCompat.Light.DarkActionBar">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/blue"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Include it in you layout:
<include layout="#layout/toolbar_layout"/>
Use it in your activity's onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_card);
Toolbar toolbar = findViewById(R.id.toolbar);
TextView toolbarTitle = findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbarTitle.setText("Your title");
...
}
Hope it will help.
Try to change this:
android:titleTextColor="#android:color/holo_red_dark"
to:
app:titleTextColor="#android:color/holo_red_dark"
in your activity.xml android.support.v7.widget.Toolbar section.
Ok so I have found the answer by literally stack tracing my code.
I have a PagerAdapter returning different fragments. I thought that each Activity was responsible to the Tabbar (it's a Master/Detail pattern for different tabs each) as I've had to set stuff like setSupportActionBar() which I've done in the subsequent Activities.
Stacktracing my code I've realized that there is a CollapsingToolbarLayout being find by view ID. This is where I set the bar's title. From there, I didn't find any title property but I've let auto complete lead me to setCollapsedTitleTextColor and setExpandedTitleColor.
So although the getColor() part is deprecated it's
bar.setCollapsedTitleTextColor(getResources().getColor(R.color.colorPrimaryDark));
bar.setExpandedTitleColor(getResources().getColor(R.color.colorPrimaryDark));
Still no idea why all the other places I've set it at didn't do the trick but very happy it's working now.
In Layout file use the Toolbar as Child layout of AppBarLayout
<LinearLayout 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:orientation="vertical">
<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.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
Style.xml
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Inside MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

android change actionbar to toolbar

I am trying to change Actionbar to Toolbar, I have successfully added Toolbar and removed Actionbar, but now the problem I have is that on my layout there will be a blank space above my toolbar where the Actionbar was at before. How can I move my Toolbar up so that it aligns with the top of the screen?
I changed style to:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>>
xml file:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<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:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:titleTextColor="#android:color/white"
android:background="#drawable/header_bg_02">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Please try this.i am using 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"
>
<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 >
And my Apptheme is this
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
Hope its help.:)
Did you call setSupportActionBar?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
See this for a complete guide: material-design-everywhere
First disable the ActionBar within res/styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
Include ToolBar in Activity layout file:
<LinearLayout
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:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary"/>
</LinearLayout>
In your Activity:
public class MyActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Find the toolbar view inside the activity layout
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Sets the Toolbar to act as the ActionBar for this Activity window.
// Make sure the toolbar exists in the activity and is not null
setSupportActionBar(toolbar);
}
Note:
You'll want to add android:fitsSystemWindows="true" to the parent layout of the Toolbar to ensure that the height of the activity is calculated correctly.
When using the support library, make sure that you are importing android.support.v7.widget.Toolbar and not android.widget.Toolbar.
I include this information from here. Refer this for more.
Try to put theme for that activity in AndroidMenifiest.xml file like
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/Theme.AppCompat.Light.NoActionBar" />
Other code looking fine

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.

Overlay toolbar on top of activity content

I'm using the AppCompat v21 library to create a Lollipop-like interface in my android app. So far it's working great.
Now I want to have to toolbar appear on top of my content in one of my activities.
However, I cannot get it to work. This is what I have so far:
Layout of activity:
<LinearLayout
android:id="#+id/main_parent_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="#style/myAppTheme">
<include layout="#layout/toolbar_setlist_top"/>
<ImageView
android:id="#+id/image"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
Layout of 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:id="#+id/toolbar"
app:theme="#style/MyActionBar"
app:popupTheme="#style/MyActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:layout_marginTop="0dp" />
Theme:
<resources>
<style name="myAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:activatedBackgroundIndicator">#drawable/list_selector</item>
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#color/primaryColor2</item>
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="background">#color/primaryColor2</item>
<item name="windowActionBarOverlay">true</item>
</style>
</resources>
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setlist);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
If you want to place the toolbar on top of your content you can use a FrameLayout like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main_parent_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_height="100dp"
android:layout_width="40dp"
android:background="#000"/>
</LinearLayout>
<include layout="#layout/toolbar_setlist_top"/>
</FrameLayout>
It doesn't appear that you're acquiring a reference to the toolbar view in your layout anywhere. You need to find the view first, then pass that reference to the setSupportActionBar method:
setContentView(R.layout.activity_setlist);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Assuming that when you say "Now I want to have to toolbar appear on top of my content in one of my activities" you are trying to use the Toolbar as your activity's ActionBar (instead of the old/original ActionBar) you must make your theme's parent theme "Theme.AppCompat.NoActionBar" instead of your current setting *.Light.DarkActionBar. Hope this helps.

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