Jonny
Jonny

Reputation: 430

ConstraintLayout, best practices, banded background

I am creating a background (as in the figure), which has 3 colors divided equally (same width).

enter image description here

Today I am using the following code:

  <LinearLayout
            android:layout_marginLeft="@dimen/default_margin"
            android:layout_marginRight="@dimen/default_margin"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="4dp">

        <View 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="1" 
            android:background="@color/colorBlack"/>
        <View 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="1" 
            android:background="@color/colorGreen"/>
        <View 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="1" 
            android:background="@color/colorRed"/>
  </LinearLayout>

As you can see, I'm using the combination of LinearLayout and three Views with their weight. Since I started using ConstraintLayout I am trying to reduce the amount of Views of the XML, I believe I can reduce this in question.

I've tried creating a 3px by 1px image as background, but when I use fitXY it turns a gradient.

Is there a solution that involves using a single View that I can create this background (same width and three different colors)?

Upvotes: 2

Views: 324

Answers (3)

Ashok Reddy M
Ashok Reddy M

Reputation: 330

<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_marginTop="@dimen/margin_10_dp"
android:layout_height="wrap_content">

<View
    android:id="@+id/view1"
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:background="#212121"
    app:layout_constraintEnd_toStartOf="@+id/view2"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toStartOf="parent" />

<View
    android:id="@+id/view2"
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:background="#3cff42"
    app:layout_constraintEnd_toStartOf="@+id/view3"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toEndOf="@+id/view1" />

<View
    android:id="@+id/view3"
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:background="#f93d3d"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toEndOf="@+id/view2" />

</android.support.constraint.ConstraintLayout>

Upvotes: 0

Tamir Abutbul
Tamir Abutbul

Reputation: 7661

You can use guidelines or chains, for this, I would like to use chains:

In the example below I have 3 buttons with a horizontal chain - they are all got the same width so now all left to do is to set the background color for them.

<?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">


    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:text="Button"
        android:background="@color/black"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="Button"
        android:background="@color/some"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:text="Button"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/button" />


</androidx.constraintlayout.widget.ConstraintLayout>

It looks like this :

enter image description here

For more information, you can check ConstraintLayout and Guidelines

Upvotes: 2

user10539074
user10539074

Reputation:

Check this easy solution to understand it will fillfull your requirement

<androidx.constraintlayout.widget.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="match_parent">

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:background="#000000"
        app:layout_constraintEnd_toStartOf="@id/view2"
        app:layout_constraintStart_toStartOf="parent" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:background="#00FF00"
        app:layout_constraintEnd_toStartOf="@id/view3"
        app:layout_constraintStart_toEndOf="@id/view1"
        app:layout_constraintTop_toTopOf="@id/view1" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:background="#FF0000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/view2"
        app:layout_constraintTop_toTopOf="@id/view1" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 2

Related Questions