skip to Main Content

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 a device, It’s not working

Android Studio Preview:

img1

This is my Device Preview:

img2

4

Answers


  1. 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>
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. i think you should try with Linear Layout

    Like this…

    <LinearLayout 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:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_gravity="center"
    android:background="#ffffff">
    
    <ImageView
        android:layout_width="340dp"
        android:layout_height="340dp"
        android:layout_gravity="center"
        android:src="@drawable/app_icon"
        android:text="Hello World!"
        android:tint="#ff0000"
        android:layout_weight="1"
        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_weight="1"
        android:layout_gravity="center"
        android:src="@drawable/app_icon"
        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>
    
    Login or Signup to reply.
  4. As I can see your question you can use Constraint layout property to set two images in row :-

    Start to Start

    End to End

    Start to End

    End to Start

    for example :-

    <?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="wrap_content"
        android:layout_gravity="center">
    
        <ImageView
            android:id="@+id/button_save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="4dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button_share"
            app:layout_constraintHorizontal_chainStyle="spread" />
    
        <ImageView
            android:id="@+id/button_share"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintStart_toEndOf="@+id/button_save"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search