I have experience with RelativeLayout but I've never run across a way to solve the problem I am presented with (aside from hard coding margin values, which I want to avoid.)
I want to try to create something like the following image in a RelativeLayout:
The box is its own View and I want to get the View that contains the orange circle to be centered on the top right corner of the View that contains the blue box.
I tried with android:alignTop="boxView"
and android:alignRight="boxView"
but that put my orange circle completely within my box. I want it to be so that the circle is centered above the top right corner of the box.
Anybody know how I can get that outcome with a RelativeLayout? preferably without having to hardcode margins away from the edge of the screen for the orange dot view.
To center a view, just drag the handles to all four sides of the parent.
RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets. TableLayout : is a view that groups its child views into rows and columns. FrameLayout : is a placeholder on screen that is used to display a single ...
Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.
This code creates what you are looking for but does use margins. Now you can set the margin in code if this is a dynamic structure you are creating. As you can see I used negative margins to move the upper right shape outside of the blue box. These need to be half the height of the circle you are trying to move. You can do all of this in code to center the circle in the upper right corner.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#0000FF"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_alignRight="@+id/linearLayout1"
android:layout_alignTop="@+id/linearLayout1"
android:layout_marginRight="-13dp"
android:layout_marginTop="-13dp"
android:background="#FF00FF"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
Best and proper way to do:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp">
<View
android:id="@+id/viewBox"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<View
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/ic_close"
app:layout_constraintBottom_toTopOf="@+id/viewBox"
app:layout_constraintLeft_toRightOf="@+id/viewBox"
app:layout_constraintRight_toRightOf="@+id/viewBox"
app:layout_constraintTop_toTopOf="@+id/viewBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With