Home > other >  How to align 2 image view in a row?
How to align 2 image view in a row?

Time:11-15

this is my xml code i want to set two images in row

XML code

<?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:padding="10dp"
    android:background="#ffffff"
    tools:context=".SplashActivity">

    <ImageView
        android:layout_width="340dp"
        android:layout_height="340dp"
        android:layout_gravity="left"
        android:src="@drawable/ic_2"
        android:text="Hello World!"
        android:tint="#ff0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        tools:layout_editor_absoluteX="-52dp" />

    <ImageView
        android:layout_width="340dp"
        android:layout_height="340dp"
        android:layout_gravity="right"
        android:src="@drawable/ic_karo"
        android:text="Hello World!"
        android:tint="#0000b3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        tools:layout_editor_absoluteX="123dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

in android studio preview it's looking perfect as i want but when i install apk in device it's not working

Android Studio Preview

this is my device preview

Device Preview

CodePudding user response:

You should use a Linera Layout instead Constraint Layout or Linear Leyout within parent Constraint Layout. You can directly use the following code:

<?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:padding="10dp"
    android:background="#ffffff"
    tools:context=".SplashActivity">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="340dp"
            android:layout_height="340dp"
            android:layout_gravity="left"
            android:src="@drawable/ic_2"
            android:text="Hello World!"
            android:tint="#ff0000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.498"
            tools:layout_editor_absoluteX="-52dp" />

        <ImageView
            android:layout_width="340dp"
            android:layout_height="340dp"
            android:layout_gravity="right"
            android:src="@drawable/ic_karo"
            android:text="Hello World!"
            android:tint="#0000b3"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.498"
            tools:layout_editor_absoluteX="123dp" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

As I mentioned in the comment add layout_gravity as centre in both the ImageView. I have also edited the code as you can not use text in Image View.

<?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="#ffffff">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="10dp"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="340dp"
        android:layout_height="340dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_2"/>

    <ImageView
        android:layout_width="340dp"
        android:layout_height="340dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_karo"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Hope this will work perfectly for you.

  • Related