Views losing margin when activity created

  android, android-constraintlayout, java, margin, view

I have a strange behavior here

When I start this dialog (which is actually an activity with dialog Theme) the EditTexts are losing the left margin.

This is my XML

`<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/alert_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primary">

    <TextView
        android:id="@+id/user_entry_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="8dp"
        android:text="@string/input_user"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.cardview.widget.CardView
        android:id="@+id/user_edit_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:background="@color/icons"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/user_entry_hint">

        <LinearLayout
            android:id="@+id/editTextLinear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/icons"
            android:orientation="vertical">


            <EditText
                android:id="@+id/user_name_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/comfortaa_medium"
                android:hint="@string/userName"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="@color/primary_text"
                android:textColorHint="@color/primary_text"
                android:textSize="14sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@color/primary_dark" />

            <EditText
                android:id="@+id/user_address_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/comfortaa_medium"
                android:hint="@string/userAddress"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="@color/primary_text"
                android:textColorHint="@color/primary_text"
                android:textSize="14sp" />


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/places_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/icons"
                android:orientation="vertical"
                android:padding="10dp"
                app:layout_constrainedHeight="true" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="horizontal"
        android:weightSum="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/user_edit_card">

        <Button
            android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:layout_weight="1"
            android:text="@string/dialog_cancel"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/save_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_weight="1"
            android:text="@string/dialog_save"
            android:textAllCaps="false" />
    </LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>
`

And this is how I initialize it in activity.

public class AlertDialogActivity extends AppCompatActivity implements PlacesAutoCompleteAdapter.ClickListener {

private static final String TAG = "AlertDialogActivity";
private ActivityAlertDialogBinding binding;
private EditText userNameEdit;
private EditText userAddressEdit;
private Button saveUserButton;
private Button cancelButton;
private RecyclerView recyclerViewPlaces;
private PlacesAutoCompleteAdapter adapter;
private UserViewModel userViewModel;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityAlertDialogBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);
    this.setFinishOnTouchOutside(false);
    initViews(binding);

private void initViews(ActivityAlertDialogBinding binding) {
    userNameEdit = binding.userNameEt;
    userAddressEdit = binding.userAddressEt;
    saveUserButton = binding.saveButton;
    cancelButton = binding.cancelButton;
    recyclerViewPlaces = binding.placesRecyclerView;
}

I am trying to figure out why the views lose margin and when the Edittext is gaining focus the margins are restored

Source: Android Questions

LEAVE A COMMENT