Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8935397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:58:30+00:00 2026-06-15T09:58:30+00:00

I want to use JakeWharton library in my app. I’m intrested in example SampleTabsDefault

  • 0

I want to use JakeWharton library in my app. I’m intrested in example SampleTabsDefault from samples: enter image description here
And now, im stuck. In this sample is changing only Text, and I want to change fragments (each fragment has own layout, and each layout have different controls like buttons, checkboxs etc.)
I’m thinking I must change this method in class extends from FragmentPagerAdapter :

public Fragment getItem(int position) {
            return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
        }

I’d like to create new fragment (with own layout) in according to particular position.

I have implemented this code:

public class MainActivity extends FragmentActivity {
    private static final String[] CONTENT = new String[] {"My Profile", "Statistics"};

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

        FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());

        ViewPager pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);



        }


    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }


        public Fragment getItem(int position) {
            if (position==0)
            return MyProfile.newInstance(CONTENT[position % CONTENT.length]);
            else if (position==1)
            return Statistics.newInstance(CONTENT[position % CONTENT.length]);
            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

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

and

public class MyProfile extends Fragment {
      private static final String KEY_CONTENT = "TestFragment:Content";


        public static MyProfile newInstance(String content) {
            MyProfile fragment = new MyProfile();

            fragment.mContent = content;

            return fragment;
        }

        private String mContent = "???";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
                mContent = savedInstanceState.getString(KEY_CONTENT);
            }


        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_basic, container, false);
            Button button = (Button) view.findViewById(R.id.fragment_button);

            button.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Activity activity = getActivity();

                    if (activity != null) {
                        Toast.makeText(activity, "fragmen1", Toast.LENGTH_SHORT).show();
                    }
                }

            });

            return view;
        }
           public void onSaveInstanceState(Bundle outState) {
                super.onSaveInstanceState(outState);
                outState.putString(KEY_CONTENT, mContent);
            }
}

and this:

public class Statistics extends Fragment {
      private static final String KEY_CONTENT = "TestFragment:Content";


        public static Statistics newInstance(String content) {
            Statistics fragment = new Statistics();

            fragment.mContent = content;

            return fragment;
        }

        private String mContent = "???";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
                mContent = savedInstanceState.getString(KEY_CONTENT);
            }


        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_basic2, container, false);
            Button button = (Button) view.findViewById(R.id.fragment_button);

            button.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Activity activity = getActivity();

                    if (activity != null) {
                        Toast.makeText(activity, "fragmen2", Toast.LENGTH_SHORT).show();
                    }
                }

            });

            return view;
        }

           @Override
            public void onSaveInstanceState(Bundle outState) {
                super.onSaveInstanceState(outState);
                outState.putString(KEY_CONTENT, mContent);
            }
}

and it doesn’t work… I can’t switch page, and my layouts from fragments doesn’t display…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:58:31+00:00Added an answer on June 15, 2026 at 9:58 am

    In your getItem() method inside your FragmentPagerAdapter you can return other fragments depending on the position. So for example:

     public Fragment getItem(int position) {
     switch(position)
           {
                   case 0:
                        TestFragment fragment = new TestFragment();  
                        return fragment;
    
                    case 1:
                        TestFragment2 fragment2 = new TestFragment2();  
                        return fragment2;
    
             }
    
                        DefaultFragment fragment3 = new DefaultFragment();  
                        return fragment3;
    
             }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use UIImagePickerController, and I found this example Added to .h @interface MenuScene
I'm creating a new app and I want to use some libraries like this:
I want to use the DiskLruCache from Jake Wharton in my Android app based
I use 2 WebForms ans I want use one webform and from this I
I want use a RegularExpressionValidator for a date in this form yyyy-mm-dd (example: 2012-11-29)
I want use this 1 for using Bar code or QR code scanner. I
i want use some data from a website with web service. i have a
We have a powerbuilder application and we want use a scanner through this application
I want use php curl with oauth to get the JSON data from twitter
i want to use title on text. is this possible to make title on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.