Zulqurnain Jutt
Zulqurnain Jutt

Reputation: 1093

Center Align View With respect to another view

I want to center align my view1 over another view2 , and view2 and view1 both are of custom size.

View1
|------------------------|
|                        |
|                        |
|     |---------|        |
|     |   View2 |        |
|     |---------|        |
|                        |
|------------------------|

Note: View2 is in the middle/center of view1 and view1 is not parent , how can i achieve this?

My Attempt:

<!-- some xml of another parents -->
<RelativeLayout
android:layout_width="match_parent"
    android:layout_height="match_parent"
>
<ImageView
        android:id="@+id/pin"
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:src="@drawable/image"
        android:layout_centerInParent="true"/>
</RelativeLayout>
<!--- long xml --->

Upvotes: 0

Views: 409

Answers (1)

Rissmon Suresh
Rissmon Suresh

Reputation: 14053

Try below code... itz working for me

Let View1=ImageView; View2=TextView;

<?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" >


<ImageView
    android:id="@+id/overlapImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp"

    android:src="@drawable/common_plus_signin_btn_text_dark_pressed" />
<TextView
    android:id="@+id/textViewTittle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Center Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textStyle="bold" />

</RelativeLayout>

Upvotes: 1

Related Questions