How I can put icons beside some in the middle using ConstraintLayout

I tried put Icon beside some in the middle but I failed, can someone leave name attribute that can fix that problem?

XML

<?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"
    android:background="@color/SSA1Background"
    tools:context=".SplashScreen">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/txtSignInVia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:fontFamily="@font/main_font_file"
            android:text="@string/SSA1SignInVia"
            android:textColor="@color/SSA1SignInText"
            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/btnFacebook"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/s_s_a_1_facebook_btn"
            android:fontFamily="@font/icons_brands"
            android:text="@string/SSA1FacebookIcon"
            android:textColor="@color/SSA1FacebookText"
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txtSignInVia" />

        <Button
            android:id="@+id/btnGoogle"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:background="@drawable/s_s_a_1_google_btn"
            android:fontFamily="@font/icons_brands"
            android:text="@string/SSA1GoogleIcon"
            android:textColor="@color/SSA1GoogleText"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txtSignInVia" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Phone Result

enter image description here

Facebook and google icons I want them in the center.

2 answers

  • answered 2019-06-12 01:18 PampaZiya

    Constraint the ends of the views (left/right) to each others and set a horizontal chain style to packed like so:

    • Also.. no need to nest the constraint layout inside another constraint layout. Remove the unused parent Constraint Layout

      <androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent">
      
          <TextView
              android:id="@+id/txtSignInVia"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="5dp"
              android:layout_marginStart="8dp"
              android:layout_marginTop="8dp"
              android:layout_marginEnd="8dp"
              android:layout_marginBottom="8dp"
              android:fontFamily="@font/main_font_file"
              android:text="@string/SSA1SignInVia"
              android:textColor="@color/SSA1SignInText"
              android:textSize="25sp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <Button
              android:id="@+id/btnFacebook"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:layout_margin="5dp"
              android:layout_marginStart="8dp"
              android:layout_marginTop="8dp"
              android:background="@drawable/s_s_a_1_facebook_btn"
              android:fontFamily="@font/icons_brands"
              android:text="@string/SSA1FacebookIcon"
              android:textColor="@color/SSA1FacebookText"
              android:textSize="20sp"
              app:layout_constraintEnd_toStartOf="@id/btnGoogle"
              app:layout_constraintHorizontal_chainStyle="packed"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/txtSignInVia" />
      
          <Button
              android:id="@+id/btnGoogle"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:layout_margin="5dp"
              android:layout_marginTop="8dp"
              android:layout_marginEnd="8dp"
              android:background="@drawable/s_s_a_1_google_btn"
              android:fontFamily="@font/icons_brands"
              android:text="@string/SSA1GoogleIcon"
              app:layout_constraintStart_toEndOf="@id/btnFacebook"
              app:layout_constraintHorizontal_chainStyle="packed"
              android:textColor="@color/SSA1GoogleText"
              android:textSize="20sp"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/txtSignInVia" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

  • answered 2019-06-12 06:37 Rithin

    For button id btnFacebook, add

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"`
    

    For button id btnGoogle, add

    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHorizontal_bias="0.6"` 
    

    Because if all constraints are not set views will not be in the position we wanted. I hope this helps.