How to place text in the right center of the image on the layout constraint

The following is part of my entire XML constraint template.

<ImageView
            android:id="@+id/img_apn_not_set"
            style="@style/DeviceManagementImageView"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_sos"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view1" />

        <TextView
            android:id="@+id/tv_apn_not_set"
            style="@style/DeviceManagementHeaderText"
            android:text="Apn not set"
            android:layout_marginTop="5dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/img_apn_not_set"

            app:layout_constraintTop_toTopOf="@+id/img_apn_not_set" />

What I'm trying to do is get a text representation in the exact center on the right side of the image. In a linear arrangement, we achieve it mainly by gravity. Here I use marginTopto achieve the same. So I can do the same using any property. Is there something like rightOfCentreOf? thank

+4
source share
5 answers

pls add one line to the text

app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"

also remove android: layout_marginTop = "5dp"

hope this helps you

+3
source

. , . , .

<TextView
        android:id="@+id/tv_apn_not_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/app_name"
        app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"
        app:layout_constraintEnd_toEndOf="@+id/img_apn_not_set"
        app:layout_constraintHorizontal_bias="0.63"
        app:layout_constraintStart_toStartOf="@+id/img_apn_not_set"
        app:layout_constraintTop_toTopOf="@+id/img_apn_not_set" />
+2

<?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:orientation="vertical">

    <ImageView
        android:id="@+id/AmountLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBaseline_toBaselineOf="@id/Amount"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/Amount"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="NILESH"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@id/AmountLabel"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

enter image description here

+1
source
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img_apn_not_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_apn_not_set"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Apn not set"
        app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/img_apn_not_set"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
+1
source

You can use app: layout_constraintWidth_default = "wrap" and give all 4 side constraints that will be in the center

Example:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img_apn_not_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/tv_apn_not_set"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Apn not set"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintTop_toTopOf="@+id/img_apn_not_set"
        app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"
        app:layout_constraintLeft_toRightOf="@+id/img_apn_not_set"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

It will look like

enter image description here

+1
source

Source: https://habr.com/ru/post/1695153/


All Articles