0

I have a layout with a two buttons, one that is in view and one that is hidden. When a ViewPager is swiped I need it so that the button that is in view shrinks to half its size and moves to the right while the button that was not previously in view grows to fill the space the large button took up e.g

From this: enter image description here To this: enter image description here

Then when the ViewPager is swiped the other way the Previous button needs to shrink to nothing while the Next button grows again to fill the space again.

So far I have tried multiple approaches from the simple animateLayoutChanges="true" in the containing LinearLayout to using ScaleAnimation and btnMain.animate().translationX(); but none seem to have the desired effect.

Here is the layout for the buttons in XML:

<LinearLayout
        android:id="@+id/ll_button_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_marginBottom="20dp">

        <Button
            android:id="@+id/btn_back"
            android:layout_marginStart="32dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="@dimen/creds_button_height"
            android:background="@color/colorPrimary"
            android:letterSpacing="0.0"
            android:visibility="gone"
            android:layout_gravity="end"
            android:layout_marginEnd="-22dp"
            android:text="@string/previous"
            android:textAllCaps="false"
            android:textColor="@color/colorWhite"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btn_main"
            android:layout_marginStart="32dp"
            android:layout_width="0dp"
            android:layout_marginEnd="32dp"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:layout_height="@dimen/creds_button_height"
            android:background="@color/colorPrimaryDark"
            android:letterSpacing="0.0"
            android:text="@string/login_caps"
            android:textAllCaps="false"
            android:textColor="@color/colorWhite" />


    </LinearLayout>
MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49

1 Answers1

0

You can use PageTransformer like in this article with OnPageChangeListener to do that. With something like this:

public class MainActivity extends AppCompatActivity {

    public ViewPager mViewPager;
    public Button mFirstButton;
    public Button mSecondButton;

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

        mFirstButton = (Button) findViewById(R.id.first_button);
        mFirstButton.setText("NEXT");
        mSecondButton = (Button) findViewById(R.id.second_button);
        mSecondButton.setText("PREVIOUS");

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new CustomPagerAdapter(this));
        mViewPager.setPageTransformer(false, mButtonsPageTransformer);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    mFirstButton.setText("NEXT");
                } else {
                    mFirstButton.setText("PREVIOUS");
                    mSecondButton.setText("NEXT");
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    public enum CustomPagerEnum {

        FIRST_PAGE(R.string.first_page_title, R.layout.first_page),
        SECOND_PAGE(R.string.second_page_title, R.layout.second_page);

        private int mTitleResId;
        private int mLayoutResId;

        CustomPagerEnum(int titleResId, int layoutResId) {
            mTitleResId = titleResId;
            mLayoutResId = layoutResId;
        }

        public int getTitleResId() {
            return mTitleResId;
        }

        public int getLayoutResId() {
            return mLayoutResId;
        }

    }

    private class CustomPagerAdapter extends PagerAdapter {

        private Context mContext;

        public CustomPagerAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return CustomPagerEnum.values().length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
            return mContext.getString(customPagerEnum.getTitleResId());
        }

        @Override
        public Object instantiateItem(ViewGroup collection, int position) {
            CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
            LayoutInflater inflater = LayoutInflater.from(mContext);
            ViewGroup layout = (ViewGroup) inflater.inflate(customPagerEnum.getLayoutResId(), collection, false);
            collection.addView(layout);
            return layout;
        }

        @Override
        public void destroyItem(ViewGroup collection, int position, Object view) {
            collection.removeView((View) view);
        }
    }

    ViewPager.PageTransformer mButtonsPageTransformer = new ViewPager.PageTransformer() {

        public void transformPage(View view, float position) {
            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
            } else if (position <= 1) { // [-1,1]
                LinearLayout.LayoutParams firstButtonLayoutParams = (LinearLayout.LayoutParams) mFirstButton.getLayoutParams();
                firstButtonLayoutParams.weight = 1 - position;
                mFirstButton.setLayoutParams(firstButtonLayoutParams);
            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
            }
        }
    };
}

activity_main.xml like

<?xml version="1.0" encoding="utf-8"?>
<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="{YOUR_CONTEXT}.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_above="@+id/buttons_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <LinearLayout
        android:id="@+id/buttons_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/first_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:lines="1"
            />
        <Button
            android:id="@+id/second_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:lines="1"
            />
    </LinearLayout>
</RelativeLayout>

and first_page.xml and second_page.xml like:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/first_page_title"/>

</RelativeLayout>

you'll got something like that:

ViewPager PageTransformer

Of course, it's just concept and you need more elegant (with transition animation) solution for text changing, button styles, etc.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79