Question

I am trying to achieve the following effect:

2 screens, first is my main activity and the second is where the user can change the app properties of the main activity.

I want my app to use a swipe gesture from left-right/right-left to move between screens. I dont want this looped just back and forth.

I have done some research and found other peoples advice on SO but still cant seem to implement this correctly.

Edited code as per answer below:

Here is my code for Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.sample"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="18" />

<application 
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

    <activity 
        android:name=".ScreenSlideActivity"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

</manifest>

My ScreenSlideActivity:

package com.sample.sample;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class ScreenSlideActivity extends FragmentActivity {
//number of pages
private static final int NUM_PAGES = 2;

private ViewPager mPager;

private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen_slide);

    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {

            invalidateOptionsMenu();
        }
    });
}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return ScreenSlidePageFragment.create(position);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}  
}

I dont quite get how to create multiple instance of this for another page:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen_slide);

Then, this used to work, but now it doesnt since the addition of the screen slide:

My ButtonActivity:

package com.example.android.animationsdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class ButtonActivity extends ScreenSlideActivity {

ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
static int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_screen_slide_page);

setupPlayPauseButton1();
setupPlayPauseButton2();
setupPlayPauseButton3();
setupPlayPauseButton4();
setupPlayPauseButton5();

}

private void setupPlayPauseButton1() {
    btn1 = (ImageButton) findViewById(R.id.m1_btn);

    btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            i +=1;
            if (i % 2 == 0) {
                btn1.setImageResource(R.drawable.pause);
            } else {
                btn1.setImageResource(R.drawable.play);

            }
        }
    });
}

private void setupPlayPauseButton2() {
    btn2 = (ImageButton) findViewById(R.id.m2_btn);

    btn2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            i +=1;
            if (i % 2 == 0) {
                btn2.setImageResource(R.drawable.pause);
            } else {
                btn2.setImageResource(R.drawable.play);

            }
        }
    });
}

private void setupPlayPauseButton3() {
    btn3 = (ImageButton) findViewById(R.id.m3_btn);

    btn3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            i +=1;
            if (i % 2 == 0) {
                btn3.setImageResource(R.drawable.pause);
            } else {
                btn3.setImageResource(R.drawable.play);

            }
        }
    });
}

private void setupPlayPauseButton4() {
    btn4 = (ImageButton) findViewById(R.id.m4_btn);

    btn4.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            i +=1;
            if (i % 2 == 0) {
                btn4.setImageResource(R.drawable.pause);
            } else {
                btn4.setImageResource(R.drawable.play);

            }
        }
    });
}

private void setupPlayPauseButton5() {
    btn5 = (ImageButton) findViewById(R.id.m5_btn);

    btn5.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            i +=1;
            if (i % 2 == 0) {
                btn5.setImageResource(R.drawable.pause);
            } else {
                btn5.setImageResource(R.drawable.play);

            }
        }
    });
}
}

The ScreeSlidePageFragment:

package com.example.android.animationsdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class ScreenSlidePageFragment extends Fragment {

public static final String ARG_PAGE = "page";

private int mPageNumber;

public static ScreenSlidePageFragment create(int pageNumber) {
    ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, pageNumber);
    fragment.setArguments(args);
    return fragment;
}

public ScreenSlidePageFragment() {
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.fragment_screen_slide_page, container, false);

    return rootView;
}

public int getPageNumber() {
    return mPageNumber;
}
}

activity_screen_slide.xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

fragment_screen_slide_page.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:orientation="vertical"
tools:context=".ScreenSlideActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/m1_btn_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/m1_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@drawable/pause" />

        <ImageButton
            android:id="@+id/m2_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@null"
            android:src="@drawable/pause" />

        <ImageButton
            android:id="@+id/m3_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@null"
            android:src="@drawable/pause" />

        <ImageButton
            android:id="@+id/m4_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@null"
            android:src="@drawable/pause" />

        <ImageButton
            android:id="@+id/m5_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@null"
            android:src="@drawable/pause" />
    </LinearLayout>

