Slide Animations when switching from one activity to another – Java Android

Create a anim/ folder inside res/ in your Android Project and create the following files:

slide_in_left.xml

<set xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shareInterpolator=”false” >

    <translate
        android:duration=”200″
        android:fromXDelta=”-100%”
        android:fromYDelta=”0%”
        android:toXDelta=”0%”
        android:toYDelta=”0%” />

</set>

slide_in_right.xml

<set xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shareInterpolator=”false” >

    <translate
        android:duration=”200″
        android:fromXDelta=”100%”
        android:fromYDelta=”0%”
        android:toXDelta=”0%”
        android:toYDelta=”0%” />


</set>

slide_out_left.xml

<set xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shareInterpolator=”false” >

    <translate
        android:duration=”200″
        android:fromXDelta=”0%”
        android:fromYDelta=”0%”
        android:toXDelta=”-100%”
        android:toYDelta=”0%” />


</set>

slide_out_right.xml

<set xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shareInterpolator=”false” >

    <translate
        android:duration=”200″
        android:fromXDelta=”0%”
        android:fromYDelta=”0%”
        android:toXDelta=”100%”
        android:toYDelta=”0%” />


</set>

Then you can use animations like this:

Intent intent = new Intent(FirstAct.this, SecondAct.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

Bye!


Handle Swipe Gestures in Java Android: Right to Left (Forward)

Hi guys,

Instead of using a ViewPager you can handle touch gestures like this (Useful when you have only two views and you’d like to swipe only in a single direction – For example: Only Right to Left to go forward):

 //This method is called everytime the user touches the screen
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = touchevent.getX();
                y1 = touchevent.getY();
                break;
            case MotionEvent.ACTION_UP:
                x2 = touchevent.getX();
                y2 = touchevent.getY();

                if (x1>x2) {
                    Log.i(“log_tag”, “Right to left Swipe Detected!”);
                }
                break;
        }
        return false;
    }

How to pass CustomObject/ArrayList from one activity to another? Using Parcelable

We’d like to pass an Object from one Activity to another in Java Android, there are two ways to do this operation, the first one is to use a Serializable Object while the second one is to use a Parcelable Object.

It is strongly recommended due to performance issues to use the first solution.

Imagine you have a Song Custom Object, you need implements Parcelable in your Song class:

import android.os.Parcel;
import android.os.Parcelable;

public class Song implements Parcelable {
    private String title;
    private String author;
    private String album;
    private String genre;
    private String location;

    public Song (String title, String author, String album, String genre, String location) {
        this.title = title;
        this.author=author;
        this.album=album;
        this.genre=genre;
        this.location=location;
    }

    public Song(Parcel in) { //Constructor when loading from Parcel
        title = in.readString();
        author = in.readString();
        album = in.readString();
        genre = in.readString();
        location = in.readString();
    }

//Getters and Setters

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

//Required method, but not used
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

//When Parcel this Object write every variable to Parcel dest
        dest.writeString(title);
        dest.writeString(author);
        dest.writeString(album);
        dest.writeString(genre);
        dest.writeString(location);
    }

//Parcelable Creator, every Parcelable Object should implement this method.

    public static final Parcelable.Creator<Song> CREATOR = new Parcelable.Creator<Song>() {
        public Song createFromParcel(Parcel in) {
            return new Song(in);
        }

        public Song[] newArray(int size) {
            return new Song[size];
        }
    };
}

Then in the first Activity:

ArrayList<Song> passedList = new ArrayList <Song>();
Intent intent = new Intent(context, SecondActivity.class);
String pkg = getPackageName();
intent.putExtra(pkg+”values”, passedList);
startActivity(intent);

In the second Activity:

ArrayList<Song> passedValues = getIntent().getParcelableArrayListExtra(pkg+”values”);

YouTube Downloader Android

YouTube Downloader: My New App! 😉

 Download your videos from YouTube and convert them to MP3 from your Android Device!

Convert your videos from YouTube to MP3s directly from your smartphone. Just open the official YouTube App, choose your video and tap on the share icon (top-right corner) and select YouTube Downloader, then Download your song…Enjoy!

– Amedeo