Sabuhi Khalili
Sabuhi Khalili

Reputation: 86

Any reason why the distance between buttons do not match in design view and on device at Android Studio?

Screenshot from Android Studio

Here is the code I am using at the layout file. I am using Material components as theme style. Here I used both material component button and button, but I think they are both converted to material button due to my theming style:

<?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"
tools:context=".MainActivity">

<Button
    android:id="@+id/createAccountBtn"
    android:layout_width="238dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="86dp"
    android:layout_marginTop="35dp"
    android:layout_marginEnd="87dp"
    android:layout_marginBottom="318dp"
    android:text="@string/create_account"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/signInBtn" />

<Button
    android:id="@+id/signInBtn"
    android:layout_width="237dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="89dp"
    android:layout_marginTop="282dp"
    android:layout_marginEnd="85dp"
    android:layout_marginBottom="14dp"
    android:text="@string/common_signin_button_text"
    app:layout_constraintBottom_toTopOf="@+id/createAccountBtn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 177

Answers (2)

Jerry
Jerry

Reputation: 406

The emulator model and the model for which you designed the app might be different!

Please check that and revert back.

Upvotes: 0

Sabuhi Khalili
Sabuhi Khalili

Reputation: 86

Apparently I need to use match_parent and wrap_content properties for layout_width and layout_height. One can see this documentation for its use with ConstraintLayout here: https://developer.android.com/training/multiscreen/screensizes

But I would suggest using LinearLayout as ConstraintLayout in my case behaved still badly because I had to define margin properties in exact dps which may not align with a certain screen size. I am adding the LinearLayout solution below which worked as I expected in portrait and landscape mode as well:

<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/createAccountBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/create_account" />

    <Button
        android:id="@+id/signInBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/common_signin_button_text"/>
</LinearLayout>

Upvotes: 0

Related Questions