1

I'm stuck with an issue and I have browsed the entire internet for a solution but nothing is working for me. I have added an imageview inside my toolbar but it's slightly towards the right. It doesn't align centrally, it's shifted a bit towards the right. When i align the imageview to the left, there's an unknown padding/gap between the imageview and the left side of the toolbar. Following is my XML code:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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"
        tools:context=".NavigationDrawerHomeScreen.BaseActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:elevation="0dp">

            <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_home_activity"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:contentInsetLeft="0dp"
                android:contentInsetStart="0dp"
                android:contentInsetStartWithNavigation="0dp"
                android:minHeight="?attr/actionBarSize"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:contentInsetStartWithNavigation="0dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/logo_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:src="@drawable/fixer_title_thick" />
                </RelativeLayout>

                <android.support.v7.widget.SearchView
                    android:id="@+id/search_home"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:showAsAction="ifRoom"
                    android:visibility="gone">

                </android.support.v7.widget.SearchView>


            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.AppBarLayout>

        <FrameLayout

            android:id="@+id/container_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/home_bg" />

        </FrameLayout>

        <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_centerInParent="true"-->
        <!--android:text="this is base" />-->

    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

<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:background="#2A2929"
    android:backgroundTint="#2A2929"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_home_customer"
    app:itemTextAppearance="@style/NavDrawerTextStyle"
    app:itemTextColor="#fff"
    app:menu="@menu/activity_home_customer_drawer">

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:clickable="true"
        android:orientation="vertical">

        <!-- any addition stuff you want in yoour footer layout -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="50dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Set Search Radius"
                android:textColor="@color/white" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:clipChildren="false">

                <com.crystal.crystalrangeseekbar.widgets.CrystalSeekbar
                    android:id="@+id/rangeSeekbar1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"

                    app:bar_color="@color/blue"
                    app:left_thumb_color="@color/blue_dark" />

                <TextView
                    android:id="@+id/textMin1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/rangeSeekbar1"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="5dp"
                    android:text="0"
                    android:textColor="@color/white"
                    android:textSize="16dp" />

                <TextView
                    android:id="@+id/textMax1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_below="@id/rangeSeekbar1"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="5dp"
                    android:text="100"
                    android:textColor="@color/white"
                    android:textSize="16dp" />

            </RelativeLayout>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="#1E88E5">

            <TextView
                android:id="@+id/logout_footer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:drawableLeft="@drawable/ic_logout_white_24dp"
                android:drawablePadding="32dp"
                android:text="Logout"
                android:textColor="#fff"
                android:textSize="18sp" />
        </RelativeLayout>
    </LinearLayout>

</android.support.design.widget.NavigationView>

The app:contentinset/android:contentinset solution works for some people but it doesnt work for me.

here's the image https://i.stack.imgur.com/G5YJo.jpg.

Tayyaba
  • 23
  • 7

1 Answers1

5

I had this same problem, and I tried a lot of things like setting

app:contentInsetStart ="0dp" ;

app:contentInsetStartWithNavigation = "0dp" ;

It didn't work. I even set them to a negative value, but it didn't work. I tried adding a right margin to my child view but it reduced the visible size of child view instead of occupying that left space. So probably child views are not allowed in this space.

So after spending some time I figured there are 2 ways to workaround this problem, one is preferred other I'd not prefer. First let me tell you the one I do not prefer:

Custom Implementation of the Up button: This problem only arises when in your Java Class for this layout you set this toolbar as your ActionBar and you

setDisplayHomeAsUpEnabled(true); 

Without this command this problem doesn't exist. So if you don't use the default back button and implement one your own by putting an imageView and providing onClick implementation, this should work fine.

Make ChildViews as rather Siblings: The other method is using the property of toolbar of it being just another view. I prefer this method because by using this method you can use the android's default up button. The only thing you need to do is - instead of defining child views inside the toolbar, make the child view and toolbar as siblings in a Linear or Relative Layout. This way you can reduce the left space only to "wrap_content" of the toolbar and utilise the left space in other child views. In my app, I even had an onClick functionality on the toolbar which can now be done by giving that functionality on the parent of both the layouts i.e. set the onClick on the LinearLayout.

Though I still don't know the exact reason why the left space exists or is there a way to disable it, but this is a pretty clean method to work around this.

Shubham
  • 175
  • 3
  • 12
  • hey !please remove ; in app:contentInsetStart ="0dp" ; because in the xml file is not support it! – hamid Jan 09 '19 at 16:00