<LinearLayout
    android:id="@+id/m1_stave_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<LinearLayout
    android:id="@+id/m1_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_gravity="right"
    android:background="@drawable/stave_top"
    android:orientation="horizontal" >

</LinearLayout>

<LinearLayout
    android:id="@+id/m2_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_gravity="right"
    android:background="@drawable/stave_middle"
    android:orientation="horizontal" >


</LinearLayout>

<LinearLayout
    android:id="@+id/m3_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_gravity="right"
    android:background="@drawable/stave_middle"
    android:orientation="horizontal" >


</LinearLayout>

<LinearLayout
    android:id="@+id/m4_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_gravity="right"
    android:background="@drawable/stave_middle"
    android:orientation="horizontal" >

</LinearLayout>

<LinearLayout
    android:id="@+id/m5_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_gravity="right"
    android:background="@drawable/stave_bottom"
    android:orientation="horizontal" >

</LinearLayout>    
</LinearLayout>
</LinearLayout>

</RelativeLayout>

EDIT PageTwoFragment:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class PageTwoFragment extends Fragment {

ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
int i = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
        R.layout.page1_layout, container, false);
btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
}

private void setupPlayPauseButton1(View view) { 
btn1 = (ImageButton) view.findViewById(R.id.m1_btn);
    btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            i +=1;
            if (i % 2 == 0) {
                btn1.setImageResource(R.drawable.pause);
            } else {
                btn1.setImageResource(R.drawable.play);

            }
        }
    });
}
}
Was it helpful?

Solution

You can accomplish the behavior you want by using Androids ViewPager. You would need to use Fragments for your two views but it would work just the same.

ViewPagers have built-in swipe gestures to transition through pages, and they display screen slide animations by default, so you don't need to create any.

Check out this example from the Android Documentation, it has a sample application you can download and test out too.

http://developer.android.com/training/animation/screen-slide.html

Let me know if this helps,

Good Luck!

EDIT

Basically what you need to do is make sure BOTH of the views that you want to slide between are fragments. Once you have them both as fragments, inside your PagerAdapters getItem(int Position) method you would do something like this:

@Override
public Fragment getItem(int i) {

    Fragment frag = null;

    switch (i) {
    case 0:
        frag = PageOneFragment.create();
        break;
    case 1:
        frag = PageTwoFragment.create();
        break;

     }
    return frag;
}

Each fragment can have its own xml file, that can be whatever you want for the layout. Each fragment can contain elements such as buttons and images that you can add listeners to.

Make sense? If not, let me know where it is crashing and maybe post the error.

EDIT 2

The example from the developer site does have a lot more code because it wraps a few demos into one. Look at the code below, It does exactly what you want with the sliding, you just need to add in your xml's to the two fragments.

MainActivity

public class MainActivity extends FragmentActivity {

private final static int NUM_PAGES = 2;
private ViewPager mPager;
private ScreenSlidePagerAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mAdapter);
}

/**
 * A simple pager adapter that represents 5 ScreenSlidePageFragment objects,
 * in sequence.
 */
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {

        Fragment frag = null;

        switch (i) {
        case 0:
            frag = new PageOneFragment();
            break;
        case 1:
            frag = new PageTwoFragment();
            break;

        }
        return frag;
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

}

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" >

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

PageOneFragment (This will be whatever you want your first view to be, you can give it a custom xml layout and define elements just like in an activity)

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PageOneFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_one, container, false);
    return rootView;
}
}

fragment_one.xml(the layout for your first view)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    style="?android:textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:lineSpacingMultiplier="1.2"
    android:padding="16dp"
    android:text="Example text" />

FragmentTwo (This will be your second view, the view you slide to)

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PageTwoFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_two, container, false);

    return rootView;
}
}

fragment_two.xml (The layout for your second view)

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

</LinearLayout>

From EDIT 2 down is pretty much a simpler version of the example app, all you need to do is add the custom xml layouts for the two fragments. If you would like to know more about fragments and their capabilities this a great place to start:

http://developer.android.com/guide/components/fragments.html

onCreateView Edit

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ 
  ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.page1_layout, container, false);
  btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
  setupPlayPauseButton1(rootView); 
return rootView; 
}

Good Luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top