android – Difficulty understanding layout_alignWithParentIfMissing-ThrowExceptions

Exception or error:

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.

How to solve:

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.

###

Another sample. I have just used.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="6dip" >

    <Button
       android:id="@+id/streamOverflow"
       android:layout_width="50dp"
       android:layout_height="30dp"
       android:layout_alignParentRight="true"
       android:background="@drawable/ic_action_overflow"
       android:focusable="false" />

    <TextView
       android:id="@+id/delete_time"
       style="?android:attr/textAppearanceSmall"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Disappears "
       android:layout_toLeftOf="@id/streamOverflow"
       android:layout_alignWithParentIfMissing="true"
     />
</RelativeLayout>

if “streamOverflow” is missing, “delete_time” textview behaves like having android:layout_alignParentRight=”true” property.

###

In Relative Layouts, the position of a View can be specified relative to another View.

The tags layout_toLeftOf and layout_toRightOf position the view relative to an ‘anchor’ view object. When this anchor View is missing, the tag alignWithParentIfMissing, will align your view with the parent view.

Leave a Reply

Your email address will not be published. Required fields are marked *