I want the ToolBar to be transparent. I have an Activity that is showing a Toolbar and looks like this:
I have this on my xml:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
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="#android:color/transparent" />
</android.support.design.widget.AppBarLayout>
But I can't make my Activity be on top and be visible at the back of my ToolBar...
How can I achieve this?
StackOverflow does not allow me to comment because I don't have 50 reputation that why I am commenting in answer box
You can find you answer here:
How to make the support Toolbar background transparent?
How to make Toolbar transparent?
#RAP's answer seems work. But note that if you set the background to transparent, means that the toolbar is still clickable when user touch on it and doing some actions you don't want. Other alternative to achieve your goal is by setting its visibility to INVISIBLE:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setVisibility(View.INVISIBLE);
// to make it appear again:
toolbar.setVisibility(View.VISIBLE);
Related
I am using right to left support for my app. I have a custom arrow set to toolbar as the navigation icon. When I switch to arabic language, everything aligns to right including the toolbar custom navigation arrow. But the problem is the custom navigation icon is not flipping, that is it should be pointing to the right instead of left(default). This works fine with the default indicator if I dont specify any navigation icon. Have anyone encountered this?
Any solution?
Here is my code for the toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:elevation="0dp"
app:navigationIcon="#drawable/ic_back"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:logo="#drawable/ic_logo"
android:gravity="center_vertical|start"
>
in Activity
Toolbar toolbar = (Toolbar) findViewById(R.id.tb_venue_filter);
setSupportActionBar(toolbar);
setDisplayHomeAsUpEnabled(true);
If you are on android API 21+, you can just flip your icon by adding android:autoMirrored="true" to your vector icon or layer-list XML icon, this will alleviate the hasle of adding it to every toolbar manually, but if you want to support 21- versions you will have to go with the accepted answer.
This is a feature of the Drawable, not so much the Toolbar.
Try with:
toolbar.getNavigationIcon().setAutoMirrored(true);
I am having issues setting the title of my toolbar, which only seems to work when there is a delay.
For example a simple oncreate() method for my app 'Forecast'
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(_toolbar);
setTitle("ABCDEFG");
}
When the app is run, the toolbar has the name of my app, not 'ABCDEFG'.
However, if I put a 200 millisecond delay before setting the title..
Observable.just("ABCDEFG")
.delay(200,TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::setTitle);
My Toolbar title is correctly displayed as "ABCDEFG"
this is the same when it comes to doing anything with the Toolbar, does anyone know what is actually going on here? and how I can solve this without having to put a delay in?
Here is my toolbar xml..
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
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"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
Thanks for your help!
EDIT forgot to mention setTitle() simply calls _toolbar.setTitle(title)
If you call setSupportActionBar(Toolbar), then the Action Bar is then responsible for handling the title, therefore you need to call getSupportActionBar().setTitle("My Title"); to set a custom title.
Also check this link where toolbar.setTitle("My title"); may cause problem like below:- In android app Toolbar.setTitle method has no effect – application name is shown as title
And toolbar is the general form of action bar.
We can have multiple toolbars as layout widget but action is not.
Thus better approach is to use getSupportActionBar().setTitle("My Title");
As maRShmallow stated, I changed _toolbar.setTitle() to getSupportActionBar().setTitle()
Im dealing with a strange toolbar behavior in which if I set a logo with
getSupportActionBar().setLogo(R.drawable.logo_toolbar);
The Toolbar components (in my case the logo itself and the title) get disaligned from the standard gravity start behavior.
This is my toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/grey_900"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"/>
The toolbar has a SearchView at the right and no "home button", only title.
Graphic explanation:
1) Behavior without calling setLogo(...)
2) Behavior when calling setLogo(...)
3) What I'm trying to achieve
I've already tried playing with gravity but nothing happened.
Also, please, I know that Toolbar is a ViewGroup and I can customize it, but I'm looking for a clean code, also this should be a default behavior and but it isn't working so I would like to know if I'm doing something wrong.
How can I fix this? Thanks in advance!
getSupportActionBar().setTitle("Home");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
If you use getSupportActionBar , No need to use toolbar again.
Overview
I am trying to implement one of the Scrolling Techniques, Flexible space with overlapping content, described in Material Design.
Flexible space with overlapping content
Content can overlap the app bar.
Behavior:
The app bar’s starting position should be located behind the content.
Upon upward scroll, the app bar should scroll faster than the content,
until the content no longer overlaps it. Once anchored in place, the
app bar lifts up to allow content to scroll underneath.
https://www.google.co.in/design/spec/patterns/scrolling-techniques.html#scrolling-techniques-scrolling
Problem
However, the problem is,
the title in my AppBar scrolls down when expanded and hides below the overlapping content.
Here, my toolbar is hidden below the overlapping CardView.
When the appbar is collapsed, the toolbar and hence the Title slides up from below.
Code
Here's my Code:
activity-main.xml
<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="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
android:background="?attr/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
...
I have also added these in my MainActivity's onCreate function
setSupportActionBar(toolbar);
collapsingToolbarLayout.setTitle("App Name");
I want the toolbar(with the tile and the other contents, which I will add later) to stay at the top irrespective of the appbar being expanded or collapsed.
I have read the documentations, gone through many posts and tutorials, watched a lot of videos but failed to find a working solution or any related solutions at all.
If anyone has some idea on how to fix this, please suggest. Thanks for helping.
I was looking for a solution myself when I found the answer in the comments on a similar issue report.
Basically you call setTitleEnabled() on your CollapsingToolbarLayout like this:
CollapsingToolbarLayout.setTitleEnabled(false);
You can do this in xml as well, by adding this to your CollapsingToolbarLayout:
app:titleEnabled="false"
By setting it to false, you'll get the desired behaviour. The title stays fixed to the top of the screen.
The Toolbar itself was actually already at the top, but this makes the title stay there as well, instead of translating between the bottom of the CollapsingToolbarLayout and the Toolbar.
I have acheived this by adding below code inside Toolbar tag.
app:layout_collapseMode="pin"
In my case I needed to add app:titleEnabled="false" to the CollapsingToolbarLayout AND app:layout_collapseMode="pin" to the android.support.v7.widget.Toolbar
Now the toolbar stays pinned to the top of the screen, irrespective of whether the user scrolls up or down.
To keep title at top, simple put this attribute to your CollapsingToolbarLayout:
app:expandedTitleGravity="top"
I'm using the AppCompat toolbar, and for the most part its great. However, dealing with the title has been a nightmare. It's set to the app name by default, and I cannot find a way to hide it.
I was trying to hide it so that I can add my own textview so that I could properly align the title text to the top currently its left-aligned but vertically centered. I could not figure out how to position it where it would be normally if I didn't make my toolbar height longer than usual.
How have you guys been managing the toolbar? How can I align the title text to be at the top position where the title's normally are?
Edit: I've added a screenshot of what it looks like. I'm trying to keep my project private for now so I erased any identifying elements. But notice that the D (which is the title) is not aligned to the top.
If you have set your toolbar as action bar setSupportActionBar(toolbar);
then you can sipmly disable the title by using
getSupportActionBar().setDisplayShowTitleEnabled(false);
and add a textview as your Title in your toolbar layout as toolbar is also a view. See following
<LinearLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/material_blue"
android:elevation="5dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:theme="#style/ToolbarCustomIconColor" >
<TextView
android:id="#+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="#string/app_name"
android:textColor="#android:color/white" />
</android.support.v7.widget.Toolbar>
</LinearLayout>
This way you can arrange your title anywhere and customize accordingly.
You are using ToolBar from support v7 AppCompat
after that set that toolbar as action bar and then set action bar title for simple title or you want to set your custom layout inside toolbar then same as we are doing in action bar hence.. now your toolbar work as a actionbar..
See below example from ActionBarActivity..
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Your Title");
Now if you do anything with action bar related methods it is ultimately affected in ToolBar.
to change Toolbar title try this
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Text");
for custom layout for Toolbar try this
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar); //replace with your custom toolbar title