user2189878
user2189878

Reputation: 257

How do I divide the View below using weights and constraint layout

Desired result

I used LinearLayout to create the UI but I'm unable to centre the horizontal gray line in alignment to centre of the circle. I placed the circles and text in a vertical linear layout.

Find the code below:

<LinearLayout
        android:id="@+id/step_indicator"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:layout_margin="@dimen/margin_10"
        android:gravity="center"
        android:orientation="horizontal">

    <LinearLayout
            android:id="@+id/step1_layout"
            style="@style/StepIndividualLayout">

        <ImageView
                android:id="@+id/step1_imageView"
                style="@style/StepImageView"
                android:layout_gravity="center"
                android:src="@drawable/active"
                app:layout_constraintDimensionRatio="1:1" />

        <TextView
                android:id="@+id/step1_textView"
                style="@style/StepTextView"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Text1"
                android:textColor="@color/dark_blue_color" />
    </LinearLayout>

    <View
            android:id="@+id/step_1_2_view"
            style="@style/StepLine"></View>

    <LinearLayout
            android:id="@+id/step2_layout"
            style="@style/StepIndividualLayout">

        <ImageView
                android:id="@+id/step2_imageView"
                style="@style/StepImageView"
                android:layout_gravity="center"
                android:src="@drawable/step_2_deactivated"
                app:layout_constraintDimensionRatio="1:1" />

        <TextView
                android:id="@+id/step2_textView"
                style="@style/StepTextView"
                android:layout_gravity="center"
                android:text="Text 2" />
    </LinearLayout>

    <View
            android:id="@+id/step_2_3_view"
            style="@style/StepLine"/>
 
</LinearLayout>

The above code produces the following result:

Stepper with line bug

Upvotes: 1

Views: 467

Answers (2)

Shivam Jamaiwar
Shivam Jamaiwar

Reputation: 544

instead of doing manually, you can use this libarary to manage your step status.

OR

use the following code. just replace Imageview with your custom view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp75"
    android:layout_marginHorizontal="@dimen/dp4"
    android:gravity="center"
    android:weightSum="7">
    
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginHorizontal="@dimen/dp4"
        android:layout_weight="1"
        android:background="@color/black"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginHorizontal="@dimen/dp4"
        android:layout_weight="1"
        android:background="@color/black"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginHorizontal="@dimen/dp4"
        android:layout_weight="1"
        android:background="@color/black"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>
        
</LinearLayout>

enter image description here

Upvotes: 1

Tamir Abutbul
Tamir Abutbul

Reputation: 7661

You can use constraitnLyaout as you want to, and give your lines proper constraints so they will be centered according to your images. In addition, I think that in here chain (Horizontal) will make your life easier, for more info ConstraintLayout chains

something like this:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.2"
        app:layout_constraintHeight_percent="0.1"
        android:text="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:text="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view5"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.2" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.2"
        app:layout_constraintHeight_percent="0.1"
        android:text="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view5"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view4"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="@+id/button2" />

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="@+id/view"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/view" />

    <View
        android:id="@+id/view5"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="@+id/view"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button4"
        app:layout_constraintTop_toTopOf="@+id/view" />

</androidx.constraintlayout.widget.ConstraintLayout>

It will look like this:

enter image description here

now just replace your buttons with your custom view and you are done.

Upvotes: 1

Related Questions