1

I wanted the relativelayout child views to be adjusted w.r.t an anchor view placed in center. So basically I have four views with one corner anchored to parent edge and another to center view. I have done this in past successfully, but this time the anchor view(center view) is dynamically positioned based on touch-move.

The expected result is that child views should resize itself based on anchor view position. Instead only one child view is visible which resizes as expected, and others are not visible at all.

Below is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

 <Button
    android:id="@+id/viewBottom"
    android:background="#ffff00"
    android:text="test"
    android:layout_below="@+id/dragView"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/dragView"
    android:layout_width="100dp"
    android:layout_height="100dp" />

 <View
     android:id="@+id/viewTop"
     android:layout_width="100dp"
     android:layout_height="100dp"
     android:layout_alignParentRight="true"
     android:layout_alignParentTop="true"
     android:layout_toRightOf="@+id/dragView"
     android:layout_above="@+id/dragView"
     android:background="#00ff00" />

 <Button
     android:id="@+id/dragView"
     android:layout_width="100dp"
     android:layout_height="100dp"
     android:layout_centerInParent="true"
     android:background="#0000ff" />

My oncreate code:

setContentView(R.layout.activity_main);

    container = (RelativeLayout)findViewById(R.id.container);
    dragView = (View)findViewById(R.id.dragView);
    viewTop = (View)findViewById(R.id.viewTop);

    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)dragView.getLayoutParams();
    layoutParams.width = 100;
    layoutParams.height = 100;
    layoutParams.leftMargin = 50;
    layoutParams.topMargin = 50;
    dragView.setLayoutParams(layoutParams);

    dragView.setOnTouchListener(this);

Ontouch code to move anchor view

public boolean onTouch(View view, MotionEvent event) {
    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            _xDelta = X - lParams.leftMargin;
            _yDelta = Y - lParams.topMargin;
            break;
        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_POINTER_UP:
            break;
        case MotionEvent.ACTION_MOVE:
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            layoutParams.leftMargin = X - _xDelta;
            layoutParams.topMargin = Y - _yDelta;
            view.setLayoutParams(layoutParams);

            break;
    }

    return true;
}

Any sort of help is highly appreciated. Thanks.

Avinazz
  • 372
  • 2
  • 14

0 Answers0