Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set background when using constraint layout

I am using constraint layout as parent, and added multiple child using constraint. There are three views horizontally and I have to apply rounded corner background for two view only, which was achieved by linear layout previously. But with the help of constraint layout, I am not able to achieve this.

How can I achieve this?

enter image description here

like image 979
RAMU PAL Avatar asked Jun 30 '18 05:06

RAMU PAL


People also ask

How do you set a barrier in constraint layout?

Creating Barriers in XMLThe app:barrierDirection attribute determines the direction of the Barrier - in this case it will be positioned at the end of the referenced Views. The list of referenced Views is a comma separated list of the IDs of other Views within the layout (without the @+id/ qualifier).

Is constraint layout better than linear layout?

ConstraintLayout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout). Despite the fact that it's awesome, it fails to serve the purpose with simple UI layouts.

Is constraint layout faster than linear layout?

More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.


1 Answers

ConstraintLayout recently introduced the concept of constrainthelper which can be used to perform an action on a group of views. 'Group', which is used to toggle visibility of multiple view is subclass of this.

A constrainthelper which changes background of multiple views together is not yet a part of the stable release, but soon will be.

Until then, you can achieve a backround as in the example below, where 3 textview share a common background, using an View class:

<?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"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#AAA">

  <View
    android:id="@+id/background"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#FFF"
    app:layout_constraintBottom_toBottomOf="@+id/textView3"
    app:layout_constraintEnd_toEndOf="@+id/textView1"
    app:layout_constraintStart_toStartOf="@+id/textView1"
    app:layout_constraintTop_toTopOf="@+id/textView1" />

  <TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:padding="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="TextView" />

  <TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:padding="8dp"
    app:layout_constraintEnd_toEndOf="@+id/textView1"
    app:layout_constraintStart_toStartOf="@+id/textView1"
    app:layout_constraintTop_toBottomOf="@+id/textView1"
    tools:text="TextView" />

  <TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:padding="8dp"
    app:layout_constraintEnd_toEndOf="@+id/textView1"
    app:layout_constraintStart_toStartOf="@+id/textView1"
    app:layout_constraintTop_toBottomOf="@+id/textView2"
    tools:text="TextView" />
</android.support.constraint.ConstraintLayout>

EDIT: A nice blog on what to expect from ConstraintLayout 2.0

like image 199
Udit Avatar answered Sep 24 '22 20:09

Udit