Skip to content Skip to sidebar Skip to footer

Android - Creating Resizable View

I am currently working on a Dashboard with movable and resizable views. The problem I have now is that I want to resize the view by touch gestures. Therefore, I thought of a Point

Solution 1:

Try following class.

package mayank.com.bhaktirasamritaswami.ui.components;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import mayank.com.bhaktirasamritaswami.R;
publicclassResizableLayoutextendsRelativeLayoutimplementsView.OnTouchListener{
publicstaticint top_margine;
publicResizableLayout(Context context) {
    super(context);
    init();
}
publicResizableLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

publicvoidinit(){
    addView(inflate(getContext(), R.layout.ui_component_resizable_layout,null));
    setOnTouchListener(this);
    dragHandle = this.findViewById(R.id.drag_handle);
}

private View dragHandle;
float downRawY;
float dY;
float height;

@OverridepublicbooleanonTouch(View view, MotionEvent motionEvent) {
    intaction= motionEvent.getAction();
    if(action == MotionEvent.ACTION_DOWN){
        downRawY = motionEvent.getRawY();
        height = this.getMeasuredHeight();
    } else {
        Viewparent= (View) this.getParent();
        if(downRawY<parent.getHeight() - height + 2*dragHandle.getHeight()) {
            floatrawY= motionEvent.getRawY()>20*dragHandle.getHeight()?motionEvent.getRawY():20*dragHandle.getHeight();
            MarginLayoutParamsp= (MarginLayoutParams) this.getLayoutParams();
            p.topMargin = (int)rawY;
            if(p.topMargin!=0)
                this.top_margine = p.topMargin;
            this.setLayoutParams(p);
        }
    }
    returnfalse;
}
@OverridepublicbooleanonInterceptTouchEvent(MotionEvent motionEvent){
    onTouch(this, motionEvent);
    returnfalse;
}
}

I designed this class for vertical resizing only. You need to change onTouch method to suit your case.

Post a Comment for "Android - Creating Resizable View"