Hiren Hiren - 3 years ago 236
Android Question

Custom toolbar using ConstraintLayout in android

I am trying to create my own custom

Toolbar
for my project. Currently I am using
RelativeLayout
to create it. But I wanna avoid deep hierarchy of
Views
. So I wanna create it using
ConstrainLayout
. How can I achieve the following in
ConstraintLayout
.

enter image description here

Code for the above layout:

<RelativeLayout
android:id="@+id/customAppBarLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:paddingEnd="10dp"
android:paddingStart="10dp">

<ImageView
android:id="@+id/toolbarCloseButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
app:srcCompat="@drawable/ic_cancel" />

<TextView
android:id="@+id/toolbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"
android:layout_toEndOf="@+id/toolbarCloseButton"
android:textColor="#000"
android:textSize="18sp"
tools:text="Puzzle" />

<RelativeLayout
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true">

<ImageView
android:id="@+id/coinIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingBottom="2dp"
android:paddingRight="2dp"
app:srcCompat="@drawable/ic_single_coin"
tools:ignore="MissingPrefix" />

<TextView
android:id="@+id/prizeSize"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:background="@drawable/circular_background"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:maxEms="20"
android:maxLines="1"
android:padding="1dp"
android:scrollHorizontally="true"
android:textColor="#fff"
android:textSize="6sp"
tools:text="100" />
</RelativeLayout>
</RelativeLayout>


How this can be done in
ConstraintLayout
?

Answer Source

I wrote a simple layout. You can see my code to improve your layout. Hope it helps:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="#00ffff">

    <TextView
        android:id="@+id/tv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#0000ff"
        android:gravity="center_vertical"
        android:text="TextView"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/iv_1"
        app:layout_constraintRight_toLeftOf="@+id/iv_2"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/iv_1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@android:color/holo_red_dark"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ImageView
        android:id="@+id/iv_2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="#00ff00"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download