How to get the position of a dynamically added view relative to its parent

788 views Asked by At
RelativeLayout.LayoutParams imageParams= new RelativeLayout.LayoutParams(200,200);
ImageView mImageView=new ImageView(this);
mImageView.setImageBitmap(bitmap);
viewParent.addView(mImageView, imageParams);

Under my touch event, i log getX, getLeft and leftMargin of my view.

18835-18835/com.android.test E/﹕ getX: 260.49933 getLeft: 0.0 leftMargin: 0.0

Why getLeft and leftMargin return 0? How can i get the view's position relative to its parent?

EDIT

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.now.stickit.EditNoteActivity"
    android:id="@+id/edit_note_layout">

<RelativeLayout
    android:id="@+id/view_parent"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <ImageView
        android:id="@+id/background_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"/>

    <ImageView
        android:id="@+id/canvas_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"/>

</RelativeLayout>

   <LinearLayout

    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:layout_gravity="bottom"
    android:gravity="center_horizontal"
    android:background="@color/primary">

   <ImageButton
       android:id="@+id/addText"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_add_text"
       android:background="#00000000"/>

   <ImageButton
       android:id="@+id/draw"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_draw"
       android:background="#00000000"/>

   <ImageButton
       android:id="@+id/erase"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_erase"
       android:background="#00000000"/>

   <ImageButton
       android:id="@+id/addImage"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_add_image"
       android:background="#00000000"/>

   <ImageButton
       android:id="@+id/undo"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_undo"
       android:background="#00000000"/>
</LinearLayout>
</LinearLayout>

I added an imageview programmatically into the relative layout. Then I am able to drag and drop the image any where inside the view

enter image description here

06-16 02:34:45.858    2721-2721/com.android.test E/﹕ getX: 206.57422getLeft: 0.0leftMargin: 0.0

enter image description here

06-16 02:43:28.566    2721-2721/com.now.stickit E/﹕ getX: 225.54785getLeft: 0.0leftMargin: 0.0
1

There are 1 answers

1
Anatol On

It will be better to provide image for your question for visualization.

Quoting the documentation:

getLeft ()

Left position of this view relative to its parent.

So you are adding your view inside RelativeLayout and it goes to top, left corner. It is normal behavior and getLeft() and getLeftMargin() will return 0.

How can i get the view's position relative to its parent?

So now your are getting your view position relative to it's parent. Maybe your forget to add some rules on your RelativeLayout.Params?