Thursday 25 October 2012

how to drag and drop a button to another separate layouts in android programmatically



my android java main program:
   
    package com.example.draganddropdemo;
   
    import android.os.Bundle;
   
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.app.ActionBar.LayoutParams;
    import android.graphics.Color;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.Toast;
   
    public class MainActivity extends Activity /*implements OnTouchListener*/ {
   
       private static final int START_DRAGGING = 0;
       private static final int STOP_DRAGGING = 1;
      
       private Button dragbtn1,dragbtn2,dragbtn3,dragbtn4;
       private Button para_btn1,para_btn2,para_btn3;
       private RelativeLayout layout,layoutNew,layoutNew2,layoutNew3;
       private int status;
       private int buttonId = 0;
       private Boolean booleanbtn1 = false,booleanbtn2 = false,booleanbtn3 = false,booleanbtn4 = false;
       //private ViewGroup.LayoutParams params;
       private ImageView image,imageNew,imageNew3,imageNew4;
      
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            System.out.println("------------onCreate-----------------------------------");
           
            para_btn1 = (Button) findViewById(R.id.para_button1);
            para_btn2 = (Button) findViewById(R.id.para_button2);
            para_btn3 = (Button) findViewById(R.id.para_button3);
           
            layout =  (RelativeLayout) findViewById(R.id.frameLayout1);
            layoutNew = (RelativeLayout) findViewById(R.id.relativeLayoutsample1);
            layoutNew2 = (RelativeLayout) findViewById(R.id.relativeLayoutsample2);
            layoutNew3 = (RelativeLayout) findViewById(R.id.relativeLayoutsample3);
           
           
          //------------------------------------------------------------------------
            dragbtn1 = (Button) findViewById(R.id.drag_button1);
            dragbtn1.setDrawingCacheEnabled(true);
            dragbtn1.setOnTouchListener(new View.OnTouchListener() {
               
                public boolean onTouch(View v, MotionEvent event) {
                    buttonId = 1;
                   
                    if( event.getAction() == MotionEvent.ACTION_DOWN ) {
                        status = START_DRAGGING;
                        image = new ImageView(getApplicationContext());
                        image.setImageBitmap(dragbtn1.getDrawingCache());
                        layout.addView(image);
                       
                    }
                    if( event.getAction() == MotionEvent.ACTION_UP ) {
                        status = STOP_DRAGGING;
                        Log.i("Drag", "Stopped Dragging");
                        booleanbtn1 = true;
                        booleanbtn2 = false;
                        booleanbtn3 = false;
                        booleanbtn4 = false;
                        checkMethod();
                    }
                    else if( event.getAction() == MotionEvent.ACTION_MOVE ) {
                        if( status == START_DRAGGING ) {
                           
                            System.out.println("--------------Dragging--------------");
                            image.setPadding((int)event.getRawX(),(int)event.getRawY(), 0, 0);
                            image.invalidate();
                           
                        }
                    }
                   
                    return false;
                }
            });
          //------------------------------------------------------------------------
           
            dragbtn2 = (Button) findViewById(R.id.drag_button2);
            dragbtn2.setDrawingCacheEnabled(true);
            dragbtn2.setOnTouchListener(new View.OnTouchListener() {
               
                public boolean onTouch(View v, MotionEvent event) {
                    System.out.println("------------onTouch----------------------event:"+event+"-------View:"+v);
                    buttonId = 2;
                    if( event.getAction() == MotionEvent.ACTION_DOWN ) {
                        status = START_DRAGGING;
                        imageNew = new ImageView(getApplicationContext());
                        imageNew.setImageBitmap(dragbtn2.getDrawingCache());
                        layout.addView(imageNew);
                    }
                    if( event.getAction() == MotionEvent.ACTION_UP ) {
                        status = STOP_DRAGGING;
                        Log.i("Drag", "Stopped Dragging");
                        booleanbtn2 = true;
                        booleanbtn1 = false;
                        booleanbtn3 = false;
                        booleanbtn4 = false;
                        checkMethod();
                    } else if( event.getAction() == MotionEvent.ACTION_MOVE ) {
                        if( status == START_DRAGGING ) {
                           
                            System.out.println("--------------Dragging--------------");
                            imageNew.setPadding((int)event.getRawX(),(int)event.getRawY(), 0, 0);
                            imageNew.invalidate();
                           
                        }
                    }
                   
                    return false;
                }
            });
           
           // params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
           
          //------------------------------------------------------------------------
            dragbtn3 = (Button) findViewById(R.id.drag_button3);
            dragbtn3.setDrawingCacheEnabled(true);
            dragbtn3.setOnTouchListener(new OnTouchListener() {
               
                public boolean onTouch(View v, MotionEvent event) {
                    buttonId = 3;
                    if( event.getAction() == MotionEvent.ACTION_DOWN ) {
                        status = START_DRAGGING;
                        imageNew3 = new ImageView(getApplicationContext());
                        imageNew3.setImageBitmap(dragbtn3.getDrawingCache());
                        layout.addView(imageNew3);
                    }
                    if( event.getAction() == MotionEvent.ACTION_UP ) {
                        status = STOP_DRAGGING;
                        Log.i("Drag", "Stopped Dragging");
                        booleanbtn3 = true;
                        checkMethod();
                    } else if( event.getAction() == MotionEvent.ACTION_MOVE ) {
                        if( status == START_DRAGGING ) {
                           
                            System.out.println("--------------Dragging--------------");
                            imageNew3.setPadding((int)event.getRawX(),(int)event.getRawY(), 0, 0);
                            imageNew3.invalidate();
                           
                        }
                    }
                   
                    return false;
                }
            });
          
           
          //------------------------------------------------------------------------
          //------------------------------------------------------------------------
            dragbtn4 = (Button) findViewById(R.id.drag_button4);
            dragbtn4.setDrawingCacheEnabled(true);
            dragbtn4.setOnTouchListener(new OnTouchListener() {
               
                public boolean onTouch(View v, MotionEvent event) {
                    buttonId = 4;
                    if( event.getAction() == MotionEvent.ACTION_DOWN ) {
                        status = START_DRAGGING;
                        imageNew4 = new ImageView(getApplicationContext());
                        imageNew4.setImageBitmap(dragbtn4.getDrawingCache());
                        layout.addView(imageNew4);
                    }
                    if( event.getAction() == MotionEvent.ACTION_UP ) {
                        status = STOP_DRAGGING;
                        Log.i("Drag", "Stopped Dragging");
                        booleanbtn4 = true;
                        checkMethod();
                    } else if( event.getAction() == MotionEvent.ACTION_MOVE ) {
                        if( status == START_DRAGGING ) {
                           
                            System.out.println("--------------Dragging--------------");
                            imageNew4.setPadding((int)event.getRawX(),(int)event.getRawY(), 0, 0);
                            imageNew4.invalidate();
                           
                        }
                    }
                   
                    return false;
                }
            });
          
           
          //------------------------------------------------------------------------
           
           
        }
        //------------------------------------------------------------------------
         @SuppressLint({ "NewApi", "NewApi" })
        protected void checkMethod() {
            System.out.println("------------checkMethod----------------------------");
            Toast.makeText(MainActivity.this,"checkMethod", 10).show();
           
            /*Boolean lay1,lay2,lay3,btn1,btn2,btn3;
            lay1 = layoutNew.isInTouchMode();
            lay2 = layoutNew2.isInTouchMode();
            lay3 = layoutNew3.isInTouchMode();
            System.out.println("-----------------------lay1:"+lay1+"------lay2:"+lay2+"-----lay3:"+lay3);
            Toast.makeText(MainActivity.this,"lay1:"+lay1+" lay2:"+lay2+" lay3:"+lay3, 120).show();*/
            //btn1 = para_btn1.isInTouchMode();
            //btn2 = para_btn2.isInTouchMode();
            //btn3 = para_btn3.isInTouchMode();
            //System.out.println("-----------------------btn1:"+btn1+"------btn2:"+btn2+"-----btn3:"+btn3);
            //Toast.makeText(MainActivity.this,"btn1:"+btn1+" btn2:"+btn2+" btn3:"+btn3, 120).show();
   
            //----if start-------
            if ( layoutNew.isInTouchMode() == true )  // First Layout
            {
               
                Toast.makeText(MainActivity.this,"Parameter1", 30).show();
               
                if ( booleanbtn1 == true )  //-----------
                {
                    Toast.makeText(MainActivity.this,"booleanbtn1", 20).show();
                    image.setVisibility(View.GONE);
                    //layoutNew.removeView(image);
                   
                           if( buttonId == 1 ) {
                           Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 1", 120).show();
                           para_btn1.setHeight(dragbtn1.getHeight());
                           para_btn1.setBackgroundColor(Color.GREEN);
                           }
                           if( buttonId == 2 ) {
                              
                               Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 2", 120).show();
                               para_btn1.setHeight(dragbtn2.getHeight());
                               para_btn1.setBackgroundColor(Color.YELLOW);
                            }
                           if( buttonId == 3 ) {
                               Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 3", 120).show();
                               para_btn1.setHeight(dragbtn3.getHeight());
                               para_btn1.setBackgroundColor(Color.RED);
                               }
                               if( buttonId == 4 ) {
                                  
                                   Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 4", 120).show();
                                   para_btn1.setHeight(dragbtn4.getHeight());
                                   para_btn1.setBackgroundColor(Color.BLACK);
                                }
                          
                }
                else if ( booleanbtn2 == true ) {
                    Toast.makeText(MainActivity.this,"booleanbtn2", 20).show();
                    imageNew.setVisibility(View.GONE);
                   
                        if( buttonId == 1 ) {
                            Toast.makeText(MainActivity.this,"ImageNew Parameter1buttonId 1", 120).show();
                           para_btn1.setHeight(dragbtn1.getHeight());
                           para_btn1.setBackgroundColor(Color.GREEN);
                           }
                           if( buttonId == 2 ) {
                               Toast.makeText(MainActivity.this,"ImageNew Parameter1buttonId 2", 120).show();
                               para_btn1.setHeight(dragbtn2.getHeight());
                               para_btn1.setBackgroundColor(Color.YELLOW);
                            }
                           if( buttonId == 3 ) {
                               Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 1", 120).show();
                               para_btn1.setHeight(dragbtn3.getHeight());
                               para_btn1.setBackgroundColor(Color.RED);
                               }
                               if( buttonId == 4 ) {
                                  
                                   Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 2", 120).show();
                                   para_btn1.setHeight(dragbtn4.getHeight());
                                   para_btn1.setBackgroundColor(Color.BLACK);
                                }
                   
                   
                }//-----------
           
                else if ( booleanbtn3 == true) {
                    Toast.makeText(MainActivity.this,"booleanbtn3", 20).show();
                        if( buttonId == 1 ) {
                            Toast.makeText(MainActivity.this,"ImageNew Parameter1buttonId 1", 120).show();
                           para_btn1.setHeight(dragbtn1.getHeight());
                           para_btn1.setBackgroundColor(Color.GREEN);
                           }
                           if( buttonId == 2 ) {
                               Toast.makeText(MainActivity.this,"ImageNew Parameter1buttonId 2", 120).show();
                               para_btn1.setHeight(dragbtn2.getHeight());
                               para_btn1.setBackgroundColor(Color.YELLOW);
                            }
                           if( buttonId == 3 ) {
                               Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 1", 120).show();
                               para_btn1.setHeight(dragbtn3.getHeight());
                               para_btn1.setBackgroundColor(Color.RED);
                               }
                               if( buttonId == 4 ) {
                                  
                                   Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 2", 120).show();
                                   para_btn1.setHeight(dragbtn4.getHeight());
                                   para_btn1.setBackgroundColor(Color.BLACK);
                                }
                   
                   
                }//-----------
                else if ( booleanbtn4 == true) {
                    Toast.makeText(MainActivity.this,"booleanbtn4", 20).show();
                   
                        if( buttonId == 1 ) {
                            Toast.makeText(MainActivity.this,"ImageNew Parameter1buttonId 1", 120).show();
                           para_btn1.setHeight(dragbtn1.getHeight());
                           para_btn1.setBackgroundColor(Color.GREEN);
                           }
                           if( buttonId == 2 ) {
                               Toast.makeText(MainActivity.this,"ImageNew Parameter1buttonId 2", 120).show();
                               para_btn1.setHeight(dragbtn2.getHeight());
                               para_btn1.setBackgroundColor(Color.YELLOW);
                            }
                           if( buttonId == 3 ) {
                               Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 3", 120).show();
                               para_btn1.setHeight(dragbtn3.getHeight());
                               para_btn1.setBackgroundColor(Color.RED);
                               }
                               if( buttonId == 4 ) {
                                  
                                   Toast.makeText(MainActivity.this,"Image1 Parameter1buttonId 4", 120).show();
                                   para_btn1.setHeight(dragbtn4.getHeight());
                                   para_btn1.setBackgroundColor(Color.BLACK);
                                }
                   
                   
                }//-----------
           
            }//--if end---------   
            else if ( layoutNew2.isInTouchMode() == true ) {  // Check touch code for Second Layout
               
               
                Toast.makeText(MainActivity.this,"Parameter 2", 120).show();
               
                if ( booleanbtn1 == true )  //-----------
                {
                    image.setVisibility(View.GONE);
                   
                           if( buttonId == 1 ) {
                           para_btn1.setHeight(dragbtn1.getHeight());
                           para_btn1.setBackgroundColor(Color.GREEN);
                           }
                           if( buttonId == 2 ) {
                               para_btn1.setHeight(dragbtn2.getHeight());
                               para_btn1.setBackgroundColor(Color.YELLOW);
                            }
                          
                }
                else if ( booleanbtn2 == true ) {
                   
                    imageNew.setVisibility(View.GONE);
                   
                          if( buttonId == 1 ) {
                           para_btn1.setHeight(dragbtn1.getHeight());
                           para_btn1.setBackgroundColor(Color.GREEN);
                           }
                           if( buttonId == 2 ) {
                               para_btn1.setHeight(dragbtn2.getHeight());
                               para_btn1.setBackgroundColor(Color.YELLOW);
                            }
                   
                }//-----------
               
            } // else if end-------------
           
            else if ( layoutNew3.isInTouchMode() == true ) {  // Third Layout
                Toast.makeText(MainActivity.this,"Parameter 3", 120).show();
               
                if ( booleanbtn1 == true)  //-----------
                {
                    image.setVisibility(View.GONE);
                   
                           if( buttonId == 1 ) {
                           para_btn1.setHeight(dragbtn1.getHeight());
                           para_btn1.setBackgroundColor(Color.GREEN);
                           }
                           if( buttonId == 2 ) {
                               para_btn1.setHeight(dragbtn2.getHeight());
                               para_btn1.setBackgroundColor(Color.YELLOW);
                            }
                          
                }
                else if ( booleanbtn2 == true ) {
                   
                    imageNew.setVisibility(View.GONE);
                   
                         if( buttonId == 1 ) {
                           para_btn1.setHeight(dragbtn1.getHeight());
                           para_btn1.setBackgroundColor(Color.GREEN);
                           }
                           if( buttonId == 2 ) {
                               para_btn1.setHeight(dragbtn2.getHeight());
                               para_btn1.setBackgroundColor(Color.YELLOW);
                            }
                   
                   
                }//-----------
            } // else if end-------------
           
        }
        //------------------------------------------------------------------------
    }
   
    -------------------------------------------------------------------------------------------
    my XML program :
   
    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/frameLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true" >
   
            <Button
                android:id="@+id/drag_button1"
                android:layout_width="30dp"
                android:layout_height="70dp"
                android:background="#4CC417"
                android:text="btn1" />
   
            <RelativeLayout
                android:id="@+id/relativeLayoutsample3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:background="#5CB3FF" >
   
                <Button
                    android:id="@+id/para_button3"
                    android:layout_width="30dp"
                    android:layout_height="70dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="24dp"
                    android:text="Button" />
   
            </RelativeLayout>
   
            <Button
                android:id="@+id/drag_button2"
                android:layout_width="30dp"
                android:layout_height="70dp"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="17dp"
                android:layout_toRightOf="@+id/drag_button1"
                android:background="#FFFF00"
                android:text="btn2" />
   
            <RelativeLayout
                android:id="@+id/relativeLayoutsample1"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:background="#808000" >
   
                <Button
                    android:id="@+id/para_button1"
                    android:layout_width="30dp"
                    android:layout_height="70dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="18dp"
                    android:text="Button" />
   
            </RelativeLayout>
   
            <Button
                android:id="@+id/drag_button3"
                android:layout_width="30dp"
                android:layout_height="70dp"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/relativeLayoutsample1"
                android:background="#FF0000"
                android:text="btn3" />
   
            <Button
                android:id="@+id/drag_button4"
                android:layout_width="30dp"
                android:layout_height="70dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="#000000"
                android:text="btn4" />
   
            <RelativeLayout
                android:id="@+id/relativeLayoutsample2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:background="#FFA500" >
   
                <Button
                    android:id="@+id/para_button2"
                    android:layout_width="30dp"
                    android:layout_height="70dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="19dp"
                    android:text="Button" />
            </RelativeLayout>
   
        </RelativeLayout>
   
  --------------------------------------------------------------------------------------------     

10 comments:

  1. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.

    angularjs-Training in velachery

    angularjs-Training in annanagar

    angularjs Training in chennai

    angularjs Training in chennai

    ReplyDelete
  2. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    Java training in Marathahalli | Java training in Btm layout

    Java training in Jaya nagar | Java training in Electronic city

    ReplyDelete
  3. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
    Data Science Training in Chennai | Data Science course in anna nagar

    Data Science course in chennai | Data science course in Bangalore

    Data Science course in marathahalli | Data Science course in btm layout

    ReplyDelete
  4. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
    Data Science Training in Chennai
    Python Training in Chennai
    RPA Training in Chennai
    Digital Marketing Training in Chennai

    ReplyDelete
  5. This is such a good post. One of the best posts that I\'ve read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  6. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.keep sharing!!

    Android Training in Chennai

    Android Online Training in Chennai

    Android Training in Bangalore

    Android Training in Hyderabad

    Android Training in Coimbatore

    Android Training

    Android Online Training

    ReplyDelete

Complete Details about eLitmus pH Test at Rs:699/- Share your java material and fresher interview Information for us to Help Others... mail to : vhkrishnan.v@gmail.com