I am using Toolbar instead of actionabar in my appliaction.The toolbar works fine but the problem is that i want the app logo to appear in the center of the toolbar.The image will be in center if no menu items are displayed on the toolbar, but if i add search or refresh menu item to the toolbar the image also shifts.I want the toolbar to be in center always
Code
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/pink"
android:minHeight="56dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp">
<RelativeLayout
android:id="#+id/rl_toolbar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/tv_home_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/image"
android:drawablePadding="10dp"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Using a bitmap drawable with center gravity as Toolbar background helps you create the required effect. e.g.,
Let's assume your image to be centered is centeree.png, and that will be wrapped inside drawable/toolbar_background.xml
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#android:drawable/centeree"
android:gravity="center" />
You can assign toolbar_background.xml as a background to toolbar in your layout
<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="?attr/actionBarSize"
android:background="#drawable/toolbar_background">
Also as an alternative, you can use a 9 patch drawable with a equal stretchable area on both sides.
You can not do it because of
One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
A easy way to do it, although its probably not the right way to do it.
I set my right side padding to the same width as my hamburger icon. Then everything else what I wanted in my dimens.xml. May need to be adjusted if you are adding action items on the right.
#dimen/app_bar_padding_right = 72dp
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="#dimen/app_bar_padding"
android:paddingBottom="#dimen/app_bar_padding"
android:paddingLeft="#dimen/app_bar_padding"
android:paddingRight="#dimen/app_bar_padding_right"
android:src="#drawable/nav_bar_logo"
android:contentDescription="#string/app_name" />
Add these attributes to the ImageView...
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
Related
Lets take a look at what i would like to achieve first:
So the yellow area will be for text. it should not move. it should always be in a static position pinned. The toolbar should likewise also be static and always pinned. But the blue part will have an image in it. It should shrink when scrolled and parallax. so if user scrolls up the image should scroll up and eventually disappear with parallax. I am having trouble getting the yellow area to not move.
if you need to know, below the blue area is a scrollView.
Just so you know, the yellow bar is actually a textview with match parent and yellow background, but it can be any view i suppose. Lets take a look at what i have done so far:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="#color/white"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:layout_collapseMode="pin"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/white"
app:title="SHOPPING BAG"
android:elevation="4dp"/>
<RelativeLayout
android:layout_width="match_parent"
app:layout_scrollFlags="exitUntilCollapsed"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_tax_rebate_msg"
fontPath="fonts/Medium-Extd.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bright_yellow"
android:lineSpacingExtra="6sp"
android:padding="15dp"
android:text="#string/tax_rebate_disclaimer"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="10sp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:layout_below="#id/tv_msg"
app:layout_collapseMode="parallax"
android:orientation="vertical"/>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
//... rest if it is a scrollview, not important
so the problem im having with this again, is that the yellow area is moving when i scroll. How do i make it so only the blue area shrinks when user scrolls ?the yellow area will have important info and should never be unseen by the user.
You can use Coordinator Layout Behaviors to achieve what you are looking for.
The basic idea is rather than setting the coordinator layout to do a default behavior for everything, you can fine grain every view by creating a custom behavior. In this way you can do some fancy things.
I'm using a toolbar which works pretty well for the most part until I started adding widgets inside it. I have a checkbox and button declared inside the toolbar but would like them to appear at the far end of the toolbar and working towards the title. I.e. the title remains on the left starting from the margin, but icons are added from the right. how do I go about this? I tried changing layout direction, but it just appeared Arabic like..
As you have not shown your code, supposing that your toolbar.xml is somewhat like this.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/accent_color"
android:minHeight="?attr/actionBarSize"
android:layout_alignParentTop="true"
tools:context=".MyActivity"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/save"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="right" //might be the missing part
android:textColor="#color/white"
android:text="SAVE"/>
</android.support.v7.widget.Toolbar>
all you have to do is add gravity
android:layout_gravity="right"
You can use RelativeLayout as a child layout in toolbar and add icons in it then you can give alignment(left,right etc) to those icons
I'm trying to have a transparent toolbar,
such that it will be shown on top of an ImageView which is set as follow:
<ImageView
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="8"
android:scaleType="center"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:elevation="0dp"
/>
Illustration (blue area is an ImageView):
so, it is transparent, but it pushes the ImageView down, instead of being on top of it.
I also tried using android:elevation, but it didn't help.
Use frame layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
// Your imageview
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
//your toolbar
</FrameLayout>
This should help
Toolbar is just ViewGroup. So consider making layout like Relative layout.
In Relative layout, u have such order : the lower position in layout code, then this view would be considered as highest layer of layout. So put your views in correct order and you will achieve desired result. Worked for me many times before.
I want to implement a search view by adding a EditText to toolbar.
Here is my layout,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:clickable="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<EditText
android:id="#+id/search_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="search"/>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/place_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
, and my activity extends from ActionBarActivity.
As Google documented
One or more custom views. The application may add arbitrary child
views to the Toolbar. They will appear at this position within the
layout. If a child view's Toolbar.LayoutParams indicates a Gravity
value of CENTER_HORIZONTAL the view will attempt to center within the
available space remaining in the Toolbar after all other elements have
been measured.
I set the layout_gravity of EditText as center_horizontal. But it turns out that the EditText flows to the next row.
Is it possible to float the textedit to the right of the arrow icon?
By default, eg. having Toolbar and some other Views in FrameLayout container causes Toolbar to appear on top of other views, no matter what their (views) order is.
Is it possible to force some of the views (eg. floating action buttons) to appear in front of Toolbar or should look for workaround?
edit:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar"/>
<!-- some RecyclerView goes here also -->
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:src="#drawable/ic_content_add"
fab:fab_colorNormal="#color/accentColor"
fab:fab_colorPressed="#color/accentColor"
android:layout_marginRight="20dp"
/>
</FrameLayout>
Result:
Problem: The oval button appears in front of RecyclerView, but behind Toolbar.
The floating action button has to have equal or higher elevation as the toolbar. Assuming your toolbar has elevation set to 8dp, the FAB needs to have the same at least.
Basically widgets with higher elevation move physically above and stop respecting the XML order.