Pages

Showing posts with label yashpal singla. Show all posts
Showing posts with label yashpal singla. Show all posts

Friday, August 5, 2011

Creating Custom Horizontal Scroll View With Snap or paging

I have faced various issues to develop a horizontal scroll view with paging effect. I googled it a lot but not able to find anything satisfactory. Some search results help me, using those i have created my own custom horizontal scrollview class which can give similar effect as effect on Tablet Home page.

I will be discussing it step by step so that you can use it where required and it becomes easy for you to understand.

Step 1) Layout file -  Simple layout file for demo


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="match_parent"
android:layout_width="match_parent" android:id="@+id/layer">
</LinearLayout>

Step 2) CustomHorizontalScrollView - Class file which extends HorizontalScrollView and give snapping or paging effect.


public class CustomHorizontalScrollView extends HorizontalScrollView implements
OnTouchListener, OnGestureListener {

        private static final int SWIPE_MIN_DISTANCE = 300;

private static final int SWIPE_THRESHOLD_VELOCITY = 300;
private static final int SWIPE_PAGE_ON_FACTOR = 10;


private GestureDetector gestureDetector;
private int scrollTo = 0;
private int maxItem = 0;
private int activeItem = 0;
private float prevScrollX = 0;
private boolean start = true;
private int itemWidth = 0;
private float currentScrollX;
private boolean flingDisable = true;

public CustomHorizontalScrollView(Context context) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
}

public CustomHorizontalScrollView(Context context, int maxItem,
int itemWidth) {
this(context);
this.maxItem = maxItem;
this.itemWidth = itemWidth;
gestureDetector = new GestureDetector(this);
this.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
Boolean returnValue = gestureDetector.onTouchEvent(event);

int x = (int) event.getRawX();

switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (start) {
this.prevScrollX = x;
start = false;
}
break;
case MotionEvent.ACTION_UP:
start = true;
this.currentScrollX = x;
int minFactor = itemWidth
/ ConfigurationParams.SWIPE_PAGE_ON_FACTOR;

if ((this.prevScrollX - this.currentScrollX) > minFactor) {
if (activeItem < maxItem - 1)
activeItem = activeItem + 1;

} else if ((this.currentScrollX - this.prevScrollX) > minFactor) {
if (activeItem > 0)
activeItem = activeItem - 1;
}
System.out.println("horizontal : " + activeItem);
scrollTo = activeItem * itemWidth;
this.smoothScrollTo(scrollTo, 0);
returnValue = true;
break;
}
return returnValue;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (flingDisable)
return false;
boolean returnValue = false;
float ptx1 = 0, ptx2 = 0;
if (e1 == null || e2 == null)
return false;
ptx1 = e1.getX();
ptx2 = e2.getX();
// right to left

if (ptx1 - ptx2 > ConfigurationParams.SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > ConfigurationParams.SWIPE_THRESHOLD_VELOCITY) {
if (activeItem < maxItem - 1)
activeItem = activeItem + 1;

returnValue = true;

} else if (ptx2 - ptx1 > ConfigurationParams.SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > ConfigurationParams.SWIPE_THRESHOLD_VELOCITY) {
if (activeItem > 0)
activeItem = activeItem - 1;

returnValue = true;
}
scrollTo = activeItem * itemWidth;
this.smoothScrollTo(0, scrollTo);
return returnValue;
}

@Override
public boolean onDown(MotionEvent e) {
return false;
}

@Override
public void onLongPress(MotionEvent e) {
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}

@Override
public void onShowPress(MotionEvent e) {
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}



Step 3) TestHorizontalActivity- This class will be used as test class


public class TestHorizontalActivity extends Activity {

private LinearLayout linearLayout;
private CustomHorizontalScrollView horizontalScrollView;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
horizontalScrollView = new CustomHorizontalScrollView(this, 3,
width);
setContentView(R.layout.horizontal);
linearLayout = (LinearLayout) findViewById(R.id.layer);
linearLayout.addView(horizontalScrollView);

LinearLayout container = new LinearLayout(this);
container.setLayoutParams(new LayoutParams(width, height));
// container.setHeight(height);

TextView textView = new TextView(this);
textView.setWidth(width);
textView.setHeight(height);
textView.setGravity(Gravity.CENTER);
textView.setText("First  Screen");
textView.setBackgroundColor(Color.CYAN);
container.addView(textView);

textView = new TextView(this);
textView.setWidth(width);
textView.setHeight(height);
textView.setGravity(Gravity.CENTER);
textView.setText("Second  Screen");
textView.setBackgroundColor(Color.GREEN);
container.addView(textView);

textView = new TextView(this);
textView.setWidth(width);
textView.setHeight(height);
textView.setGravity(Gravity.CENTER);
textView.setText("Third  Screen");
textView.setBackgroundColor(Color.RED);
container.addView(textView);

horizontalScrollView.addView(container);
}

}


This is it, these 3 simple steps and you are ready to run a paging effect in your android application.

In next blog i will discuss how we can use combination of both horizontal scrolling and vertical scrolling.


I hope this post will be helpful. 

Please share your comments and and you can also contact me @ yash@iotasol.com or visit our site www.iotasol.com , www.iotadomains.com.

Wednesday, August 3, 2011

Extracting Zip File in Android

The code snippet below explains how to extract zip file in android
You can call extractZip method with activity from where its called and path of file to be extracted
It will extract the files on same destination path
/**
 * This method will extract the zip file from location inside application 
 * 
 * @param activity
 * @param destinationPath - Path of zip file
 * @param _location - location where file will be extracted
 * @author Yash@iotasol.com
 */
public void extractZip(Activity activity,String destinationPath,String _location ) {
System.out.println(" ctr : ::: extract zip" + zipExtracted + " : "
+ destinationPath);
FileInputStream fin = null;
ZipInputStream zin = null;
try {
fin = new FileInputStream(destinationPath);
zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
extractDirectoryZipEntry(ze.getName(), _location);
} else {
extractZipFileEntry(_location, zin, ze);
}
}
zipExtracted = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (zin != null) {
zin.close();
}
if (fin != null) {
fin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}



/**
 * This method will work on every single zip entry and store it in location
 * Buffered input stream is used to speed up the downloading process
 * @param _location
 * @param zin
 * @param ze
 * @author Yash@iotasol.com
 */
private void extractZipFileEntry(String _location, ZipInputStream zin,
ZipEntry ze) {
System.out.println(">>>>> file : " + ze.getName());
FileOutputStream fout = null;
BufferedOutputStream bout = null;
try {
fout = new FileOutputStream(_location + ze.getName());
bout = new BufferedOutputStream(fout, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = zin.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
}
} catch (Exception r) {
r.printStackTrace();
} finally {
try {
zin.closeEntry();
if (bout != null)
bout.close();
if (fout != null)
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(">>>>> file downloaded: " + ze.getName());
}


/**
 * This method will work on every single zip entry in case if its a directory, and creates new
 * 
 * @param dir
 * @param _location
 * @author Yash@iotasol.com
 */
private void extractDirectoryZipEntry(String dir, String _location) {

File f = new File(_location + dir);

if (!f.isDirectory()) {
f.mkdirs();
}
}


I hope this post will be helpful. 

Please share your comments and and you can also contact me @ yash@iotasol.com or visit our site www.iotasol.com , www.iotadomains.com.