View Pager - how can I place fixed buttons on top of it

Viewed 7780

In my android app I have a view pager that is essentially a slide show of pictures. those pictures are the background images of 5 different fragments

I want to put two buttons on top of it all, and those buttons to remain fixed on screen. How can I do that?

public class WhatIsThis extends SherlockFragmentActivity {

    private static final int NUM_PAGES = 5;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_slides_view_pager);

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

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            super.onBackPressed();
        } else {

            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }


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


        @Override
        public SherlockFragment getItem(int position) {
            if(position == 1) {
                return new WelcomeSlideFragmentA();
            } else if (position == 2) {
                return new WelcomeSlideFragmentB();
            } else if (position == 3) {
                return new WelcomeSlideFragmentC();
            } else if (position == 4) {
                return new WelcomeSlideFragmentD();
            } else {
                return new WelcomeSlideFragmentE();
            }
        }

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

}

welcome_slides_view_pager.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" />
4 Answers

You can put these two button above of view pager inside in layout

2 ways.

1 You can put this buttons above the view pager on your layout.

2 If you have a Action Bar, you can create a Activity with NAVIGATION_MODE_TABS and add this tabs to your activity with the ActionBar methods.

I think the second way is more cool.

You try like this:

<?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:orientation="vertical" >
       <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="horizontal" >
          <Button
             android:id="@+id/button1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Button1" />
          <Button
             android:id="@+id/button2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Button2" />Ï
       </LinearLayout>
       <android.support.v4.view.ViewPager
             android:id="@+id/pager"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
</LinearLayout>
Related