Saturday, September 24, 2016

Dynamic Swipe View Android

I am going to explore my first  sample application in android ,which gives you
an idea , How to create a Dynamic swipe view in Android.

Hope this will helps you......


1- Print Screen of Dynamic Swipe View


2-Android Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="swip.view.example"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
           </application>
</manifest>

3-MainActivity code


package swip.view.example;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
int cardRest;
String userRest;
String[] name;
Integer[] card;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ArrayList<Contact> Contact_data = new ArrayList<Contact>();
// fill hard coded data contact type in array list
Contact_data.add(new Contact(R.drawable.card1, "Ashish"));
Contact_data.add(new Contact(R.drawable.card2, "Avinash"));
Contact_data.add(new Contact(R.drawable.card3, "Ben"));
Contact_data.add(new Contact(R.drawable.card4, "Champ"));

name = new String[Contact_data.size()];
card = new Integer[Contact_data.size()];
for (int i = 0; i < Contact_data.size(); i++) {

name[i] = Contact_data.get(i).title;
card[i] = Contact_data.get(i).icon;
}

viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
for (int j = 0; j < Contact_data.size(); j++) {
TextView label = new TextView(this);
TextView value = new TextView(this);
ImageView imge = new ImageView(this);
WebView web = new WebView(this);
Button btn = new Button(this);

label.setText("Get your  world here!");
value.setText(name[j]);
imge.setImageResource(card[j]);
imge.setLayoutParams(new LinearLayout.LayoutParams(70, 70));
web.loadUrl("http://www.google.co.in/");
web.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 300));
btn.setText("Go To Facebook!");
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));


LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mLinearLayout.setOrientation(1);
LinearLayout mLinearLayout2 = new LinearLayout(this);

mLinearLayout2.setOrientation(0);
mLinearLayout.addView(mLinearLayout2);
mLinearLayout2.addView(imge);
LinearLayout mLinearLayout3 = new LinearLayout(this);

mLinearLayout3.setOrientation(1);
mLinearLayout2.addView(mLinearLayout3);
mLinearLayout3.addView(value);
mLinearLayout3.addView(label);
mLinearLayout.addView(web);
mLinearLayout.addView(btn);
viewFlipper.addView(mLinearLayout);


slideLeftIn = AnimationUtils.loadAnimation(this,
R.anim.slide_in_left);
slideLeftOut = AnimationUtils.loadAnimation(this,
R.anim.slide_out_left);
slideRightIn = AnimationUtils.loadAnimation(this,
R.anim.slide_in_right);
slideRightOut = AnimationUtils.loadAnimation(this,
R.anim.slide_out_right);

gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};

}
}

class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
}
} catch (Exception e) {
// nothing
}
return false;
}
}

// It is necessary to return true from onDown for the onFling event to
// register
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
}
3-contact.java 
package swip.view.example;

public class Contact {
  public int icon;
    public String title;
    public Contact(){
        super();
    }
        public Contact(int icon, String title) {
        super();
        this.icon = icon;
        this.title = title;
    }
}


4-Main.xml file
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ViewFlipper>

5-And create a folder name "anim" in res and add 4 xml file for left, right up, down swipe on touch screen.
i)res/anim/slide_in_left.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

ii)res/anim/slide_in_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

iii)res/anim/slide_out_left.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

iv)res/anim/slide_out_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="50%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

No comments:

Post a Comment