-2

I want to create my Toolbar layout like below image.

enter image description here

I written below code for create this view. Please see my xml Code.

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/_40sdp"
        android:background="@color/black"
        android:padding="@dimen/_5sdp">

        <ImageView
            android:id="@+id/toolbar_default_img_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:padding="@dimen/_5sdp"
            android:src="@drawable/icon_menu" />

        <TextView
            android:id="@+id/toolbar_default_textview_title"
            style="@style/textview_normal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/toolbar_default_img_menu"
            android:layout_toStartOf="@+id/toolbar_default_img_search"
            android:text="@string/app_name"
            android:textColor="@color/white"
            android:textSize="@dimen/_15ssp" />

        <ImageView
            android:id="@+id/toolbar_default_img_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toStartOf="@+id/toolbar_default_img_filter_tag"
            android:padding="@dimen/_5sdp"
            android:src="@drawable/icon_search" />

        <ImageView
            android:id="@+id/toolbar_default_img_filter_tag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toStartOf="@+id/toolbar_default_img_filter"
            android:padding="@dimen/_5sdp"
            android:src="@drawable/icon_filter" />

        <ImageView
            android:id="@+id/toolbar_default_img_filter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:padding="@dimen/_5sdp"
            android:src="@drawable/icon_filter" />

    </RelativeLayout>

After written this code i can see my view is correct in android studio preview but when i run my project it look like below image.

enter image description here

only left and right imageview on correct position but my textview and other two imageview is not set on correct position after run this code.

May be problem with android:layout_toEndOf and android:layout_toStartOf property of RelativeLayout.

I don't know why this problem create.Thank you for help.

Onik
  • 19,396
  • 14
  • 68
  • 91
Nikunj Patel
  • 424
  • 2
  • 13

5 Answers5

3

Replace this code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/_40sdp"
    android:background="@color/black"
    android:padding="@dimen/_5sdp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical"
        android:layout_centerVertical="true"
        android:orientation="horizontal"
        >
        <ImageView
            android:id="@+id/toolbar_default_img_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/_5sdp"
            android:src="@drawable/ic_launcher_background" />

        <TextView
            android:id="@+id/toolbar_default_textview_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:maxEms="50"
            android:text="@string/app_name" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/toolbar_default_img_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:padding="@dimen/_5sdp"
            android:src="@drawable/icon_search" />

        <ImageView
            android:id="@+id/toolbar_default_img_filter_tag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:padding="@dimen/_5sdp"
            android:src="@drawable/icon_filter" />

        <ImageView
            android:id="@+id/toolbar_default_img_filter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/_5sdp"
            android:src="@drawable/icon_filter" />
    </LinearLayout>

</RelativeLayout>

Do changes as per your requirement by padding and margin to make a proper design.

Sameer Jani
  • 1,192
  • 9
  • 16
1

Try using layout_alignParentRight , layout_toLeftOf , layout_toRightOf ,layout_alignParentLeft instead of layout_alignParentEnd , layout_toStartOf , layout_toEndOf and layout_alignParentStart or use Both in the 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="match_parent">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/black"
        android:padding="5dp">

        <ImageView
            android:id="@+id/toolbar_default_img_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:padding="5dp"
            android:src="@drawable/add_btn" />

        <TextView
            android:id="@+id/toolbar_default_textview_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/toolbar_default_img_menu"
            android:layout_toLeftOf="@+id/toolbar_default_img_search"
            android:text="@string/app_name"
            android:textColor="@color/white"
            android:textSize="15sp" />

        <ImageView
            android:id="@+id/toolbar_default_img_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/toolbar_default_img_filter_tag"
            android:padding="5dp"
            android:src="@drawable/add_btn" />

        <ImageView
            android:id="@+id/toolbar_default_img_filter_tag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/toolbar_default_img_filter"
            android:padding="5dp"
            android:src="@drawable/add_btn" />

        <ImageView
            android:id="@+id/toolbar_default_img_filter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:padding="5dp"
            android:src="@drawable/add_btn" />

    </RelativeLayout>
</RelativeLayout>

Screen Shot On Device

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
  • @NikunjPatel : Try the Complete Code. I had Just Updated the Complete Code – Tomin B Azhakathu Apr 10 '18 at 10:33
  • Sorry, Bro. Check your code correctly. I'm Getting the result correctly. Check the Screen Shot. Which Device are you using for Testing? API 16 and Below Requires Start and End Values. Where 17 and Later Require Right and Left attribute Values – Tomin B Azhakathu Apr 10 '18 at 10:43
0

you need to add them as action button on the appbar

https://developer.android.com/training/appbar/actions.html

user3277530
  • 351
  • 6
  • 14
0

You need to add

android:layout_toLeftOf

along with

android:layout_toStartOf

and

android:layout_toRightOf

with

android:layout_toEndOf
0

Add

android:layout_toLeftOf:

along with

android:layout_toStartOf:

and

 android:layout_toRightOf:

with

 android:layout_toEndOf:

because you need to support lower API versions.

glagarto
  • 913
  • 8
  • 20