Android – Difficulty understanding layout_alignWithParentIfMissing

androidandroid-layout

I don't understand the meaning of layout_alignWithParentIfMissing for the TextView attribute.

I read the following documentation:

android:layout_alignWithParentIfMissing

If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc.

Must be a boolean value, either true or false.

Please explain to me.

Best Solution

This apply only when using RelativeLayout.

If You set element to be toLeftOf some other element it means it will be on the left of this element.

But if this element will be missing, because You delete it for example it will be align to parent.

Take this example

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="102dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignWithParentIfMissing="true"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

See what happens If You delete textView1 but don't touch textView2.

It will go to the bottom, on left(not right because it is inside of parent)

Android will position element relative to parent instead of element it should be because it is missing.

If it is false parameters relating to missing elements will be ignored.