Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create draggable image in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 173
    Comment on it

    In the below example I have created draggable panel function. This will works like a notification bar up and down. Here first I have added slidinguppanel support library in build.gradle file. Then in next step i have added SlidingUpPanelLayout, Button, TextView, ImageView in the activity_main.xml layout. And last step In MainActivity I have used setPanelSlideListener method. You can see below program it will clearly describe you How to create draggable image in android.

     

    Step(1)-I have added  first  slidinguppanel support library in build.gradle  file -
     

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        repositories {
            mavenCentral()
        }
        compile 'com.sothree.slidinguppanel:library:3.0.0'
    
    }

    Step(2)activity.main.xml-
     

    <RelativeLayout 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"
        tools:context=".MainActivity">
    
        <com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto"
            android:id="@+id/sliding_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom"
            sothree:umanoDragView="@+id/dragView"
            sothree:umanoOverlay="true"
            sothree:umanoPanelHeight="68dp"
            sothree:umanoParalaxOffset="100dp"
            sothree:umanoShadowHeight="4dp">
    
            <!-- MAIN CONTENT -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:clickable="true"
                    android:focusable="false"
                    android:text="Welcome"
                    android:focusableInTouchMode="false"
                    android:gravity="center"
                    android:textSize="20sp" />
    
                <Button
                    android:id="@+id/btn_show"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/text"
                    android:layout_centerInParent="true"
                    android:layout_marginRight="15dp"
                    android:layout_marginLeft="15dp"
                    android:text="Show"
                    android:visibility="gone" />
            </RelativeLayout>
    
            <!-- SLIDING LAYOUT -->
            <LinearLayout
                android:id="@+id/dragView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffffff"
                android:clickable="true"
                android:focusable="false"
                android:orientation="vertical">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
    
                    <TextView
                        android:id="@+id/name"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:textColor="@android:color/holo_green_dark"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:text="slide"
                        android:textSize="14sp" />
    
                    <Button
                        android:id="@+id/btn_hide"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:gravity="center_vertical|right"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:text="Hide"
                        android:textSize="14sp" />
    
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFCCCC">
    
                    <ImageView
                        android:layout_width="300dp"
                        android:layout_height="200dp"
                        android:layout_margin="10dp"
                        android:layout_weight="1"
                        android:src="@drawable/one" />
                </LinearLayout>
            </LinearLayout>
        </com.sothree.slidinguppanel.SlidingUpPanelLayout>
    
    </RelativeLayout>
    
    

    Step(3)-MainActivity-
      

      public class MainActivity extends Activity {
        private SlidingUpPanelLayout slidingLayout;
        private Button btnShow;
        private Button btnHide;
        private TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btnShow = (Button)findViewById(R.id.btn_show);
            btnHide = (Button)findViewById(R.id.btn_hide);
            textView = (TextView)findViewById(R.id.text);
    
            slidingLayout = (SlidingUpPanelLayout)findViewById(R.id.sliding_layout);
            
            slidingLayout.setPanelSlideListener(onSlideListener());
            btnHide.setOnClickListener(onHideListener());
            btnShow.setOnClickListener(onShowListener());
        }
    
        private View.OnClickListener onShowListener() {
            return new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //show sliding layout in bottom of screen (not expand it)
                    slidingLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
                    btnShow.setVisibility(View.GONE);
                }
            };
        }
    
        /**
         * Hide sliding layout when click button
         * @return
         */
        private View.OnClickListener onHideListener() {
            return new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //hide sliding layout
                    slidingLayout.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN);
                    btnShow.setVisibility(View.VISIBLE);
                }
            };
        }
    
        private SlidingUpPanelLayout.PanelSlideListener onSlideListener() {
            return new SlidingUpPanelLayout.PanelSlideListener() {
                @Override
                public void onPanelSlide(View view, float v) {
                    textView.setText("panel is sliding");
                }
    
                @Override
                public void onPanelCollapsed(View view) {
                    textView.setText("panel Collapse");
                }
    
                @Override
                public void onPanelExpanded(View view) {
                    textView.setText("panel expand");
                }
    
                @Override
                public void onPanelAnchored(View view) {
                    textView.setText("panel anchored");
                }
    
                @Override
                public void onPanelHidden(View view) {
                    textView.setText("panel is Hidden");
                }
            };
        }
    }

     

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