Search code examples
androidxmlandroid-studiolayoutandroid-relativelayout

Layout has been misplaced while view in emulator and mobile


I'm implementing a login screen while implementing it in the Android Studio the deigning seems fine, but after building the app to emulator and mobile it seems weird, like the elements are misplaced, stretched and compressed. I'm using the drawable element in different sizes because I want this app to support multiple resolution devices.

Below image is from XML designing preview

View from xml design

Below image is a screenshot of emulator

From emulator

Below image is from Android device

from android device

here is the xml design [link][4]

What I'm doing here wrong, any suggestions would be helpful

  [4]: https://pastebin.com/d9MPen7c

Solution

  • The issue is you have not set the constraints properly. That is why it looks worse on the real devices.

    Here is the 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:orientation="vertical">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:background="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <TextView
                android:id="@+id/tv_UserName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="24dp"
                android:layout_marginTop="100dp"
                android:layout_marginEnd="24dp"
                android:layout_marginBottom="24dp"
                android:text="User Name"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <EditText
                android:id="@+id/et_UserName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="24dp"
                android:backgroundTint="#FFFFFF"
                android:ems="10"
                android:inputType="textPersonName"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tv_UserName" />
    
            <TextView
                android:id="@+id/tv_Password"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="24dp"
                android:text="Password"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/et_UserName" />
    
            <EditText
                android:id="@+id/et_Password"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="24dp"
                android:backgroundTint="#FFFFFF"
                android:ems="10"
                android:inputType="textPassword"
                app:layout_constraintBottom_toTopOf="@id/iv_google_bt"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tv_Password" />
    
            <ImageView
            android:id="@+id/iv_google_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp" 
            android:layout_marginTop="24dp" 
            android:layout_marginEnd="24dp" 
            android:layout_marginBottom="80dp"
            android:contentDescription="@null"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/et_Password" />
    
        <ImageView
            android:id="@+id/iv_fb_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp" 
            android:layout_marginTop="24dp" 
            android:layout_marginEnd="24dp" 
            android:layout_marginBottom="80dp"
            android:layout_toStartOf="@+id/iv_google_bt"
            android:contentDescription="@null"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/et_Password" />
    
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
        <ImageView
            android:id="@+id/iv_getstarted_login"
            android:layout_width="0dp"
            android:layout_height="75dp"
            android:contentDescription="@null"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
            app:layout_constraintEnd_toEndOf="@+id/constraintLayout"
            app:layout_constraintStart_toStartOf="@+id/constraintLayout"
            app:layout_constraintTop_toBottomOf="@id/constraintLayout">
    
        </ImageView>
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="176dp"
            android:layout_height="172dp"
            android:contentDescription="@null"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
            app:layout_constraintEnd_toEndOf="@+id/constraintLayout"
            app:layout_constraintStart_toStartOf="@+id/constraintLayout"
            app:layout_constraintTop_toTopOf="@+id/constraintLayout" />
    
        <TextView
            android:id="@+id/login_signup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_24dp"
            android:layout_marginBottom="16dp"
            android:gravity="center"
            android:text="Dummy text"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/iv_getstarted_login" />
    
    
        <TextView
            android:id="@+id/notamem"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_24dp"
            android:layout_marginBottom="16dp"
            android:gravity="center"
            android:text="Dummy text"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/login_signup" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    I have placed my resources like dimen, String and drawable. So, Please change you resources with mine.