Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to resize a custom view by drag side of view in android

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 11.3k
    Comment on it

    This tutorial will help to resize a custom view by dragging of view's side and provide drag functionality also. Here is the step by step code.

    Step 1- Create a xml file res/activity_main

    <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" ;
    
        Button
            android:id="@+id/button"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:text="Button View" ;
    
        AbsoluteLayout
            android:id="@+id/boardLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/board"
            android:gravity="center"
            android:padding="20dp";
    
        
    

     

    Step 2- Create a activity

     import android.app.Activity;
        import android.content.ClipData;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.DragEvent;
        import android.view.MotionEvent;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.View.OnLongClickListener;
        import android.view.View.OnTouchListener;
        import android.view.ViewGroup;
        import android.view.ViewGroup.LayoutParams;
        import android.widget.AbsoluteLayout;
        import android.widget.Button;
        import android.widget.ImageView;
        import android.widget.RelativeLayout;
    
        public class DragDrop extends Activity implements OnLongClickListener,
        View.OnDragListener,OnClickListener,OnTouchListener{
    
            private View imageView;
            private int height;
            private int width;
            private AbsoluteLayout boardLayout;
            private View layout;
            private RelativeLayout relativeLayoutrectangleView;
            private ImageView triangleView,circleView,rectangleView;
            private String IMAGE_RECTANGLE = "RECTANGLE";
            //boolean drag;
            float X,Y;
            boolean temp;
            private AbsoluteLayout absoluteLayout,absoluteLayout2;
            private AbsoluteLayout containView ;
            private Button button;
            int heightlayout,widthlayout;
            private float newX = 0;
            private float newY = 0;
            private float oldX = 0;
            private float oldY = 0;
            private float dX = 0;
            private float dY = 0;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
    
                // set OnDragListener on the view where to drop the dragged item
                boardLayout=(AbsoluteLayout)findViewById(R.id.boardLayout);
                boardLayout.setOnDragListener(this);
                button=(Button)findViewById(R.id.button);
                button.setOnClickListener(this);
    
            }
    
            @Override
            public boolean onDrag(View v, DragEvent event) {
    
                // Defines a variable to store the action type for the incoming
                // event
                final int action = event.getAction();
    
                // Handles each of the expected events
                switch (action) {
    
                case DragEvent.ACTION_STARTED:
                    // Invalidate the view to force a redraw in the new tint
                    v.invalidate();
    
    
                    // dragged data.
                    return true;
    
                case DragEvent.ACTION_DRAG_ENTERED:
    
    
                    // Invalidate the view to force a redraw in the new tint
                    v.invalidate();
    
                    return false;
    
                case DragEvent.ACTION_LOCATION:
    
                    X = event.getX();
                    Y = event.getY();
    
    
                    if(X>widthlayout  && Y>heightlayout){
                        temp = true;
                    }
                    // Ignore the event
                    return false;
    
                case DragEvent.ACTION_DRAG_EXITED:
    
                    //v.invalidate();
    
                    return false;
    
                case DragEvent.ACTION_DROP:
    
    
                    // Gets the item containing the dragged data
                    ClipData dragData = event.getClipData();
    
                    // Gets the text data from the item.
                    final String tag = dragData.getItemAt(0).getText().toString();
                    View view = (View) event.getLocalState();
                    ViewGroup viewgroup = (ViewGroup) view.getParent();
                    viewgroup.removeView(view);
                    X = event.getX();
                    Y = event.getY();
                    view.setX(X-(view.getWidth()/2));
                    view.setY(Y-(view.getHeight()/2));
                    containView = (AbsoluteLayout) v;
                    containView.addView(view);
    
    
                    view.setVisibility(View.VISIBLE);
    
                    // Invalidates the view to force a redraw
                    v.invalidate();
    
                    // Returns true. DragEvent.getResult() will return true.
                    return true;
    
    
                case DragEvent.ACTION_DRAG_ENDED:
                    //drag=false;
    
                    v.invalidate();
    
    
                    return false;
    
                default:
                    break;
                }
    
                return false;
            }
    
    
    
    
            @Override
            public boolean onLongClick(View v) {
                String tag = v.getTag().toString();
                ClipData dragData = ClipData.newPlainText("TAG", tag);
                // Instantiates the drag shadow builder.
                View.DragShadowBuilder myShadow = null;
                //v.setBackgroundResource(0);
    
                if(tag.equals(IMAGE_RECTANGLE)){
                    v.setVisibility(View.INVISIBLE);
    
                    myShadow = new View.DragShadowBuilder(relativeLayoutrectangleView);
                }
                // Starts the drag
                v.startDrag(dragData, myShadow, v, 0);
                return false;
    
            }
    
            @Override
            public void onClick(View v) {
    
                createCustomView();
    
            }
    
            private void createCustomView() {
                absoluteLayout=new AbsoluteLayout(this);
                absoluteLayout.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                relativeLayoutrectangleView = new RelativeLayout(this); 
                relativeLayoutrectangleView.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
                relativeLayoutrectangleView.getLayoutParams().height=100;
                relativeLayoutrectangleView.getLayoutParams().width=100;
                rectangleView=new ImageView(this);
                rectangleView.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                relativeLayoutrectangleView.setTag(IMAGE_RECTANGLE);
                rectangleView.getLayoutParams().height=300;
                rectangleView.getLayoutParams().width=300;
                rectangleView.setImageResource((R.drawable.rectangle));
                relativeLayoutrectangleView.setOnLongClickListener(this);
                relativeLayoutrectangleView.setBackgroundResource(R.drawable.rectangle);
                relativeLayoutrectangleView.setOnTouchListener(this);
    
    
                ViewGroup.LayoutParams la_pm = rectangleView.getLayoutParams();
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(la_pm);
                //relativeLayoutrectangleView.addView(rectangleView,lp);  
    
                absoluteLayout.addView(relativeLayoutrectangleView);
                boardLayout.addView(absoluteLayout);
    
    
    
    
            }
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    width =v.getWidth()/2;
                    height =v.getHeight()/2;
    
                    // Construct a rect of the view's bounds
                    oldX=event.getX();
                    oldY=event.getY();
                    Log.d("oldX", oldX+"");
                    Log.d("oldY", oldY+"");
    
                    //Log.d("height and width", height+"" + width+"");
    
                }
                if(event.getAction() == MotionEvent.ACTION_MOVE){
    
                    newX=event.getX();
                    newY=event.getY();
    
                    dX=newX-oldX;
                    dY=newY-oldY;
    
    
                    if(oldX>width){
                        if(newX>oldX){
    
    
                            AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                            // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                            v.getLayoutParams().width=v.getWidth()+Math.abs((int)dX);
                            v.setLayoutParams(params); 
    
                        }else if(newX>oldX){
    
    
                            AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                            // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                            v.getLayoutParams().width=v.getWidth()-Math.abs((int)dX);
                            v.setLayoutParams(params); 
    
                        }
    
                        //if(dX==0){
                        if(newY>height){
                            if(dY>0){
                                Log.d("size of HEIGHT", "increaseingggggggg");
                                AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                                // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                                v.getLayoutParams().height=v.getHeight()+Math.abs((int)dY);
                                v.setLayoutParams(params);
                            }else if(dY<0 ){
    
                                AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                                // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                                v.getLayoutParams().height=v.getHeight()-Math.abs((int)dY);
                                v.setLayoutParams(params); 
                            }
                        }
                        else if(newY0){
    
                                AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                                // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                                v.getLayoutParams().height=v.getHeight()-Math.abs((int)dY);
                                v.setLayoutParams(params); 
                            }else if(dY<0){
    
                                AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                                // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                                v.getLayoutParams().height=v.getHeight()+Math.abs((int)dY);
                                v.setLayoutParams(params);
                            }
                        }
    
    
                        //}
                    }else if(oldXheight){
                                if(dY>0){
    
                                    AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                                    // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                                    v.getLayoutParams().height=v.getHeight()+Math.abs((int)dY);
                                    v.setLayoutParams(params);
                                }else if(dY<0 ){
    
                                    AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                                    // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                                    v.getLayoutParams().height=v.getHeight()-Math.abs((int)dY);
                                    v.setLayoutParams(params); 
                                }
                            }
                            else if(newY0){
                                    Log.d("size of HEIGHT", "decreaseingggggggg");
                                    AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                                    // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                                    v.getLayoutParams().height=v.getHeight()-Math.abs((int)dY);
                                    v.setLayoutParams(params); 
                                }else if(dY<0){
    
                                    AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                                    // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                                    v.getLayoutParams().height=v.getHeight()+Math.abs((int)dY);
                                    v.setLayoutParams(params);
                                }
                            }
    
    
                        }// end of dx==0 if
    
                        else{
                            if(newX>oldX){
                                if(dX>0){
    
                                    AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
                                    // relativeLayoutcircleView.getLayoutParams().height=relativeLayoutcircleView.getHeight()-50;
                                    v.getLayoutParams().width=v.getWidth()-Math.abs((int)dX);
                                    v.setLayoutParams(params); 
                                }
                            }else if(newX

 2 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: