Hi Friends,
I am going to share 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......
I am going to share 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......
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>
Nice tutorial thanks manish keep it up......
ReplyDelete@Manish mLinearLayout.setOrientation(1); what is the 1 and 0 in your code? can you please help me?
ReplyDelete@Naman I am sorry dear i have not used proper comment in my code..
ReplyDeletemLinearLayout is used for create dynamically Linear layout and 1 and 0 used for set orientation vertical and horizontal...I have create 3 Linear layout in my application and add it in a swipe view. Its work rock for me .. hope its will help you...
Could you email me the code zip file to me ?
ReplyDeleteMy email is : ufficiopierangelo@libero.it
thanks for your help.
Hi Thanks for your comment kindly check your email I have sent you the zip code..
DeleteThanks,
Hi Manish am getting dynamic pages from server i want to display it on textview and go to another page on swipping ...am unable to set multiple pages on single layout so that am going for static layout designing on swipe.is their any solution to display all pages on single layout on swipe .....
DeleteIts really helpfull.Can u plz email me the code zip file.My email id is gs_20@rediffmail.com.
ReplyDeleteThanks for your positive comment but I think every think clear on my blog, I don't think any need for zip code here any way please check your email I have sent you the zip code..
DeleteThanks,
please help me if you have done any work related to Services and broadcastreceivers and alaram manager..my mail id :sampath.muddineni@gmail.com along with this custom calender projeect and this project..Thanks
ReplyDeleteHi muddineni!
DeletePlease try my below post hope it will help you but I am not sure it will fulfillment your requirements. And check your email for other project code..
1)http://www.androidhub4you.com/2012/09/wi-fi-device-scanning-in-android.html
2)http://www.androidhub4you.com/2012/09/location-manager-demo-in-android.html
3)http://www.androidhub4you.com/2013/03/how-to-create-services-in-android.html
Thanks,
Please send me zip code file.
ReplyDeleteMy email is: lovegreenforever92@gmail.com
Thanks for your help!!!
Hi!
DeleteYou can copy paste code from above it's not very complex...Well I have sent you the zip code please check your email!
Please send me zip code file.
ReplyDeleteMy email is: ladrajvi2@gmail.com
Thanks in advance.
Please send zip code file..
ReplyDeleteMail ID: karthikbala1212@gmail.com
Thanks bro....