Views losing margin when activity created Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Views losing margin when activity created without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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

The Ui works fine on emulator. The video is from my device.

Answer

I would first remove all match_parent sizes from the children of the ConstraintLayout and replace with 0dp and the appropriate constraints. See the “important” comment here.

Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to “parent”.

We are here to answer your question about Views losing margin when activity created - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji