How to vertically center the items in the Android Toolbar View - android

I have a Toolbar, and it seems no matter what I set the height to, the menu items I've added to it remain in the same spot vertically. I'm trying to make the Toolbar smaller, and because the items aren't moving, they get clipped when I make the Toolbar the height I want it to be. Is there any way to center the menu items vertically in the Toolbar?
Toolbar src:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/red">
<include
android:id="#+id/search_items"
layout="#layout/search_items"
android:visibility="gone"/>
</android.support.v7.widget.Toolbar>
Doesn't work:
setting android:gravity="center_vertical" for the Toolbar

Put it inside Linear or Relative Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/red">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"/>
<include
android:id="#+id/search_items"
layout="#layout/search_items"
android:visibility="gone"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>

Related

How do I make a Toolbar so that it stays on top of all view and layouts under it?

I currently have a Toolbar and a FrameLayout in the activity. The problem is that during runtime, the FrameLayout expands and covers the toolbar, which I do not want. How should I modify the toolbar so that it stays on top of all views under it?
The layout file :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
mapbox:layout_constraintBottom_toTopOf="#id/frameLayout"
mapbox:layout_constraintLeft_toLeftOf="parent"
mapbox:layout_constraintRight_toRightOf="parent"
mapbox:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="600dp"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent"
mapbox:layout_constraintStart_toStartOf="parent"
mapbox:layout_constraintTop_toBottomOf="#id/toolbar"
android:paddingTop="2dp">
// Map here.
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

How to center view below CollapsingToolbarLayout inside CoordinatorLayout

When having a CollapsingToolbarLayout inside CoordinatorLayout you need to use app:layout_behavior="#string/appbar_scrolling_view_behavior" so the view below the toolbar moves down the height of the toolbar, but in that case this view below the toolbar goes beyond the window itself on the bottom, thus if I want to center something in it between the toolbar and the bottom of the screen it's not really possible?
Here is my code:
<androidx.coordinatorlayout.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:orientation="vertical">
<include layout="#layout/generic_toolbar_collapsing" />
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/list_view_empty_string"
android:visibility="gone"
tools:visibility="visible" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And here a screenshot from the layout editor:
The selected view on the picture is the LinearLayout, you can see how the dots go beyond the bottom window line, so the text is centered inside the view, but not centered relative to the toolbar and the bottom of the window, is it possible to achieve this?

Removing the extra top space in CoordinatorLayout- Android

I am trying to use BottomSheetBehavior to make a layout similar to what google maps is providing. I am successful in using the BottomSheetBehavior and create slide up layout. The problem I am having now is CordinatorLayout takes extra space even when my layout is collapsed.
Below is a screenshot of my layout.
White background in my main activity layout
Pink is my collapsed layout with peekHeight
And the grey background is the one which should be transparent but takes the extra space.
My main layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<include layout="#layout/sample_coordinator" />
</FrameLayout>
CordinatorLayout with BottomSheetBehavior
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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="wrap_content"
android:background="#color/common_google_signin_btn_text_light_disabled">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:behavior_hideable="false"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#color/colorPrimary" />
</FrameLayout>
You have to use CoordinatorLayout in your activity layout.
Then in your bottom sheet layout insert these lines:
app:behavior_hideable="true"
app:behavior_peekHeight="Xdp"

Header of NavigationView not showing unless there is margin to top

I am creating an Android app which got a navigation drawer in a layout:
<?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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/navigation_select_layout"
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"
android:fitsSystemWindows="true"
app:headerLayout="#layout/navigation_drawer_header_layout"
app:menu="#menu/main_drawer_options" />
</android.support.v4.widget.DrawerLayout>
the navigation_drawer_header_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textViewButtonCloseDrawer"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="X"/>
</RelativeLayout>
The header layout is added but not showing unless I add either a top margin for the TextView or some padding to the RelativeLayout which pushes the TextView down.
Why won't it show as defined in the header layout? Why is the need to push the layout in order for it to be shown?
As understood from the comments, you got a ToolBar which hides the content instead of staying above it at all times. Check out this answer here which might solve your problem:
Display content under toolbar

Transluscent StatusBar with a AppBarLayout and Toolbar

How to have a good Toolbar when using the <item name="android:windowTranslucentStatus">true</item> flag ?
I tried to add almost every combinaison of fitSystemWindow to my view hierarchy but none is working !
Here is my layout with the best result :
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
tools:context="fr.pdegand.fullscreentoolbar.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:layout_scrollFlags="scroll|enterAlways"
app:title="Test"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<View
android:background="#FF00FF"
android:layout_height="400dp"
android:layout_width="match_parent"/>
<View
android:background="#00FF00"
android:layout_height="400dp"
android:layout_width="match_parent"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
But it has 2 problems : first, the Coordinator is using the colorPrimaryDark to fill the gap of the status bar and when the view is fully scrolled, the Toolbar doesnt disapear totally at the top of the screen.
The best result would be that the Toolbar itself stretch it's top to fill the gap so that the color behind the status bar would always match the color of the toolbar (my toolbar actually have dynamic coloring in my real app) and when the view is fully scrolled the toolbar should totally disappear at the top. And if possible without code. I know I could add some OnApplyWindowInsetsListener and manually handle the insets but I'm looking for a more generic solution that could be applied to all my screens.
Thank you for the help.

Categories

Resources