Search code examples
androidviewlayout-anchor

Anchoring item to center of the screen with some offset


I am trying to build a screen with a chat bubble. Problem is when i use android:layout_centerHorizontal="true" the bubble gets exactly to the center and expands on both direction based on the size of text. But I want to anchor the center of this bubble 20dp right to the center of the screen or parent layout.

<View
                android:id="@+id/anchor"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_centerHorizontal="true" />
            <TextView
                android:id="@+id/textview"
                android:text="Some Text"
                style="Style_with_layout_height_width_etc"
                android:layout_alignParentBottom="true"
                android:layout_alignStart="@+id/anchor"
                android:layout_marginStart="-20dp"
                android:layout_marginBottom="20dp"/>

I have tried the methods like having an anchor view with 0dp width and height but its of no use.

EDIT-1 : added XML code for further reference

EDIT-2 : using constraint layout and setting the horizontal bias helped me solve the problem.


Solution

  • Consider using the new ConstraintLayout for adjusting the view's position precisely for your needs.

    Reference- https://developer.android.com/training/constraint-layout/index.html