Saturday 8 December 2012

Handler in Android

public class MyGridFragment extends Fragment{

    static class MyInnerHandler extends Handler{
        WeakReference<MyGridFragment> mFrag;

        MyInnerHandler(MyGridFragment aFragment) {
            mFrag = new WeakReference<MyGridFragment>(aFragment);
        }

        @Override
        public void handleMessage(Message message) {
            MyGridFragment theFrag = mFrag.get();
            switch (message.what) {
            case 2:
                ArrayList<HashMap<String,String>> theurls = (ArrayList<HashMap<String,String>>) message.obj;
                theFrag.urls.addAll(theurls);
                theFrag.theimageAdapter.notifyDataSetChanged();
                theFrag.dismissBusyDialog();
                break;
            }//end switch
        }
    }
    MyInnerHandler myHandler = new MyInnerHandler(this);
//write all other code here
 
 
}

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>
   
  --------------------------------------------------------------------------------------------     

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