title: how to centre elements in a constraint layout android
body: <p>I want to centre the elements in the constraint layout. I have added a new textview <code>Turns charger...</code> which is kept bellow the <code>sleep when inactive</code> text view and that causes it to expand and empty gap at the top of the card view</p>
  <p>Is there a way I can centre all the elements inside?</p>
  <pre><code>&lt;androidx.cardview.widget.CardView
              android:id=&quot;@+id/cvTapToWake&quot;
              style=&quot;@style/Tile&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:visibility=&quot;visible&quot;
              android:layout_marginHorizontal=&quot;24dp&quot;
              android:clipChildren=&quot;false&quot;
              android:clipToPadding=&quot;false&quot;
              app:layout_constraintEnd_toEndOf=&quot;parent&quot;
              app:layout_constraintStart_toStartOf=&quot;parent&quot;
              app:layout_constraintTop_toBottomOf=&quot;@+id/cvLockCharger&quot;&gt;
  
              &lt;androidx.constraintlayout.widget.ConstraintLayout
                  android:id=&quot;@+id/tapToWake&quot;
                  style=&quot;@style/Tile&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:background=&quot;@color/white_screen_bg&quot;
                  android:clipChildren=&quot;false&quot;
                  android:clipToPadding=&quot;false&quot;
  
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  app:layout_constraintTop_toBottomOf=&quot;@+id/lockChargerButtons&quot;
                  app:layout_goneMarginTop=&quot;32dp&quot;&gt;
  
                  &lt;ImageView
                      android:id=&quot;@+id/ivTapToAwake&quot;
                      android:layout_width=&quot;32dp&quot;
                      android:layout_height=&quot;32dp&quot;
                      android:layout_marginVertical=&quot;26dp&quot;
                      android:layout_marginStart=&quot;24dp&quot;
                      android:src=&quot;@drawable/ic_wake&quot;
                      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                      app:layout_constraintStart_toStartOf=&quot;parent&quot;
                      app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
                  &lt;TextView
                      android:id=&quot;@+id/tvTapToWake&quot;
                      style=&quot;@style/Text.Medium.Bold&quot;
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;match_parent&quot;
                      android:layout_marginStart=&quot;@dimen/text_margin&quot;
                      android:text=&quot;@string/tap_to_wake&quot;
                      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                      app:layout_constraintStart_toEndOf=&quot;@+id/ivTapToAwake&quot;
                      app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
                  &lt;androidx.appcompat.widget.SwitchCompat
                      android:id=&quot;@+id/tapToAwakeSwitch&quot;
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_alignParentEnd=&quot;true&quot;
                      android:layout_marginEnd=&quot;16dp&quot;
                      android:theme=&quot;@style/SwitchCompatTheme&quot;
                      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                      app:layout_constraintTop_toTopOf=&quot;parent&quot;
                      tools:checked=&quot;true&quot; /&gt;
  
                  &lt;TextView
                      android:layout_width=&quot;0dp&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_marginTop=&quot;8dp&quot;
                      android:paddingBottom=&quot;16dp&quot;
                      android:text=&quot;@string/turn_off_screen&quot;
                      app:layout_constraintStart_toStartOf=&quot;@+id/tvTapToWake&quot;
                      app:layout_constraintEnd_toEndOf=&quot;@+id/tapToAwakeSwitch&quot;
                      app:layout_constraintTop_toBottomOf=&quot;@+id/tvTapToWake&quot; /&gt;
  
              &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
          &lt;/androidx.cardview.widget.CardView&gt;
  </code></pre>
  <p>this is how it looks now with blank space at the top</p>
  <p><a href="https://i.stack.imgur.com/CzbFN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CzbFN.png" alt="enter image description here" /></a></p>
  <p>Could you please suggest how can I remove the empty space at the top and centre all the elements please</p>
  <p>thanks
  R</p>