i have created an Toolbar with navigation drawer and menu, now i'm trying to add a textView and edittext on the same toolbar and place them in the center, but the problem is when i add relative layout and set width as match parent, it leases some space on the left side and the items which are to be placed in the center are shifted right.
i've pasted the code below:-
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H E A D"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:id="#+id/ToolbarTextView"
style="#style/Base.TextAppearance.AppCompat.Large"
android:layout_marginTop="9dp"
android:textColor="#color/colorwhite"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ToolbarTextView"
android:layout_centerHorizontal="true"
android:text="Tagline"
android:textSize="15dp"
android:textColor="#color/colorwhite"/>
<EditText
android:layout_width="292dp"
android:layout_height="wrap_content"
android:background="#drawable/main_search"
android:padding="7dp"
android:backgroundTint="#color/colorwhite"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:hint="Search..."
android:id="#+id/ToolbarSearch1"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar></android.support.design.widget.AppBarLayout><include layout="#layout/content_main" /><android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
the xml code of the mainActivity:-
<android.support.constraint.ConstraintLayout
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"
tools:context="com.strease.user.strease.MainActivity"
android:id="#+id/layout4"
tools:showIn="#layout/app_bar_main">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/></android.support.constraint.ConstraintLayout>
the reference image is given below:-
Screenshot of the device is given below:-
P.S:- i need to omit the blank space and get the editText and textViews to the center of the layout(currently the are shifted towards right)
Navigation Drawer code in main Activity:-
<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
layout="#layout/app_bar_main"
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/nav_header_main"
app:menu="#menu/activity_main_drawer"/></android.support.v4.widget.DrawerLayout>
android:fitsSystemWindows="false"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
Try to add above four line in your toolbar. It will resolve your problem. And make your child layout inside width match_parent and height wrap_content. And set your sub child view alignment center to horizontally. It will resolve your problem.
If still not resolve your problem then update your XML file for your layout exactly with the included layout so we can find out what exactly problem going on with your layout.
You need to add this properties to Toolbar to remove that space.
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
Like as below.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
android:background="?attr/colorPrimary">
This will remove the default left space from Toolbar.
Here is screen shot. It seems Edit Text showing in exact center of Toolbar. Also update your minSdkVersion to 21 surely it will work.
Here is the layout code.
<android.support.design.widget.AppBarLayout 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="130dp"
android:background="#android:color/holo_red_light"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorAccent"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/ToolbarTextView"
style="#style/Base.TextAppearance.AppCompat.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp"
android:text="H E A D"
android:textColor="#android:color/white"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ToolbarTextView"
android:layout_centerHorizontal="true"
android:text="Tagline"
android:textColor="#android:color/white"
android:textSize="15dp" />
<EditText
android:id="#+id/ToolbarSearch1"
android:layout_width="292dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="#android:color/white"
android:backgroundTint="#android:color/white"
android:hint="Search..."
android:padding="7dp" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.AppBarLayout>
EDIT
Add another property to toolbar for Navigation Icon.
app:contentInsetStartWithNavigation="0dp"
Now your toolbar should be as below.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorAccent"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStartWithNavigation="0dp"
app:contentInsetStart="0dp">
EDIT 2
I have made some changes to your layout used Linear Layout instead of Relative Layout.
<android.support.design.widget.AppBarLayout 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="130dp"
android:background="#android:color/holo_red_light"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorAccent"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStartWithNavigation="0dp"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/ToolbarTextView"
style="#style/Base.TextAppearance.AppCompat.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="H E A D"
android:textColor="#android:color/white"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tagline"
android:layout_marginTop="2dp"
android:textColor="#android:color/white"
android:textSize="15dp" />
<EditText
android:id="#+id/ToolbarSearch1"
android:layout_width="292dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:layout_marginTop="3dp"
android:backgroundTint="#android:color/white"
android:hint="Search..."
android:padding="7dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.AppBarLayout>
Related
Here is the image :
I have used a textview inside the toolbar and tried to center the textview using gravity="center". I want to center the textview without using margin shifts or padding shifts or translationX.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:title="Train App"
app:collapseIcon="#mipmap/ic_launcher"
app:titleTextColor="#color/primaryTextColor"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
android:layout_width="match_parent"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/appTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Train App"
android:gravity="center"
android:textSize="20sp"
android:textColor="#color/primaryTextColor"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_mood_white_24dp"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
You can try to do something like this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:theme="#style/AppTheme.AppBarOverlay"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:drawablePadding="#dimen/bounds_m"
android:fontFamily="#font/quicksand"
android:gravity="center"
android:text="#string/appname"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white"/>
</FrameLayout>
And about ic_mood_white_24dp just go with option menu instead of putting it in toolbar.
Try this..
<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="wrap_content"
android:background="#android:color/holo_blue_light"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/appTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Train App"
android:textColor="#android:color/background_dark"
android:textSize="20sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
You have to add android:layout_centerHorizontal="true" property in your TextView.
Like :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/appTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:text="Train App"
android:textColor="#color/colorPrimary"
android:textSize="20sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
In my application im using CoordinatorLayout,AppBarLayout and CollapsingToolbarLayout because i want my app to have top menu bar pinned and header which hides on scroll. I have recyclerview where my items are listed, and items are made in CardView.
Problem is that when i add toolbar as CollapsingToolbarLayouts child at bottom of screen,under recyclerview, on my phone whitespace appears. Also whitespaces dimensions are same like toolbars. When i remove toolbar whitespace also disappears.
here is my main xml code:
<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:background="#color/GrayColour"
android:orientation="vertical"
tools:context=".MainActivity">
<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="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax">
<include
android:id="#+id/Header"
layout="#layout/header_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="53dp"/>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="pin"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp">
<include
android:id="#+id/TopBar"
layout="#layout/top_navigation_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/Categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
tools:listitem="#layout/list_item"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.design.widget.CoordinatorLayout>
and here is my xml code for items with who recyclerview is filled:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/white_background_rounded_corners"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="156dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/white_background"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/ivCategory"
android:layout_width="156dp"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:background="#null"
android:clickable="true"
android:contentDescription="#string/category"
android:focusable="true"
android:scaleType="fitCenter"
android:src="#mipmap/android_icon" />
<TextView
android:id="#+id/tvCategoryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="#string/text"
android:textColor="#color/mainColour"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView
I am new in android development.Can anyone help me how can I customise the toolbar like the below image
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delivery Location"
android:layout_gravity="center"
android:textColor="#000"
android:id="#+id/toolbar_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jiwaji Ganj"
android:layout_gravity="center"
android:textColor="#color/tabUnselectedIconColor"
android:id="#+id/toolbar_title_location" />
</LinearLayout>
I'm trying to use toolbar for my project.
Here is the code I am using:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:contentInsetLeft="0dp"
android:elevation="#dimen/margin_padding_8dp"
android:contentInsetStart="0dp">
<RelativeLayout
android:id="#+id/rlToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingRight="#dimen/margin_padding_16dp"
android:text="AppBar"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textColor="#color/white"
android:textSize="#dimen/text_size_20sp" />
</RelativeLayout>
I want to remove left margin, Here I set android:contentInsetLeft="0dp" and android:contentInsetStart="0dp" but it's not working..Please help me !
replace your xml with below xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="#dimen/margin_padding_8dp"
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp">
<RelativeLayout
android:id="#+id/rlToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingRight="#dimen/margin_padding_16dp"
android:text="AppBar"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textColor="#color/white"
android:textSize="#dimen/text_size_20sp" />
</RelativeLayout>
Use app:contentInsetStart="0dp" to remove that left space.
See the below code and here I added app:contentInsetStart="0dp". You need to add that to your code because before API 21 (i.e. Lollipop) you need to add that line.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:contentInsetStart="0dp"
app:contentInsetStart="0dp"
>
</android.support.v7.widget.Toolbar>
Referring to #calvinfly comment:
I updated my code
<RelativeLayout
android:id="#+id/rlTop"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#android:color/white" >
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="#string/titleString"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#468bac"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/rlStarsTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:layout_marginRight="2dp"
android:layout_toRightOf="#+id/toolbar_title"
android:layout_toEndOf="#+id/toolbar_title"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true">
<RatingBar
android:id="#+id/txtRatings"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="1dp"
android:gravity="center"
android:max="5"
android:rating="3.7"
android:textColor="#android:color/holo_blue_bright"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
Just add these two lines in your toolbar.xml
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
This works for me...
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app2="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app2:contentInsetStart="0dp"/>
Add below xml code to your Toolbar !
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
Add following code to your .xml file It may solve. Perfectly working solution I have tried.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/white"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp"
app:theme="#style/toolbarPopup">
above 21 use following code
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />
Use This Toolbar property for remove toolbar left space
app:contentInsetStart="0dp"
<androidx.appcompat.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="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:contentInsetStart="0dp">
</androidx.appcompat.widget.Toolbar>
Add below xml code to your Toolbar
<android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp">
I have the following structure in the layout file of my Android studio project, and I see unexplained left padding between the parent element (Toolbar) and it's immediate child element (LinearLayout).
Layout Text
<Toolbar
android:layout_width="fill_parent"
android:layout_height="600dp"
android:id="#+id/toolbar"
android:background="#313B45"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<ImageView
android:id="#+id/headerimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:layout_gravity="left|top"
android:layout_weight="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView"
android:scaleType="fitXY"
android:layout_gravity="left|top"
android:layout_weight="1"/>
</LinearLayout>
</Toolbar>
How can I remove this gap and have the child LinearLayout align fully with the parent Toolbar?
Add these lines to your toolbar layout :
For API<=21 toolbar :
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
For API 21>= toolbar :
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
The left inset is caused by Toolbar's contentInsetStart which by default is 16dp.
Here's the full code :
<android.support.v7.widget.Toolbar
android:layout_width="fill_parent"
android:layout_height="600dp"
android:id="#+id/toolbar"
android:background="#313B45"
android:weightSum="1"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/headerimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:layout_gravity="left|top"
android:layout_weight="1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView"
android:scaleType="fitXY"
android:layout_gravity="left|top"
android:layout_weight="1" />
</LinearLayout>
The above answer helped solve only a part, add these lines and it should work fine
android.support.v7.widget.Toolbar
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />
notice that android:contentInsetLeft and app:contentInsetLeft are 2 separate things and both of them are needed