androidxmlandroid-layoutandroid-studioandroid-designer

Design Showing Perfectly in Android Studio Preview but Not in Real Device


I am working on an Android Application and I am trying to implement Design Layout in Android Studio like Image in Android Studio Preview

But When I run It on Real Device It is showing layout like Design In Real Device.

In the Android Studio I used Nexus 4 to design the Layout and my Phone Resolution is 720x1280. Here is my xml code

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.usmanali.childsafety.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="275sp"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="3dp">

    <ImageView
        android:id="@+id/imgschool"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/im"
        android:contentDescription="@string/todo" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="115sp"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="278dp">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/download" />
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="110sp"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="393dp"
    tools:ignore="MissingConstraints">

    <Button
        android:id="@+id/btnlogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnregister"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="@string/login"
        android:textStyle="bold"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="291dp" />

    <Button
        android:id="@+id/btnregister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="@string/sign_up"
        android:textStyle="bold"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="340dp" />
</RelativeLayout>


Solution

  • Try this

    <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"
        tools:context=".MainActivity">
    
    
        <LinearLayout
            android:id="@+id/mainLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="275sp"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="3dp">
    
            <ImageView
                android:id="@+id/imgschool"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/im"
                android:contentDescription="@string/todo" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="115sp"
           app:layout_constraintTop_toBottomOf="@+id/mainLinearLayout"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="278dp">
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/download" />
        </LinearLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="110sp"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="393dp"
            tools:ignore="MissingConstraints">
    
            <Button
                android:id="@+id/btnlogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/btnregister"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:text="@string/login"
                android:textStyle="bold"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="291dp" />
    
            <Button
                android:id="@+id/btnregister"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:text="@string/sign_up"
                android:textStyle="bold"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="340dp" />
        </RelativeLayout>
    </android.support.constraint.ConstraintLayout>