Rosita Clerissa Serrao
Rosita Clerissa Serrao

Reputation: 269

Relative layout : align view horizontal and vertical center to other views

I want to create a layout as below enter image description here Where the dot is center aligned with the text above. And a line is present between two dots

I have tried this

<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Place Order" />

        <TextView android:text="Go to Collect"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView android:text="Collect Order"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView android:src="@drawable/dot_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView android:src="@drawable/line_one"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView android:src="@drawable/dot_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView android:src="@drawable/line_two"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView android:src="@drawable/dot_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

This is making the dots start exactly as the text begins.And not center aligned enter image description here

I want to make the dots center align with the text. Tried the same with RelativeLayout. But again I am facing the same issue

Upvotes: 0

Views: 950

Answers (2)

Oliver Metz
Oliver Metz

Reputation: 3748

Best way is to use the ConstraintLayout.

Add it to your app module dependencies:

dependencies {
    [...]
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}

Then create e.g. LinearLayout for text parts elements each with a weight of 1. Then for the lower part use a ContraintLayout with Guidelines at 33% and 66% and put the lines between the points using contraints. Your code should then look somewhat like this:

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Place Order" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Go to Collect" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Collect Order" />
</LinearLayout>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <android.support.constraint.Guideline
        android:id="@+id/guideline33"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.33" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline66"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.66" />

    <ImageView
        android:id="@+id/dot1"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:src="@drawable/circle_orange"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="@+id/guideline33" />

    <ImageView
        android:id="@+id/dot2"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:src="@drawable/circle_orange"
        app:layout_constraintLeft_toLeftOf="@+id/guideline33"
        app:layout_constraintRight_toRightOf="@+id/guideline66" />

    <ImageView
        android:id="@+id/dot3"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:src="@drawable/circle_orange"
        app:layout_constraintLeft_toLeftOf="@+id/guideline66"
        app:layout_constraintRight_toRightOf="parent" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/square_orange"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/dot1"
        app:layout_constraintRight_toLeftOf="@id/dot2"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/square_orange"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/dot2"
        app:layout_constraintRight_toLeftOf="@id/dot3"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

where the Dot and Square are shape drawables:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FF9800" />
</shape>

and

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/hek_orange" />
</shape>

This gives you this:

enter image description here

Play with the margins at your own discretion to create enough space between top and lower part and dots and lines.

Upvotes: 1

AIMIN PAN
AIMIN PAN

Reputation: 1675

you should use ConstraintLayout. in ConstraintLayout, you specify dot as

constraint_leftToLeftOf = R.id.text
constraint_rightToRightOf = R.id.text

then it is aligned to the center of the text.

ConstraintLayout: https://developer.android.com/training/constraint-layout/

Upvotes: 0

Related Questions