android - ConstraintLayout center align image and text

Android - ConstraintLayout center align image and text

To center align an image and text vertically and horizontally within a ConstraintLayout in Android, you can use constraints along with the app:layout_constraintVertical_bias and app:layout_constraintHorizontal_bias attributes. Here's how you can achieve this:

Example XML Layout

Assume you have an ImageView and a TextView that you want to center align within a ConstraintLayout.

<?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" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- ImageView centered vertically and horizontally --> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_image" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5" app:layout_constraintHorizontal_bias="0.5" /> <!-- TextView centered vertically and horizontally --> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Centered Text" android:textSize="18sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" app:layout_constraintVertical_bias="0.5" app:layout_constraintHorizontal_bias="0.5" /> </androidx.constraintlayout.widget.ConstraintLayout> 

Explanation

  1. ImageView Constraints:

    • app:layout_constraintBottom_toBottomOf, app:layout_constraintEnd_toEndOf, app:layout_constraintStart_toStartOf, app:layout_constraintTop_toTopOf: These constraints ensure that the ImageView spans the entire parent ConstraintLayout.
    • app:layout_constraintVertical_bias="0.5": This attribute centers the ImageView vertically. A bias value of 0.5 means it's centered.
    • app:layout_constraintHorizontal_bias="0.5": This attribute centers the ImageView horizontally. Again, a bias value of 0.5 centers it horizontally.
  2. TextView Constraints:

    • app:layout_constraintBottom_toBottomOf, app:layout_constraintEnd_toEndOf, app:layout_constraintStart_toStartOf, app:layout_constraintTop_toBottomOf="@id/imageView": These constraints ensure that the TextView is below the ImageView and spans the entire parent ConstraintLayout.
    • app:layout_constraintVertical_bias="0.5": This attribute centers the TextView vertically relative to its constraints.
    • app:layout_constraintHorizontal_bias="0.5": This attribute centers the TextView horizontally relative to its constraints.

Additional Tips

  • Adjusting Bias: You can adjust the bias values (0.5 in this case) to change the alignment within the parent ConstraintLayout.
  • Margins: If you want to add margins around the ImageView or TextView, use app:layout_constraintMargin* attributes.
  • Guidelines: You can also use guidelines (app:layout_constraintGuide_*) to help with alignment if needed.

By using these constraints and bias attributes effectively, you can achieve centered alignment of both image and text within a ConstraintLayout in your Android application. Adjust the sizes and other attributes (android:layout_width, android:layout_height, etc.) as per your specific design requirements.

Examples

  1. Android ConstraintLayout center align image and text

    • Description: Center align an ImageView and TextView horizontally within a ConstraintLayout in Android.
    • Code:
      <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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintVertical_chainStyle="packed" app:layout_constraintHorizontal_bias="0.5" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 
  2. Android ConstraintLayout center align image and text vertically

    • Description: Vertically center align an ImageView and TextView within a ConstraintLayout in Android.
    • Code:
      <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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintVertical_bias="0.5" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 
  3. Android ConstraintLayout center align image and text programmatically

    • Description: Center align an ImageView and TextView programmatically within a ConstraintLayout in Android (Java/Kotlin).
    • Code (Kotlin):
      val imageView = findViewById<ImageView>(R.id.imageView) val textView = findViewById<TextView>(R.id.textView) val constraintSet = ConstraintSet() constraintSet.clone(constraintLayout) constraintSet.centerHorizontally(R.id.imageView, ConstraintSet.PARENT_ID) constraintSet.centerHorizontally(R.id.textView, ConstraintSet.PARENT_ID) constraintSet.applyTo(constraintLayout) 
  4. Android ConstraintLayout center align image and text with margins

    • Description: Center align an ImageView and TextView with specific margins within a ConstraintLayout in Android.
    • Code:
      <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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintVertical_chainStyle="packed" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintMarginStart="16dp" app:layout_constraintMarginEnd="16dp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintMarginStart="16dp" app:layout_constraintMarginEnd="16dp" /> </androidx.constraintlayout.widget.ConstraintLayout> 
  5. Android ConstraintLayout center align image and text with different sizes

    • Description: Center align an ImageView and TextView with different sizes within a ConstraintLayout in Android.
    • Code:
      <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"> <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/your_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintVertical_chainStyle="packed" app:layout_constraintHorizontal_bias="0.5" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 
  6. Android ConstraintLayout center align image and text with fixed position

    • Description: Center align an ImageView and TextView at fixed positions within a ConstraintLayout in Android.
    • Code:
      <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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintVertical_bias="0.5" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintLeft_margin="32dp" app:layout_constraintRight_margin="32dp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 
  7. Android ConstraintLayout center align image and text with guidelines

    • Description: Center align an ImageView and TextView using guidelines within a ConstraintLayout in Android.
    • Code:
      <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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintVertical_chainStyle="packed" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintGuide_begin="50dp" app:layout_constraintGuide_end="50dp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 
  8. Android ConstraintLayout center align image and text with barriers

    • Description: Center align an ImageView and TextView using barriers within a ConstraintLayout in Android.
    • Code:
      <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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintVertical_chainStyle="packed" app:layout_constraintHorizontal_bias="0.5" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintBottom_toBottomOf="parent" /> <androidx.constraintlayout.widget.Barrier android:id="@+id/barrier" android:layout_width="wrap_content" android:layout_height="wrap_content" app:barrierDirection="left" app:constraint_referenced_ids="imageView, textView" /> </androidx.constraintlayout.widget.ConstraintLayout> 
  9. Android ConstraintLayout center align image and text with chains

    • Description: Center align an ImageView and TextView using chains within a ConstraintLayout in Android.
    • Code:
      <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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintVertical_chainStyle="packed" app:layout_constraintHorizontal_bias="0.5" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintBottom_toBottomOf="parent" /> <androidx.constraintlayout.widget.Group android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" app:constraint_referenced_ids="imageView, textView" /> </androidx.constraintlayout.widget.ConstraintLayout> 
  10. Android ConstraintLayout center align image and text with bias

    • Description: Center align an ImageView and TextView with horizontal bias within a ConstraintLayout in Android.
    • Code:
      <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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintHorizontal_bias="0.5" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.5" /> </androidx.constraintlayout.widget.ConstraintLayout> 

More Tags

reusability levenshtein-distance return-type logrotate cross-domain python-imaging-library splitter core-text kubernetes-ingress onsubmit

More Programming Questions

More Fitness Calculators

More Statistics Calculators

More Pregnancy Calculators

More Various Measurements Units Calculators