Home > OS >  How do I constraint a view to the center of another view in android studio?
How do I constraint a view to the center of another view in android studio?

Time:02-11

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:id="@ id/open_explanation_button"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/open_dialog"
        app:layout_constraintEnd_toEndOf="parent"
        />
    <TextView
        android:id="@ id/movieRatingText"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:layout_weight="53"
        android:text="Rating: R"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:autoSizeMaxTextSize="100sp"
        app:autoSizeMinTextSize="12sp"
        app:autoSizeStepGranularity="2sp"
        app:autoSizeTextType="uniform"
        app:layout_constraintTop_toBottomOf="@ id/inASeriesOrNoText"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

I have a button with an id of open_explanation_button, I need to place that button to the end of the movieRatingText. The problem is that I need the movieRatingText to be with a width of match_parent to make sure that it's an autoresizable text. In a more simpler way, I need to constraint the start of the open_explanation_button to the center of the movieRatingText, in addition to adding some margins. Any help?

CodePudding user response:

You can create a dummy view and align that as the center point of movieRatingText. And use that to start of open_explanation_button

   <androidx.constraintlayout.widget.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@ id/open_explanation_button"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:background="@drawable/ic_add"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@id/centerView" />

<TextView
    android:id="@ id/movieRatingText"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_weight="53"
    android:gravity="center"
    android:text="Rating: R"
    android:textColor="@color/white"
    android:textSize="20sp"
    app:autoSizeMaxTextSize="100sp"
    app:autoSizeMinTextSize="12sp"
    app:autoSizeStepGranularity="2sp"
    app:autoSizeTextType="uniform"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/inASeriesOrNoText"
    app:layout_constraintTop_toTopOf="parent" />

<View
    android:id="@ id/centerView"
    android:layout_width="1dp"
    android:layout_height="1dp"
    app:layout_constraintEnd_toEndOf="@id/movieRatingText"
    app:layout_constraintStart_toStartOf="@id/movieRatingText"
    app:layout_constraintTop_toTopOf="@ id/movieRatingText" />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

You have to constraint end and start of your view, to its parent

  • Related